-
-
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(PhpdocReadonlyCommentToKeywordFixer): Introduction (#7353)
- Loading branch information
1 parent
6d3c3bc
commit 76375a9
Showing
18 changed files
with
411 additions
and
9 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
22 changes: 22 additions & 0 deletions
22
doc/rules/class_notation/phpdoc_readonly_class_comment_to_keyword.rst
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,22 @@ | ||
================================================= | ||
Rule ``phpdoc_readonly_class_comment_to_keyword`` | ||
================================================= | ||
|
||
Converts readonly comment on classes to the readonly keyword. | ||
|
||
Examples | ||
-------- | ||
|
||
Example #1 | ||
~~~~~~~~~~ | ||
|
||
.. code-block:: diff | ||
--- Original | ||
+++ New | ||
<?php | ||
- /** @readonly */ | ||
- class C { | ||
+ | ||
+ readonly class C { | ||
} |
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
122 changes: 122 additions & 0 deletions
122
src/Fixer/ClassNotation/PhpdocReadonlyClassCommentToKeywordFixer.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,122 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of PHP CS Fixer. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* Dariusz Rumiński <dariusz.ruminski@gmail.com> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace PhpCsFixer\Fixer\ClassNotation; | ||
|
||
use PhpCsFixer\AbstractFixer; | ||
use PhpCsFixer\DocBlock\DocBlock; | ||
use PhpCsFixer\FixerDefinition\FixerDefinition; | ||
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface; | ||
use PhpCsFixer\FixerDefinition\VersionSpecification; | ||
use PhpCsFixer\FixerDefinition\VersionSpecificCodeSample; | ||
use PhpCsFixer\Tokenizer\Token; | ||
use PhpCsFixer\Tokenizer\Tokens; | ||
|
||
/** | ||
* @author Marcel Behrmann <marcel@behrmann.dev> | ||
*/ | ||
final class PhpdocReadonlyClassCommentToKeywordFixer extends AbstractFixer | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
* | ||
* Must run before NoEmptyPhpdocFixer, NoExtraBlankLinesFixer, PhpdocAlignFixer. | ||
* Must run after AlignMultilineCommentFixer, CommentToPhpdocFixer, PhpdocIndentFixer, PhpdocScalarFixer, PhpdocToCommentFixer, PhpdocTypesFixer. | ||
*/ | ||
public function getPriority(): int | ||
{ | ||
return 4; | ||
} | ||
|
||
public function isCandidate(Tokens $tokens): bool | ||
{ | ||
return \PHP_VERSION_ID >= 8_02_00 && $tokens->isTokenKindFound(T_DOC_COMMENT); | ||
} | ||
|
||
public function getDefinition(): FixerDefinitionInterface | ||
{ | ||
return new FixerDefinition( | ||
'Converts readonly comment on classes to the readonly keyword.', | ||
[ | ||
new VersionSpecificCodeSample( | ||
<<<EOT | ||
<?php | ||
/** @readonly */ | ||
class C { | ||
}\n | ||
EOT, | ||
new VersionSpecification(8_02_00) | ||
), | ||
] | ||
); | ||
} | ||
|
||
protected function applyFix(\SplFileInfo $file, Tokens $tokens): void | ||
{ | ||
foreach ($tokens as $index => $token) { | ||
if (!$token->isGivenKind(T_DOC_COMMENT)) { | ||
continue; | ||
} | ||
|
||
$doc = new DocBlock($token->getContent()); | ||
|
||
$annotations = $doc->getAnnotationsOfType('readonly'); | ||
|
||
if (0 === \count($annotations)) { | ||
continue; | ||
} | ||
|
||
foreach ($annotations as $annotation) { | ||
$annotation->remove(); | ||
} | ||
|
||
$mainIndex = $index; | ||
$index = $tokens->getNextMeaningfulToken($index); | ||
$addReadonly = true; | ||
|
||
while ($tokens[$index]->isGivenKind([ | ||
T_ABSTRACT, | ||
T_FINAL, | ||
T_PRIVATE, | ||
T_PUBLIC, | ||
T_PROTECTED, | ||
T_READONLY, | ||
])) { | ||
if ($tokens[$index]->isGivenKind([T_READONLY])) { | ||
$addReadonly = false; | ||
} | ||
|
||
$index = $tokens->getNextMeaningfulToken($index); | ||
} | ||
|
||
if (!$tokens[$index]->isGivenKind(T_CLASS)) { | ||
continue; | ||
} | ||
|
||
if ($addReadonly) { | ||
$tokens->insertAt($index, [new Token([T_READONLY, 'readonly']), new Token([T_WHITESPACE, ' '])]); | ||
} | ||
|
||
$newContent = $doc->getContent(); | ||
|
||
if ('' === $newContent) { | ||
$tokens->clearTokenAndMergeSurroundingWhitespace($mainIndex); | ||
|
||
continue; | ||
} | ||
|
||
$tokens[$mainIndex] = new Token([T_DOC_COMMENT, $doc->getContent()]); | ||
} | ||
} | ||
} |
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
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.