mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-24 11:44:14 +01:00
Merge pull request #1326 from rectorphp/orig-split
[CodingStyle] Add SplitStringClassConstantToClassConstFetchRector
This commit is contained in:
commit
c7d1960468
@ -9,7 +9,7 @@
|
||||
"composer/xdebug-handler": "^1.3",
|
||||
"jean85/pretty-package-versions": "^1.2",
|
||||
"nette/robot-loader": "^3.1",
|
||||
"nette/utils": "^2.5 || ^3.0",
|
||||
"nette/utils": "^2.5|^3.0",
|
||||
"nikic/php-parser": "^4.2.1",
|
||||
"phpstan/phpstan": "0.11.5",
|
||||
"sebastian/diff": "^3.0",
|
||||
|
@ -12,3 +12,4 @@ services:
|
||||
# Rector\CodingStyle\Rector\ClassMethod\ReturnArrayClassMethodToYieldRector: ~
|
||||
Rector\CodingStyle\Rector\String_\SymplifyQuoteEscapeRector: ~
|
||||
Rector\CodingStyle\Rector\ClassConst\SplitGroupedConstantsAndPropertiesRector: ~
|
||||
Rector\CodingStyle\Rector\String_\SplitStringClassConstantToClassConstFetchRector: ~
|
||||
|
@ -0,0 +1,79 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\CodingStyle\Rector\String_;
|
||||
|
||||
use Nette\Utils\Strings;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Scalar\String_;
|
||||
use Rector\Rector\AbstractRector;
|
||||
use Rector\RectorDefinition\CodeSample;
|
||||
use Rector\RectorDefinition\RectorDefinition;
|
||||
|
||||
final class SplitStringClassConstantToClassConstFetchRector extends AbstractRector
|
||||
{
|
||||
public function getDefinition(): RectorDefinition
|
||||
{
|
||||
return new RectorDefinition('Separate class constant in a string to class constant fetch and string', [
|
||||
new CodeSample(
|
||||
<<<'CODE_SAMPLE'
|
||||
class SomeClass
|
||||
{
|
||||
const HI = true;
|
||||
}
|
||||
|
||||
class AnotherClass
|
||||
{
|
||||
public function get()
|
||||
{
|
||||
return 'SomeClass::HI';
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
,
|
||||
<<<'CODE_SAMPLE'
|
||||
class SomeClass
|
||||
{
|
||||
const HI = true;
|
||||
}
|
||||
|
||||
class AnotherClass
|
||||
{
|
||||
public function get()
|
||||
{
|
||||
return SomeClass::class . '::HI';
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [String_::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param String_ $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
if (substr_count($node->value, '::') !== 1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// a possible constant reference
|
||||
[$possibleClass, $secondPart] = Strings::split($node->value, '#::#');
|
||||
|
||||
if (! class_exists($possibleClass)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$classConstFetch = new Node\Expr\ClassConstFetch(new Node\Name\FullyQualified($possibleClass), 'class');
|
||||
|
||||
return new Node\Expr\BinaryOp\Concat($classConstFetch, new Node\Scalar\String_('::' . $secondPart));
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\String_\SplitStringClassConstantToClassConstFetchRector\Fixture;
|
||||
|
||||
class SomeClass
|
||||
{
|
||||
const HI = true;
|
||||
}
|
||||
|
||||
class AnotherClass
|
||||
{
|
||||
public function get()
|
||||
{
|
||||
return 'Rector\CodingStyle\Tests\Rector\String_\SplitStringClassConstantToClassConstFetchRector\Fixture\SomeClass::HI';
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\String_\SplitStringClassConstantToClassConstFetchRector\Fixture;
|
||||
|
||||
class SomeClass
|
||||
{
|
||||
const HI = true;
|
||||
}
|
||||
|
||||
class AnotherClass
|
||||
{
|
||||
public function get()
|
||||
{
|
||||
return \Rector\CodingStyle\Tests\Rector\String_\SplitStringClassConstantToClassConstFetchRector\Fixture\SomeClass::class . '::HI';
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@ -0,0 +1,19 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\String_\SplitStringClassConstantToClassConstFetchRector;
|
||||
|
||||
use Rector\CodingStyle\Rector\String_\SplitStringClassConstantToClassConstFetchRector;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
|
||||
final class SplitStringClassConstantToClassConstFetchRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
public function test(): void
|
||||
{
|
||||
$this->doTestFiles([__DIR__ . '/Fixture/fixture.php.inc']);
|
||||
}
|
||||
|
||||
protected function getRectorClass(): string
|
||||
{
|
||||
return SplitStringClassConstantToClassConstFetchRector::class;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user