Skip to content
This repository has been archived by the owner on Dec 3, 2023. It is now read-only.

Commit

Permalink
[CodingStandard] ClassStringToClassCosntantFixer init
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jul 14, 2017
1 parent cd7f6e6 commit cd8ada1
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 0 deletions.
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(), "'");
}
}
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;
}
}
3 changes: 3 additions & 0 deletions packages/CodingStandard/tests/Fixer/Php/fixed/fixed.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

$classType = \DateTime::class;
3 changes: 3 additions & 0 deletions packages/CodingStandard/tests/Fixer/Php/fixed/fixed2.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

$classType = \DateTimeInterface::class;
3 changes: 3 additions & 0 deletions packages/CodingStandard/tests/Fixer/Php/wrong/wrong.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

$classType = 'DateTime';
3 changes: 3 additions & 0 deletions packages/CodingStandard/tests/Fixer/Php/wrong/wrong2.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

$classType = 'DateTimeInterface';

0 comments on commit cd8ada1

Please sign in to comment.