remove StaticNodeInstanceOf

This commit is contained in:
TomasVotruba 2021-04-15 20:55:45 +02:00
parent be2a54e0b9
commit 8976681b20
5 changed files with 18 additions and 79 deletions

View File

@ -15,7 +15,6 @@ use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Switch_;
use PhpParser\Node\Stmt\While_;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\Core\Util\StaticNodeInstanceOf;
final class ContextAnalyzer
{
@ -49,7 +48,13 @@ final class ContextAnalyzer
return false;
}
return StaticNodeInstanceOf::isOneOf($firstParent, self::LOOP_NODES);
foreach (self::LOOP_NODES as $type) {
if (is_a($node, $type, true)) {
return true;
}
}
return false;
}
public function isInIf(Node $node): bool

View File

@ -18,7 +18,6 @@ use PhpParser\NodeTraverser;
use Rector\Core\NodeAnalyzer\PropertyFetchAnalyzer;
use Rector\Core\NodeManipulator\ClassManipulator;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Util\StaticNodeInstanceOf;
use Rector\Core\ValueObject\MethodName;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Privatization\NodeReplacer\PropertyFetchWithVariableReplacer;
@ -221,7 +220,13 @@ CODE_SAMPLE
private function isScopeChangingNode(Node $node): bool
{
return StaticNodeInstanceOf::isOneOf($node, self::SCOPE_CHANGING_NODE_TYPES);
foreach (self::SCOPE_CHANGING_NODE_TYPES as $type) {
if (is_a($node, $type, true)) {
return true;
}
}
return false;
}
private function refactorIf(If_ $if, string $privatePropertyName): ?bool

View File

@ -16,7 +16,6 @@ use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Return_;
use PhpParser\NodeFinder;
use Rector\Core\PhpParser\Comparing\NodeComparator;
use Rector\Core\Util\StaticNodeInstanceOf;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\PackageBuilder\Php\TypeChecker;
@ -101,8 +100,10 @@ final class BetterNodeFinder
}
do {
if (StaticNodeInstanceOf::isOneOf($parent, $types)) {
return $parent;
foreach ($types as $type) {
if (is_a($node, $type, true)) {
return $parent;
}
}
if ($parent === null) {

View File

@ -1,38 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\Core\Util;
use PhpParser\Node;
use Rector\Core\Exception\ShouldNotHappenException;
/**
* @see \Rector\Core\Tests\Util\StaticNodeInstanceOfTest
*/
final class StaticNodeInstanceOf
{
/**
* @param string|object|null $element
* @param array<class-string<Node>> $nodeTypes
*/
public static function isOneOf($element, array $nodeTypes): bool
{
if ($element === null) {
return false;
}
// at least 2 types; use instanceof otherwise
if (count($nodeTypes) < 2) {
throw new ShouldNotHappenException();
}
foreach ($nodeTypes as $nodeType) {
if (is_a($element, $nodeType, true)) {
return true;
}
}
return false;
}
}

View File

@ -1,34 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\Core\Tests\Util;
use DateTime;
use Iterator;
use PhpParser\Node;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Nop;
use PHPUnit\Framework\TestCase;
use Rector\Core\Util\StaticNodeInstanceOf;
use stdClass;
final class StaticNodeInstanceOfTest extends TestCase
{
/**
* @dataProvider provideIsOneOf()
* @param array<class-string<Node>> $array
* @param DateTime|stdClass|null $object
*/
public function testIsOneOf(?object $object, array $array, bool $expected): void
{
$this->assertSame($expected, StaticNodeInstanceOf::isOneOf($object, $array));
}
public function provideIsOneOf(): Iterator
{
yield [new String_('hey'), [LNumber::class, String_::class], true];
yield [null, [Nop::class], false];
}
}