Skip to content

Commit

Permalink
[Php55] Handle pre-slash string on StringClassNameToClassConstantRect…
Browse files Browse the repository at this point in the history
…or (#2905)
  • Loading branch information
samsonasik committed Sep 4, 2022
1 parent 754fdd1 commit bdee765
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ class Fixture
{
return 'Rector\Tests\Php55\Rector\String_\StringClassNameToClassConstantRector\Source\AnotherClass';
}

public function preSlash()
{
return '\Rector\Tests\Php55\Rector\String_\StringClassNameToClassConstantRector\Source\AnotherClass';
}
}

?>
Expand All @@ -27,11 +22,6 @@ class Fixture
{
return \Rector\Tests\Php55\Rector\String_\StringClassNameToClassConstantRector\Source\AnotherClass::class;
}

public function preSlash()
{
return \Rector\Tests\Php55\Rector\String_\StringClassNameToClassConstantRector\Source\AnotherClass::class;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\Php55\Rector\String_\StringClassNameToClassConstantRector\Fixture;

final class PreSlash
{
public function preSlash()
{
return '\Rector\Tests\Php55\Rector\String_\StringClassNameToClassConstantRector\Source\AnotherClass';
}
}

?>
-----
<?php

namespace Rector\Tests\Php55\Rector\String_\StringClassNameToClassConstantRector\Fixture;

final class PreSlash
{
public function preSlash()
{
return '\\' . \Rector\Tests\Php55\Rector\String_\StringClassNameToClassConstantRector\Source\AnotherClass::class;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\BinaryOp\Concat;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name\FullyQualified;
Expand Down Expand Up @@ -105,6 +106,14 @@ public function refactor(Node $node): ?Node
}

$fullyQualified = new FullyQualified($classLikeName);
if ($classLikeName !== $node->value) {
$preSlashCount = strlen($node->value) - strlen($classLikeName);
$preSlash = str_repeat('\\', $preSlashCount);
$string = new String_($preSlash);

return new Concat($string, new ClassConstFetch($fullyQualified, 'class'));
}

return new ClassConstFetch($fullyQualified, 'class');
}

Expand Down

0 comments on commit bdee765

Please sign in to comment.