-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
304 additions
and
68 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
129 changes: 129 additions & 0 deletions
129
...dsofphp/php-cs-fixer/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,129 @@ | ||
<?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\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 <[email protected]> | ||
*/ | ||
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 isRisky(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
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) | ||
), | ||
], | ||
null, | ||
'If classes marked with `@readonly` annotation were extended anyway, applying this fixer may break the inheritance for their child classes.' | ||
); | ||
} | ||
|
||
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
Oops, something went wrong.