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
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 (#262)
* [CodingStandard] ClassStringToClassCosntantFixer init * [CodingStandard] add ClassStringToClassConstantFixer to README [ci skip] * [CodingStandard] use T_CONSTANT_ENCAPSED_STRING * [CodingStandard] allow namespaced classes in ClassStringToClassConstantFixer * [CodingStandard] ClassString fixer - use preg_match over class_exists, thanks @dg * [CodingStandard] ClassString fixer - use better message name * [ecs] add ClassConstant fixer to rules * use longer error message to not confuse ClassConstant fixer * [CodingStandard] ClassStringtoClassConstantFixer - run only for namespaced classes, run after NoLeadinSlashImport and after OrderedImports * [ecs] reorder rules * apply ClassStringToClassConstantFixer on code, few fixes * [CodingStandard] ClassStringToClassConstantFixer - add few improvements based on @keradus version * cs fixes
- Loading branch information
Tomáš Votruba
committed
Jul 15, 2017
1 parent
ce3506a
commit ddf3505
Showing
13 changed files
with
232 additions
and
11 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
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\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; | ||
} | ||
|
||
// remove quotes "" around the string | ||
$potentialClassOrInterface = substr($token->getContent(), 1, -1); | ||
if (! $this->isClassOrInterface($potentialClassOrInterface)) { | ||
continue; | ||
} | ||
|
||
$token->clear(); | ||
$tokens->insertAt($index, $this->convertClassOrInterfaceNameToTokens($potentialClassOrInterface)); | ||
} | ||
} | ||
|
||
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 | ||
{ | ||
// exception for often used "error" string; because class_exists() is case-insensitive | ||
if ($potentialClassOrInterface === 'error') { | ||
return false; | ||
} | ||
|
||
return class_exists($potentialClassOrInterface) | ||
|| interface_exists($potentialClassOrInterface) | ||
|| (bool) preg_match(self::CLASS_OR_INTERFACE_PATTERN, $potentialClassOrInterface); | ||
} | ||
|
||
private function convertClassOrInterfaceNameToTokens(string $potentialClassOrInterface): Tokens | ||
{ | ||
$tokens = Tokens::fromCode(sprintf( | ||
'<?php echo \\%s::class;', | ||
$potentialClassOrInterface | ||
)); | ||
|
||
$tokens->clearRange(0, 2); // clear start "<?php" | ||
$tokens[$tokens->getSize() - 1]->clear(); // clear end ";" | ||
$tokens->clearEmptyTokens(); | ||
|
||
return $tokens; | ||
} | ||
} |
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
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
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
ddf3505
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.
@TomasVotruba
Please don't ping ppl in comimtlogs, now I get notification each time you do anything with that commit... (eg merge, rebase, squash, anything...)
extra, extra, extra annoying...
ddf3505
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.
I see. Sorry about that.