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
4ecba58
commit de85002
Showing
14 changed files
with
448 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
172 changes: 172 additions & 0 deletions
172
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,172 @@ | ||
<?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 | ||
{ | ||
$functionsAnalyzer = new FunctionsAnalyzer(); | ||
$indicesToClear = []; | ||
$tokenSlices = []; | ||
|
||
for ($index = $tokens->count() - 1; $index > 0; --$index) { | ||
if (!$tokens[$index]->equals([T_STRING, 'get_class'], false)) { | ||
continue; | ||
} | ||
|
||
if (!$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; | ||
} | ||
|
||
$indicesToClear[$index] = [$braceOpenIndex, current($variableTokensIndices), $braceCloseIndex]; | ||
} | ||
|
||
foreach ($indicesToClear as $index => $items) { | ||
$tokenSlices += $this->getReplacementTokenSlices($tokens, $index, $items[1]); | ||
$this->clearGetClassCall($tokens, $index, $items[0], $items[2]); | ||
} | ||
|
||
$tokens->insertSlices($tokenSlices); | ||
} | ||
|
||
private function getReplacementTokenSlices(Tokens $tokens, int $index, int $variableIndex): array | ||
{ | ||
return [ | ||
$index => [ | ||
new Token([T_VARIABLE, $tokens[$variableIndex]->getContent()]), | ||
new Token([T_DOUBLE_COLON, '::']), | ||
new Token([CT::T_CLASS_CONSTANT, 'class']), | ||
], | ||
]; | ||
} | ||
|
||
private function clearGetClassCall(Tokens $tokens, int $index, int $braceOpenIndex, int $braceCloseIndex): void | ||
{ | ||
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); | ||
} | ||
} |
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.