fix another constant in repeated literal to class constant

This commit is contained in:
TomasVotruba 2020-05-09 21:50:52 +02:00
parent 83ef24bd9d
commit ff88e4b37a
5 changed files with 84 additions and 5 deletions

View File

@ -25,13 +25,34 @@ final class ScopeAwareNodeFinder
$this->betterNodeFinder = $betterNodeFinder;
}
/**
* Find node based on $callable or null, when the nesting scope is broken
* @param class-string[] $allowedTypes
*/
public function findParentType(Node $node, array $allowedTypes): ?Node
{
$callable = function (Node $node) use ($allowedTypes) {
foreach ($allowedTypes as $allowedType) {
if (!is_a($node, $allowedType)) {
continue;
}
return true;
}
return false;
};
return $this->findParent($node, $callable, $allowedTypes);
}
/**
* Find node based on $callable or null, when the nesting scope is broken
* @param class-string[] $allowedTypes
*/
public function findParent(Node $node, callable $callable, array $allowedTypes): ?Node
{
$parentNestingBreakTypes = array_diff(ControlStructure::NODE_TYPES, $allowedTypes);
$parentNestingBreakTypes = array_diff(ControlStructure::BREAKING_SCOPE_NODE_TYPES, $allowedTypes);
$this->isBreakingNodeFoundFirst = false;
$foundNode = $this->betterNodeFinder->findFirstPrevious($node, function (Node $node) use (

View File

@ -31,6 +31,6 @@ final class ScopeNestingComparator
private function findParentControlStructure(Node $node): ?Node
{
return $this->betterNodeFinder->findFirstParentInstanceOf($node, ControlStructure::NODE_TYPES);
return $this->betterNodeFinder->findFirstParentInstanceOf($node, ControlStructure::BREAKING_SCOPE_NODE_TYPES);
}
}

View File

@ -20,7 +20,7 @@ final class ControlStructure
/**
* @var class-string[]
*/
public const NODE_TYPES = [
public const BREAKING_SCOPE_NODE_TYPES = [
For_::class,
Foreach_::class,
If_::class,

View File

@ -11,11 +11,13 @@ use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassConst;
use Rector\Core\PhpParser\Node\Manipulator\ClassInsertManipulator;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\RectorDefinition\CodeSample;
use Rector\Core\RectorDefinition\RectorDefinition;
use Rector\Core\Util\StaticRectorStrings;
use Rector\NodeNestingScope\NodeFinder\ScopeAwareNodeFinder;
/**
* @see \Rector\SOLID\Tests\Rector\Class_\RepeatedLiteralToClassConstantRector\RepeatedLiteralToClassConstantRectorTest
@ -36,10 +38,15 @@ final class RepeatedLiteralToClassConstantRector extends AbstractRector
* @var ClassInsertManipulator
*/
private $classInsertManipulator;
/**
* @var ScopeAwareNodeFinder
*/
private $scopeAwareNodeFinder;
public function __construct(ClassInsertManipulator $classInsertManipulator)
public function __construct(ClassInsertManipulator $classInsertManipulator, ScopeAwareNodeFinder $scopeAwareNodeFinder)
{
$this->classInsertManipulator = $classInsertManipulator;
$this->scopeAwareNodeFinder = $scopeAwareNodeFinder;
}
public function getDefinition(): RectorDefinition
@ -188,7 +195,7 @@ PHP
}
// skip values in another constants
$parentConst = $this->betterNodeFinder->findFirstPreviousOfTypes($string, [Const_::class]);
$parentConst = $this->scopeAwareNodeFinder->findParentType($string, [ClassConst::class]);
if ($parentConst) {
return true;
}

View File

@ -0,0 +1,51 @@
<?php
namespace Rector\SOLID\Tests\Rector\Class_\RepeatedLiteralToClassConstantRector\Fixture;
final class WithNonLocalConstant
{
/**
* @var string
*/
private const REQUIRE = 'require';
private function replacePHPStanWithPHPStanSrc(array $json): array
{
// already replaced
if (! isset($json[self::REQUIRE]['phpstan/phpstan'])) {
}
$phpstanVersion = $json[self::REQUIRE]['phpstan/phpstan'];
unset($json[self::REQUIRE]['phpstan/phpstan']);
}
}
?>
-----
<?php
namespace Rector\SOLID\Tests\Rector\Class_\RepeatedLiteralToClassConstantRector\Fixture;
final class WithNonLocalConstant
{
/**
* @var string
*/
private const REQUIRE = 'require';
/**
* @var string
*/
private const PHPSTAN_PHPSTAN = 'phpstan/phpstan';
private function replacePHPStanWithPHPStanSrc(array $json): array
{
// already replaced
if (! isset($json[self::REQUIRE][self::PHPSTAN_PHPSTAN])) {
}
$phpstanVersion = $json[self::REQUIRE][self::PHPSTAN_PHPSTAN];
unset($json[self::REQUIRE][self::PHPSTAN_PHPSTAN]);
}
}
?>