Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add MultilinePromotedPropertiesFixer (#617)
- Loading branch information
1 parent
434f8ab
commit 479c935
Showing
15 changed files
with
371 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
"../src" | ||
] | ||
}, | ||
"timeout": 10, | ||
"timeout": 5, | ||
"logs": { | ||
"text": "php://stdout", | ||
"github": true, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of PHP CS Fixer: custom fixers. | ||
* | ||
* (c) 2018 Kuba Werłos | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpCsFixerCustomFixers\Fixer; | ||
|
||
use PhpCsFixer\Fixer\WhitespacesAwareFixerInterface; | ||
use PhpCsFixer\FixerDefinition\FixerDefinition; | ||
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface; | ||
use PhpCsFixer\FixerDefinition\VersionSpecification; | ||
use PhpCsFixer\FixerDefinition\VersionSpecificCodeSample; | ||
use PhpCsFixer\Tokenizer\Analyzer\WhitespacesAnalyzer; | ||
use PhpCsFixer\Tokenizer\CT; | ||
use PhpCsFixer\Tokenizer\Tokens; | ||
use PhpCsFixer\WhitespacesFixerConfig; | ||
use PhpCsFixerCustomFixers\Analyzer\ConstructorAnalyzer; | ||
|
||
final class MultilinePromotedPropertiesFixer extends AbstractFixer implements WhitespacesAwareFixerInterface | ||
{ | ||
/** @var WhitespacesFixerConfig */ | ||
private $whitespacesConfig; | ||
|
||
public function getDefinition(): FixerDefinitionInterface | ||
{ | ||
return new FixerDefinition( | ||
'Constructor having promoted properties must have them in separate lines.', | ||
[ | ||
new VersionSpecificCodeSample( | ||
'<?php class Foo { | ||
public function __construct(private array $a, private bool $b, private int $i) {} | ||
} | ||
', | ||
new VersionSpecification(80000) | ||
), | ||
] | ||
); | ||
} | ||
|
||
public function setWhitespacesConfig(WhitespacesFixerConfig $config): void | ||
{ | ||
$this->whitespacesConfig = $config; | ||
} | ||
|
||
/** | ||
* Must run after PromotedConstructorPropertyFixer. | ||
*/ | ||
public function getPriority(): int | ||
{ | ||
return 0; | ||
} | ||
|
||
public function isCandidate(Tokens $tokens): bool | ||
{ | ||
return $tokens->isAnyTokenKindsFound([ | ||
CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PRIVATE, | ||
CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PROTECTED, | ||
CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PUBLIC, | ||
]); | ||
} | ||
|
||
public function isRisky(): bool | ||
{ | ||
return false; | ||
} | ||
|
||
public function fix(\SplFileInfo $file, Tokens $tokens): void | ||
{ | ||
$constructorAnalyzer = new ConstructorAnalyzer(); | ||
|
||
for ($index = $tokens->count() - 1; $index > 0; $index--) { | ||
if (!$tokens[$index]->isGivenKind(\T_CLASS)) { | ||
continue; | ||
} | ||
|
||
$constructorAnalysis = $constructorAnalyzer->findNonAbstractConstructor($tokens, $index); | ||
if ($constructorAnalysis === null) { | ||
continue; | ||
} | ||
|
||
/** @var int $openParenthesis */ | ||
$openParenthesis = $tokens->getNextTokenOfKind($constructorAnalysis->getConstructorIndex(), ['(']); | ||
$closeParenthesis = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $openParenthesis); | ||
|
||
if (!$this->shouldBeFixed($tokens, $openParenthesis, $closeParenthesis)) { | ||
continue; | ||
} | ||
|
||
$this->fixParameters($tokens, $openParenthesis, $closeParenthesis); | ||
} | ||
} | ||
|
||
private function shouldBeFixed(Tokens $tokens, int $openParenthesis, int $closeParenthesis): bool | ||
{ | ||
for ($index = $openParenthesis + 1; $index < $closeParenthesis; $index++) { | ||
if ( | ||
$tokens[$index]->isGivenKind([ | ||
CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PRIVATE, | ||
CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PROTECTED, | ||
CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PUBLIC, | ||
]) | ||
) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private function fixParameters(Tokens $tokens, int $openParenthesis, int $closeParenthesis): void | ||
{ | ||
$indent = WhitespacesAnalyzer::detectIndent($tokens, $openParenthesis); | ||
|
||
$tokens->ensureWhitespaceAtIndex( | ||
$closeParenthesis - 1, | ||
1, | ||
$this->whitespacesConfig->getLineEnding() . $indent | ||
); | ||
|
||
/** @var int $index */ | ||
$index = $tokens->getPrevMeaningfulToken($closeParenthesis); | ||
|
||
while ($index > $openParenthesis) { | ||
/** @var int $index */ | ||
$index = $tokens->getPrevMeaningfulToken($index); | ||
|
||
/** @var null|array{isStart: bool, type: int} $blockType */ | ||
$blockType = Tokens::detectBlockType($tokens[$index]); | ||
if ($blockType !== null && !$blockType['isStart']) { | ||
$index = $tokens->findBlockStart($blockType['type'], $index); | ||
continue; | ||
} | ||
|
||
if (!$tokens[$index]->equalsAny(['(', ','])) { | ||
continue; | ||
} | ||
|
||
$tokens->ensureWhitespaceAtIndex( | ||
$index + 1, | ||
0, | ||
$this->whitespacesConfig->getLineEnding() . $indent . $this->whitespacesConfig->getIndent() | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.