-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Introduce
general_attribute_remove
fixer (#8339)
- Loading branch information
1 parent
87df022
commit f2f92d2
Showing
18 changed files
with
727 additions
and
88 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
================================= | ||
Rule ``general_attribute_remove`` | ||
================================= | ||
|
||
Removes configured attributes by their respective FQN. | ||
|
||
Configuration | ||
------------- | ||
|
||
``attributes`` | ||
~~~~~~~~~~~~~~ | ||
|
||
List of FQNs of attributes for removal. | ||
|
||
Allowed types: ``list<string>`` | ||
|
||
Default value: ``[]`` | ||
|
||
Examples | ||
-------- | ||
|
||
Example #1 | ||
~~~~~~~~~~ | ||
|
||
With configuration: ``['attributes' => ['\\A\\B\\Foo']]``. | ||
|
||
.. code-block:: diff | ||
--- Original | ||
+++ New | ||
<?php | ||
-#[\A\B\Foo] | ||
function foo() {} | ||
Example #2 | ||
~~~~~~~~~~ | ||
|
||
With configuration: ``['attributes' => ['\\A\\B\\Foo', 'A\\B\\Bar']]``. | ||
|
||
.. code-block:: diff | ||
--- Original | ||
+++ New | ||
<?php | ||
use A\B\Bar as BarAlias; | ||
-#[\A\B\Foo] | ||
-#[BarAlias] | ||
function foo() {} | ||
References | ||
---------- | ||
|
||
- Fixer class: `PhpCsFixer\\Fixer\\AttributeNotation\\GeneralAttributeRemoveFixer <./../../../src/Fixer/AttributeNotation/GeneralAttributeRemoveFixer.php>`_ | ||
- Test class: `PhpCsFixer\\Tests\\Fixer\\AttributeNotation\\GeneralAttributeRemoveFixerTest <./../../../tests/Fixer/AttributeNotation/GeneralAttributeRemoveFixerTest.php>`_ | ||
|
||
The test class defines officially supported behaviour. Each test case is a part of our backward compatibility promise. |
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
142 changes: 142 additions & 0 deletions
142
src/Fixer/AttributeNotation/GeneralAttributeRemoveFixer.php
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,142 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of PHP CS Fixer. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* Dariusz Rumiński <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace PhpCsFixer\Fixer\AttributeNotation; | ||
|
||
use PhpCsFixer\AbstractFixer; | ||
use PhpCsFixer\Fixer\ConfigurableFixerInterface; | ||
use PhpCsFixer\Fixer\ConfigurableFixerTrait; | ||
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver; | ||
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverInterface; | ||
use PhpCsFixer\FixerConfiguration\FixerOptionBuilder; | ||
use PhpCsFixer\FixerDefinition\CodeSample; | ||
use PhpCsFixer\FixerDefinition\FixerDefinition; | ||
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface; | ||
use PhpCsFixer\Tokenizer\Analyzer\Analysis\AttributeAnalysis; | ||
use PhpCsFixer\Tokenizer\Analyzer\AttributeAnalyzer; | ||
use PhpCsFixer\Tokenizer\Tokens; | ||
|
||
/** | ||
* @author Raffaele Carelle <[email protected]> | ||
* | ||
* @implements ConfigurableFixerInterface<_AutogeneratedInputConfiguration, _AutogeneratedComputedConfiguration> | ||
* | ||
* @phpstan-import-type _AttributeItems from AttributeAnalysis | ||
* @phpstan-import-type _AttributeItem from AttributeAnalysis | ||
* | ||
* @phpstan-type _AutogeneratedInputConfiguration array{ | ||
* attributes?: list<string> | ||
* } | ||
* @phpstan-type _AutogeneratedComputedConfiguration array{ | ||
* attributes: list<string> | ||
* } | ||
*/ | ||
final class GeneralAttributeRemoveFixer extends AbstractFixer implements ConfigurableFixerInterface | ||
{ | ||
/** @use ConfigurableFixerTrait<_AutogeneratedInputConfiguration, _AutogeneratedComputedConfiguration> */ | ||
use ConfigurableFixerTrait; | ||
|
||
public function getDefinition(): FixerDefinitionInterface | ||
{ | ||
return new FixerDefinition( | ||
'Removes configured attributes by their respective FQN.', | ||
[ | ||
new CodeSample( | ||
'<?php | ||
#[\A\B\Foo] | ||
function foo() {} | ||
', | ||
['attributes' => ['\A\B\Foo']] | ||
), | ||
new CodeSample( | ||
'<?php | ||
use A\B\Bar as BarAlias; | ||
#[\A\B\Foo] | ||
#[BarAlias] | ||
function foo() {} | ||
', | ||
['attributes' => ['\A\B\Foo', 'A\B\Bar']] | ||
), | ||
] | ||
); | ||
} | ||
|
||
public function getPriority(): int | ||
{ | ||
return 0; | ||
} | ||
|
||
public function isCandidate(Tokens $tokens): bool | ||
{ | ||
return \defined('T_ATTRIBUTE') && $tokens->isTokenKindFound(T_ATTRIBUTE); | ||
} | ||
|
||
protected function applyFix(\SplFileInfo $file, Tokens $tokens): void | ||
{ | ||
if (0 === \count($this->configuration['attributes'])) { | ||
return; | ||
} | ||
|
||
$index = 0; | ||
|
||
while (null !== $index = $tokens->getNextTokenOfKind($index, [[T_ATTRIBUTE]])) { | ||
$attributeAnalysis = AttributeAnalyzer::collectOne($tokens, $index); | ||
|
||
$endIndex = $attributeAnalysis->getEndIndex(); | ||
|
||
$removedCount = 0; | ||
foreach ($attributeAnalysis->getAttributes() as $element) { | ||
$fullname = AttributeAnalyzer::determineAttributeFullyQualifiedName($tokens, $element['name'], $element['start']); | ||
|
||
if (!\in_array($fullname, $this->configuration['attributes'], true)) { | ||
continue; | ||
} | ||
|
||
$tokens->clearRange($element['start'], $element['end']); | ||
++$removedCount; | ||
|
||
$siblingIndex = $tokens->getNonEmptySibling($element['end'], 1); | ||
|
||
// Clear element comma | ||
if (',' === $tokens[$siblingIndex]->getContent()) { | ||
$tokens->clearAt($siblingIndex); | ||
} | ||
} | ||
|
||
// Clear whole attribute if all are removed (multiline attribute case) | ||
if (\count($attributeAnalysis->getAttributes()) === $removedCount) { | ||
$tokens->clearRange($attributeAnalysis->getStartIndex(), $attributeAnalysis->getEndIndex()); | ||
} | ||
|
||
// Clear trailing comma | ||
$tokenIndex = $tokens->getMeaningfulTokenSibling($attributeAnalysis->getClosingBracketIndex(), -1); | ||
if (',' === $tokens[$tokenIndex]->getContent()) { | ||
$tokens->clearAt($tokenIndex); | ||
} | ||
|
||
$index = $endIndex; | ||
} | ||
} | ||
|
||
protected function createConfigurationDefinition(): FixerConfigurationResolverInterface | ||
{ | ||
return new FixerConfigurationResolver([ | ||
(new FixerOptionBuilder('attributes', 'List of FQNs of attributes for removal.')) | ||
->setAllowedTypes(['string[]']) | ||
->setDefault([]) | ||
->getOption(), | ||
]); | ||
} | ||
} |
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.