This repository has been archived by the owner on Dec 3, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 191
[CodingStandard] ClassStringToClassCosntantFixer init #262
Merged
Merged
Changes from 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
db335eb
[CodingStandard] ClassStringToClassCosntantFixer init
TomasVotruba 1f25901
[CodingStandard] add ClassStringToClassConstantFixer to README [ci skip]
TomasVotruba 32f1c75
[CodingStandard] use T_CONSTANT_ENCAPSED_STRING
TomasVotruba 8e38dc6
[CodingStandard] allow namespaced classes in ClassStringToClassConsta…
TomasVotruba e493333
[CodingStandard] ClassString fixer - use preg_match over class_exists…
TomasVotruba 79ffef0
[CodingStandard] ClassString fixer - use better message name
TomasVotruba dbaad31
[ecs] add ClassConstant fixer to rules
TomasVotruba 7166976
use longer error message to not confuse ClassConstant fixer
TomasVotruba 9df20a0
[CodingStandard] ClassStringtoClassConstantFixer - run only for names…
TomasVotruba ec2b362
[ecs] reorder rules
TomasVotruba 9b0eca7
apply ClassStringToClassConstantFixer on code, few fixes
TomasVotruba 53dd7f1
[CodingStandard] ClassStringToClassConstantFixer - add few improvemen…
TomasVotruba 61a1019
cs fixes
TomasVotruba File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
116 changes: 116 additions & 0 deletions
116
packages/CodingStandard/src/Fixer/Php/ClassStringToClassConstantFixer.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,116 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Symplify\CodingStandard\Fixer\Php; | ||
|
||
use PhpCsFixer\Fixer\DefinedFixerInterface; | ||
use PhpCsFixer\FixerDefinition\CodeSample; | ||
use PhpCsFixer\FixerDefinition\FixerDefinition; | ||
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface; | ||
use PhpCsFixer\Tokenizer\CT; | ||
use PhpCsFixer\Tokenizer\Token; | ||
use PhpCsFixer\Tokenizer\Tokens; | ||
use SplFileInfo; | ||
|
||
final class ClassStringToClassConstantFixer implements DefinedFixerInterface | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private const CLASS_OR_INTERFACE_PATTERN = '#^[A-Z]\w*[a-z]\w*(\\\\[A-Z]\w*[a-z]\w*)+\z#'; | ||
|
||
public function getDefinition(): FixerDefinitionInterface | ||
{ | ||
return new FixerDefinition( | ||
'`::class` references should be used over string for classes and interfaces.', | ||
[ | ||
new CodeSample( | ||
'<?php | ||
|
||
$className = "DateTime"; | ||
'), | ||
new CodeSample( | ||
'<?php | ||
|
||
$interfaceName = "DateTimeInterface"; | ||
'), | ||
new CodeSample( | ||
'<?php | ||
|
||
$interfaceName = "Nette\Utils\DateTime"; | ||
'), | ||
] | ||
); | ||
} | ||
|
||
public function isCandidate(Tokens $tokens): bool | ||
{ | ||
return $tokens->isTokenKindFound(T_CONSTANT_ENCAPSED_STRING); | ||
} | ||
|
||
public function fix(SplFileInfo $file, Tokens $tokens): void | ||
{ | ||
/** @var Token[] $revertedTokens */ | ||
$revertedTokens = array_reverse($tokens->toArray(), true); | ||
|
||
foreach ($revertedTokens as $index => $token) { | ||
if (! $token->isGivenKind(T_CONSTANT_ENCAPSED_STRING)) { | ||
continue; | ||
} | ||
|
||
$potentialClassOrInterface = trim($token->getContent(), "'"); | ||
if ($this->isClassOrInterface($potentialClassOrInterface)) { | ||
$token->clear(); // overrideAt() fails on "Illegal offset type" | ||
|
||
$classOrInterfaceTokens = $this->convertClassOrInterfaceNameToTokens($potentialClassOrInterface); | ||
$tokens->insertAt($index, array_merge($classOrInterfaceTokens, [ | ||
new Token([T_DOUBLE_COLON, '::']), | ||
new Token([CT::T_CLASS_CONSTANT, 'class']), | ||
])); | ||
} | ||
} | ||
} | ||
|
||
public function isRisky(): bool | ||
{ | ||
return false; | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return self::class; | ||
} | ||
|
||
public function getPriority(): int | ||
{ | ||
// should be run before the OrderedImportsFixer, after the NoLeadingImportSlashFixer | ||
return -25; | ||
} | ||
|
||
public function supports(SplFileInfo $file): bool | ||
{ | ||
return true; | ||
} | ||
|
||
private function isClassOrInterface(string $potentialClassOrInterface): bool | ||
{ | ||
return class_exists($potentialClassOrInterface) | ||
|| interface_exists($potentialClassOrInterface) | ||
|| (bool) preg_match(self::CLASS_OR_INTERFACE_PATTERN, $potentialClassOrInterface); | ||
} | ||
|
||
/** | ||
* @return Token[] | ||
*/ | ||
private function convertClassOrInterfaceNameToTokens(string $potentialClassOrInterface): array | ||
{ | ||
$tokens = []; | ||
$nameParts = explode('\\', $potentialClassOrInterface); | ||
|
||
foreach ($nameParts as $namePart) { | ||
$tokens[] = new Token([T_NS_SEPARATOR, '\\']); | ||
$tokens[] = new Token([T_STRING, $namePart]); | ||
} | ||
|
||
return $tokens; | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
packages/CodingStandard/tests/Fixer/Php/ClassStringToClassConstantFixerTest.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,44 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Symplify\CodingStandard\Tests\Fixer\Php; | ||
|
||
use PhpCsFixer\Fixer\FixerInterface; | ||
use PhpCsFixer\Test\AbstractFixerTestCase; | ||
use Symplify\CodingStandard\Fixer\Php\ClassStringToClassConstantFixer; | ||
|
||
final class ClassStringToClassConstantFixerTest extends AbstractFixerTestCase | ||
{ | ||
/** | ||
* @dataProvider provideFixCases() | ||
*/ | ||
public function testFix(string $expected, string $input): void | ||
{ | ||
$this->doTest($expected, $input); | ||
} | ||
|
||
/** | ||
* @return string[][] | ||
*/ | ||
public function provideFixCases(): array | ||
{ | ||
return [ | ||
[ | ||
file_get_contents(__DIR__ . '/fixed/fixed.php.inc'), | ||
file_get_contents(__DIR__ . '/wrong/wrong.php.inc'), | ||
], | ||
[ | ||
file_get_contents(__DIR__ . '/fixed/fixed2.php.inc'), | ||
file_get_contents(__DIR__ . '/wrong/wrong2.php.inc'), | ||
], | ||
[ | ||
file_get_contents(__DIR__ . '/fixed/fixed3.php.inc'), | ||
file_get_contents(__DIR__ . '/wrong/wrong3.php.inc'), | ||
], | ||
]; | ||
} | ||
|
||
protected function createFixer(): FixerInterface | ||
{ | ||
return new ClassStringToClassConstantFixer; | ||
} | ||
} |
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,3 @@ | ||
<?php | ||
|
||
$classType = \DateTime::class; |
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,3 @@ | ||
<?php | ||
|
||
$classType = \DateTimeInterface::class; |
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,9 @@ | ||
<?php | ||
|
||
class SomeClass | ||
{ | ||
public function getSomeData($className = \Nette\Utils\DateTime::class) | ||
{ | ||
|
||
} | ||
} |
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,3 @@ | ||
<?php | ||
|
||
$classType = 'DateTime'; |
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,3 @@ | ||
<?php | ||
|
||
$classType = 'DateTimeInterface'; |
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,9 @@ | ||
<?php | ||
|
||
class SomeClass | ||
{ | ||
public function getSomeData($className = 'Nette\Utils\DateTime') | ||
{ | ||
|
||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a lot of falsy positives here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
drop anything that may creates wrong changes, it's better to convert too few than too much
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For example?