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 190
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CodingStandard] ClassStringToClassCosntantFixer init
- Loading branch information
1 parent
ce3506a
commit db335eb
Showing
6 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
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,88 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Symplify\CodingStandard\Fixer\Php; | ||
|
||
use Nette\Utils\Strings; | ||
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 | ||
{ | ||
public function getDefinition(): FixerDefinitionInterface | ||
{ | ||
return new FixerDefinition( | ||
'::class refences should be used over string.', | ||
[ | ||
new CodeSample( | ||
'<?php | ||
$className = "DateTime"; | ||
'), | ||
new CodeSample( | ||
'<?php | ||
$interfaceName = "DateTimeInterface"; | ||
'), | ||
] | ||
); | ||
} | ||
|
||
public function isCandidate(Tokens $tokens): bool | ||
{ | ||
return true; | ||
} | ||
|
||
public function fix(SplFileInfo $file, Tokens $tokens): void | ||
{ | ||
foreach (array_reverse($tokens->toArray(), true) as $index => $token) { | ||
/** @var Token $token */ | ||
if (! $this->isStringToken($token)) { | ||
continue; | ||
} | ||
|
||
$potentialClassOrInterfaceName = trim($token->getContent(), "'"); | ||
if (class_exists($potentialClassOrInterfaceName) || interface_exists($potentialClassOrInterfaceName)) { | ||
$token->clear(); // overrideAt() fails on "Illegal offset type" | ||
$tokens->insertAt($index, [ | ||
new Token([T_NS_SEPARATOR, '\\']), | ||
new Token([T_STRING, $potentialClassOrInterfaceName]), | ||
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 | ||
{ | ||
// @todo combine with namespace import fixer/sniff | ||
return 0; | ||
} | ||
|
||
public function supports(SplFileInfo $file): bool | ||
{ | ||
return true; | ||
} | ||
|
||
private function isStringToken(Token $token): bool | ||
{ | ||
return Strings::startsWith($token->getContent(), "'") | ||
&& Strings::endsWith($token->getContent(), "'"); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
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,40 @@ | ||
<?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'), | ||
], | ||
]; | ||
} | ||
|
||
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,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'; |