Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GetClassToClassKeywordFixer - introduction
- Loading branch information
1 parent
c1a4a91
commit c5a3dc0
Showing
14 changed files
with
418 additions
and
3 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
44 changes: 44 additions & 0 deletions
44
doc/rules/language_construct/get_class_to_class_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,44 @@ | ||
=================================== | ||
Rule ``get_class_to_class_keyword`` | ||
=================================== | ||
|
||
Replace ``get_class`` calls on object variables with class keyword syntax. | ||
|
||
.. warning:: Using this rule is risky. | ||
|
||
Risky if the ``get_class`` function is overridden. | ||
|
||
Examples | ||
-------- | ||
|
||
Example #1 | ||
~~~~~~~~~~ | ||
|
||
.. code-block:: diff | ||
--- Original | ||
+++ New | ||
<?php | ||
-get_class($a); | ||
+$a::class; | ||
Example #2 | ||
~~~~~~~~~~ | ||
|
||
.. code-block:: diff | ||
--- Original | ||
+++ New | ||
<?php | ||
$date = new \DateTimeImmutable(); | ||
-$class = get_class($date); | ||
+$class = $date::class; | ||
Rule sets | ||
--------- | ||
|
||
The rule is part of the following rule set: | ||
|
||
@PHP80Migration:risky | ||
Using the `@PHP80Migration:risky <./../../ruleSets/PHP80MigrationRisky.rst>`_ rule set will enable the ``get_class_to_class_keyword`` rule. |
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
157 changes: 157 additions & 0 deletions
157
src/Fixer/LanguageConstruct/GetClassToClassKeywordFixer.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,157 @@ | ||
<?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\LanguageConstruct; | ||
|
||
use PhpCsFixer\AbstractFixer; | ||
use PhpCsFixer\FixerDefinition\FixerDefinition; | ||
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface; | ||
use PhpCsFixer\FixerDefinition\VersionSpecification; | ||
use PhpCsFixer\FixerDefinition\VersionSpecificCodeSample; | ||
use PhpCsFixer\Tokenizer\Analyzer\FunctionsAnalyzer; | ||
use PhpCsFixer\Tokenizer\CT; | ||
use PhpCsFixer\Tokenizer\Token; | ||
use PhpCsFixer\Tokenizer\Tokens; | ||
|
||
/** | ||
* @author John Paul E. Balandan, CPA <paulbalandan@gmail.com> | ||
*/ | ||
final class GetClassToClassKeywordFixer extends AbstractFixer | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getDefinition(): FixerDefinitionInterface | ||
{ | ||
return new FixerDefinition( | ||
'Replace `get_class` calls on object variables with class keyword syntax.', | ||
[ | ||
new VersionSpecificCodeSample( | ||
"<?php\nget_class(\$a);\n", | ||
new VersionSpecification(80000) | ||
), | ||
new VersionSpecificCodeSample( | ||
"<?php\n\n\$date = new \\DateTimeImmutable();\n\$class = get_class(\$date);\n", | ||
new VersionSpecification(80000) | ||
), | ||
], | ||
null, | ||
'Risky if the `get_class` function is overridden.' | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* Must run before MultilineWhitespaceBeforeSemicolonsFixer. | ||
* Must run after NoSpacesAfterFunctionNameFixer, NoSpacesInsideParenthesisFixer. | ||
*/ | ||
public function getPriority(): int | ||
{ | ||
return 1; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function isCandidate(Tokens $tokens): bool | ||
{ | ||
return \PHP_VERSION_ID >= 80000 && $tokens->isAllTokenKindsFound([T_STRING, T_VARIABLE]); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function isRisky(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function applyFix(\SplFileInfo $file, Tokens $tokens): void | ||
{ | ||
for ($index = $tokens->count() - 1; $index > 0; --$index) { | ||
if (!$tokens[$index]->equals([T_STRING, 'get_class'], false)) { | ||
continue; | ||
} | ||
|
||
if (!(new FunctionsAnalyzer())->isGlobalFunctionCall($tokens, $index)) { | ||
continue; | ||
} | ||
|
||
$braceOpenIndex = $tokens->getNextMeaningfulToken($index); | ||
$braceCloseIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $braceOpenIndex); | ||
|
||
if ($braceCloseIndex === $tokens->getNextMeaningfulToken($braceOpenIndex)) { | ||
// get_class with no arguments | ||
continue; | ||
} | ||
|
||
$meaningfulTokensCount = 0; | ||
$variableTokensIndices = []; | ||
|
||
for ($i = $braceOpenIndex + 1; $i < $braceCloseIndex; ++$i) { | ||
if (!$tokens[$i]->equalsAny([[T_WHITESPACE], [T_COMMENT], [T_DOC_COMMENT], '(', ')'])) { | ||
++$meaningfulTokensCount; | ||
} | ||
|
||
if (!$tokens[$i]->isGivenKind(T_VARIABLE)) { | ||
continue; | ||
} | ||
|
||
if ('$this' === strtolower($tokens[$i]->getContent())) { | ||
// get_class($this) | ||
continue 2; | ||
} | ||
|
||
$variableTokensIndices[] = $i; | ||
} | ||
|
||
if ($meaningfulTokensCount > 1 || 1 !== \count($variableTokensIndices)) { | ||
// argument contains more logic, or more arguments, or no variable argument | ||
continue; | ||
} | ||
|
||
$this->fixGetClassCall($tokens, $index, $braceOpenIndex, $braceCloseIndex, current($variableTokensIndices)); | ||
} | ||
} | ||
|
||
private function fixGetClassCall(Tokens $tokens, int $index, int $braceOpenIndex, int $braceCloseIndex, int $variableIndex): void | ||
{ | ||
$replacements = [ | ||
new Token([T_VARIABLE, $tokens[$variableIndex]->getContent()]), | ||
new Token([T_DOUBLE_COLON, '::']), | ||
new Token([CT::T_CLASS_CONSTANT, 'class']), | ||
]; | ||
|
||
for ($i = $braceOpenIndex; $i <= $braceCloseIndex; ++$i) { | ||
if ($tokens[$i]->isGivenKind([T_WHITESPACE, T_COMMENT, T_DOC_COMMENT])) { | ||
continue; | ||
} | ||
|
||
$tokens->clearTokenAndMergeSurroundingWhitespace($i); | ||
} | ||
|
||
$prevIndex = $tokens->getPrevMeaningfulToken($index); | ||
|
||
if ($tokens[$prevIndex]->isGivenKind(T_NS_SEPARATOR)) { | ||
$tokens->clearAt($prevIndex); | ||
} | ||
|
||
$tokens->clearAt($index); | ||
$tokens->insertSlices([$index => $replacements]); | ||
} | ||
} |
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.