mirror of
https://github.com/elyby/php-code-style.git
synced 2025-05-31 14:12:05 +05:30
124 lines
3.1 KiB
PHP
124 lines
3.1 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Ely\CS\Fixer\LanguageConstruct;
|
|
|
|
use Ely\CS\Fixer\AbstractFixer;
|
|
use PhpCsFixer\FixerDefinition\CodeSample;
|
|
use PhpCsFixer\FixerDefinition\FixerDefinition;
|
|
use PhpCsFixer\Tokenizer\CT;
|
|
use PhpCsFixer\Tokenizer\Token;
|
|
use PhpCsFixer\Tokenizer\Tokens;
|
|
use SplFileInfo;
|
|
|
|
/**
|
|
* Replaces Yii2 BaseObject::className() usages with native ::class keyword, introduced in PHP 5.5.
|
|
*
|
|
* @author ErickSkrauch <erickskrauch@ely.by>
|
|
*/
|
|
final class RemoveClassNameMethodUsagesFixer extends AbstractFixer {
|
|
|
|
/**
|
|
* @inheritdoc
|
|
*/
|
|
public function getDefinition() {
|
|
return new FixerDefinition(
|
|
'Converts Yii2 `BaseObject::className()` method usage into `::class` keyword.',
|
|
[
|
|
new CodeSample(
|
|
'<?php
|
|
|
|
use Foo\Bar\Baz;
|
|
|
|
$className = Baz::className();
|
|
'
|
|
),
|
|
]
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @inheritdoc
|
|
*/
|
|
public function isCandidate(Tokens $tokens) {
|
|
return $tokens->isTokenKindFound(T_STRING);
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function isRisky() {
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
protected function applyFix(SplFileInfo $file, Tokens $tokens) {
|
|
for ($index = $tokens->count() - 4; $index > 0; --$index) {
|
|
$candidate = $this->getReplaceCandidate($tokens, $index);
|
|
if ($candidate === null) {
|
|
continue;
|
|
}
|
|
|
|
$this->fixClassNameMethodUsage(
|
|
$tokens,
|
|
$index,
|
|
$candidate[0], // brace open
|
|
$candidate[1] // brace close
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param Tokens $tokens
|
|
* @param int $index
|
|
*
|
|
* @return null|array
|
|
*/
|
|
private function getReplaceCandidate(Tokens $tokens, $index) {
|
|
if (!$tokens[$index]->isGivenKind(T_STRING)) {
|
|
return null;
|
|
}
|
|
|
|
$braceOpenIndex = $tokens->getNextMeaningfulToken($index);
|
|
if (!$tokens[$braceOpenIndex]->equals('(')) {
|
|
return null;
|
|
}
|
|
|
|
$braceCloseIndex = $tokens->getNextMeaningfulToken($braceOpenIndex);
|
|
if (!$tokens[$braceCloseIndex]->equals(')')) {
|
|
return null;
|
|
}
|
|
|
|
$doubleColon = $tokens->getPrevMeaningfulToken($index);
|
|
if (!$tokens[$doubleColon]->isGivenKind([T_DOUBLE_COLON])) {
|
|
return null;
|
|
}
|
|
|
|
$methodName = $tokens[$index]->getContent();
|
|
if ($methodName !== 'className') {
|
|
return null;
|
|
}
|
|
|
|
return [
|
|
$braceOpenIndex,
|
|
$braceCloseIndex,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @param Tokens $tokens
|
|
* @param int $index
|
|
* @param int $braceOpenIndex
|
|
* @param int $braceCloseIndex
|
|
*/
|
|
private function fixClassNameMethodUsage(Tokens $tokens, int $index, int $braceOpenIndex, int $braceCloseIndex) {
|
|
$tokens->clearTokenAndMergeSurroundingWhitespace($braceCloseIndex);
|
|
$tokens->clearTokenAndMergeSurroundingWhitespace($braceOpenIndex);
|
|
$tokens->clearAt($index);
|
|
$tokens->insertAt($index, new Token([CT::T_CLASS_CONSTANT, 'class']));
|
|
}
|
|
|
|
}
|