[Util] Add StaticInstanceOf (#4979)

Co-authored-by: rector-bot <tomas@getrector.org>
This commit is contained in:
Abdul Malik Ikhsan 2020-12-25 18:53:26 +07:00 committed by GitHub
parent 61b72a8d7c
commit ec5daad132
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
26 changed files with 147 additions and 143 deletions

View File

@ -12,6 +12,7 @@ use PhpParser\Node\Expr\StaticPropertyFetch;
use PHPStan\Type\Type;
use PHPStan\Type\TypeWithClassName;
use PHPStan\Type\UnionType;
use Rector\Core\Util\StaticInstanceOf;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\NodeTypeResolver;
@ -57,10 +58,7 @@ final class ParsedPropertyFetchNodeCollector
$propertyType = $this->resolvePropertyCallerType($node);
// make sure name is valid
if ($node->name instanceof StaticCall) {
return;
}
if ($node->name instanceof MethodCall) {
if (StaticInstanceOf::isOneOf($node->name, [StaticCall::class, MethodCall::class])) {
return;
}

View File

@ -17,6 +17,7 @@ use PhpParser\Node\Name;
use Rector\Core\Contract\Rector\RectorInterface;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\PhpParser\Printer\BetterStandardPrinter;
use Rector\Core\Util\StaticInstanceOf;
use Rector\NodeNameResolver\Contract\NodeNameResolverInterface;
use Rector\NodeNameResolver\Regex\RegexPatternDetector;
use Rector\NodeTypeResolver\FileSystem\CurrentFileInfoProvider;
@ -219,14 +220,7 @@ final class NodeNameResolver
private function isCallOrIdentifier(Node $node): bool
{
if ($node instanceof MethodCall) {
return true;
}
if ($node instanceof StaticCall) {
return true;
}
return $node instanceof Identifier;
return StaticInstanceOf::isOneOf($node, [MethodCall::class, StaticCall::class, Identifier::class]);
}
/**

View File

@ -31,6 +31,7 @@ use PHPStan\Type\TypeWithClassName;
use PHPStan\Type\UnionType;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\NodeAnalyzer\ClassNodeAnalyzer;
use Rector\Core\Util\StaticInstanceOf;
use Rector\NodeTypeResolver\Contract\NodeTypeResolverInterface;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\NodeTypeCorrector\ParentClassLikeTypeCorrector;
@ -169,10 +170,8 @@ final class NodeTypeResolver
if ($node instanceof Arg) {
$node = $node->value;
}
if ($node instanceof Param) {
return $this->resolve($node);
}
if ($node instanceof Scalar) {
if (StaticInstanceOf::isOneOf($node, [Param::class, Scalar::class])) {
return $this->resolve($node);
}

View File

@ -10,6 +10,7 @@ use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Expression;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\Core\Util\StaticInstanceOf;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PostRector\Contract\Collector\NodeCollectorInterface;
@ -102,12 +103,10 @@ final class NodesToAddCollector implements NodeCollectorInterface
private function resolveNearestExpressionPosition(Node $node): string
{
if ($node instanceof Expression) {
return spl_object_hash($node);
}
if ($node instanceof Stmt) {
if (StaticInstanceOf::isOneOf($node, [Expression::class, Stmt::class])) {
return spl_object_hash($node);
}
/** @var Expression|null $foundNode */
$foundNode = $this->betterNodeFinder->findFirstAncestorInstanceOf($node, Expression::class);
if ($foundNode === null) {

View File

@ -16,6 +16,7 @@ use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\Type;
use Rector\Core\Exception\NotImplementedException;
use Rector\Core\Util\StaticInstanceOf;
use Rector\PHPStanStaticTypeMapper\PHPStanStaticTypeMapper;
use Rector\StaticTypeMapper\Mapper\PhpParserNodeMapper;
use Rector\StaticTypeMapper\Naming\NameScopeFactory;
@ -84,18 +85,13 @@ final class StaticTypeMapper
public function mapPHPStanPhpDocTypeToPHPStanType(PhpDocTagValueNode $phpDocTagValueNode, Node $node): Type
{
if ($phpDocTagValueNode instanceof ReturnTagValueNode) {
return $this->mapPHPStanPhpDocTypeNodeToPHPStanType($phpDocTagValueNode->type, $node);
}
if ($phpDocTagValueNode instanceof ParamTagValueNode) {
return $this->mapPHPStanPhpDocTypeNodeToPHPStanType($phpDocTagValueNode->type, $node);
}
if ($phpDocTagValueNode instanceof VarTagValueNode) {
return $this->mapPHPStanPhpDocTypeNodeToPHPStanType($phpDocTagValueNode->type, $node);
}
if ($phpDocTagValueNode instanceof ThrowsTagValueNode) {
if (StaticInstanceOf::isOneOf(
$phpDocTagValueNode,
[ReturnTagValueNode::class, ParamTagValueNode::class, VarTagValueNode::class, ThrowsTagValueNode::class]
)) {
return $this->mapPHPStanPhpDocTypeNodeToPHPStanType($phpDocTagValueNode->type, $node);
}
throw new NotImplementedException(__METHOD__ . ' for ' . get_class($phpDocTagValueNode));
}

View File

@ -24,6 +24,7 @@ use PhpParser\Node\Stmt\Unset_;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\PhpParser\Node\Manipulator\AssignManipulator;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Util\StaticInstanceOf;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@ -253,11 +254,11 @@ CODE_SAMPLE
if ($this->keyValueName === null) {
return false;
}
if ($loopExprs[0] instanceof PreInc) {
return $this->isName($loopExprs[0]->var, $this->keyValueName);
}
if ($loopExprs[0] instanceof PostInc) {
return $this->isName($loopExprs[0]->var, $this->keyValueName);
/** @var PreInc|PostInc $prePostInc */
$prePostInc = $loopExprs[0];
if (StaticInstanceOf::isOneOf($prePostInc, [PreInc::class, PostInc::class])) {
return $this->isName($prePostInc->var, $this->keyValueName);
}
return false;

View File

@ -15,6 +15,7 @@ use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Return_;
use Rector\Core\PhpParser\Node\Manipulator\ForeachManipulator;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Util\StaticInstanceOf;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
@ -127,10 +128,7 @@ CODE_SAMPLE
}
$innerNode = $node->stmts[0] instanceof Expression ? $node->stmts[0]->expr : $node->stmts[0];
if ($innerNode instanceof Assign) {
return $innerNode;
}
if ($innerNode instanceof Return_) {
if (StaticInstanceOf::isOneOf($innerNode, [Assign::class, Return_::class])) {
return $innerNode;
}

View File

@ -17,6 +17,7 @@ use PhpParser\Node\Expr\Cast\String_;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Stmt\Expression;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Util\StaticInstanceOf;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@ -100,11 +101,9 @@ CODE_SAMPLE
$varNode = $node->args[0]->value;
$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);
// result of function or probably used
if ($parentNode instanceof Expr) {
return null;
}
if ($parentNode instanceof Arg) {
if (StaticInstanceOf::isOneOf($parentNode, [Expr::class, Arg::class])) {
return null;
}

View File

@ -30,7 +30,9 @@ use PhpParser\Node\Expr\UnaryPlus;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Scalar;
use PhpParser\Node\Stmt\Expression;
use Rector\Core\Util\StaticInstanceOf;
use Rector\PostRector\Collector\NodesToAddCollector;
use Webmozart\Assert\Assert;
final class LivingCodeManipulator
{
@ -61,13 +63,8 @@ final class LivingCodeManipulator
if (! $expr instanceof Expr) {
return [];
}
if ($expr instanceof Closure) {
return [];
}
if ($expr instanceof Scalar) {
return [];
}
if ($expr instanceof ConstFetch) {
if (StaticInstanceOf::isOneOf($expr, [Closure::class, Scalar::class, ConstFetch::class])) {
return [];
}
@ -92,13 +89,8 @@ final class LivingCodeManipulator
$this->keepLivingCodeFromExpr($expr->dim)
);
}
if ($expr instanceof ClassConstFetch) {
return array_merge(
$this->keepLivingCodeFromExpr($expr->class),
$this->keepLivingCodeFromExpr($expr->name)
);
}
if ($expr instanceof StaticPropertyFetch) {
if (StaticInstanceOf::isOneOf($expr, [ClassConstFetch::class, StaticPropertyFetch::class])) {
/** @var ClassConstFetch|StaticPropertyFetch $expr */
return array_merge(
$this->keepLivingCodeFromExpr($expr->class),
$this->keepLivingCodeFromExpr($expr->name)

View File

@ -15,6 +15,7 @@ use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Expression;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Util\StaticInstanceOf;
use Rector\NodeNestingScope\ScopeNestingComparator;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
@ -99,15 +100,7 @@ CODE_SAMPLE
private function isCall(Expr $expr): bool
{
if ($expr instanceof FuncCall) {
return true;
}
if ($expr instanceof StaticCall) {
return true;
}
return $expr instanceof MethodCall;
return StaticInstanceOf::isOneOf($expr, [FuncCall::class, StaticCall::class, MethodCall::class]);
}
private function shouldSkipForDifferentScope(Assign $assign, Expression $expression): bool

View File

@ -10,6 +10,7 @@ use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Interface_;
use PhpParser\Node\Stmt\Trait_;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Util\StaticInstanceOf;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@ -85,11 +86,9 @@ CODE_SAMPLE
if ($classLike === null) {
return true;
}
// unreliable to detect trait, interface doesn't make sense
if ($classLike instanceof Trait_) {
return true;
}
if ($classLike instanceof Interface_) {
if (StaticInstanceOf::isOneOf($classLike, [Trait_::class, Interface_::class])) {
return true;
}

View File

@ -18,8 +18,10 @@ use PhpParser\Node\Expr\BinaryOp\Mul;
use PhpParser\Node\Expr\BinaryOp\Plus;
use PhpParser\Node\Expr\UnaryMinus;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Util\StaticInstanceOf;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Webmozart\Assert\Assert;
/**
* @see https://3v4l.org/I0BGs
@ -139,12 +141,11 @@ CODE_SAMPLE
private function processBinaryOp(Node $node): ?Expr
{
if ($node instanceof Plus) {
return $this->processBinaryPlusAndMinus($node);
}
if ($node instanceof Minus) {
if (StaticInstanceOf::isOneOf($node, [Plus::class, Minus::class])) {
/** @var Plus|Minus $node */
return $this->processBinaryPlusAndMinus($node);
}
// *, /
if ($node instanceof Mul) {
return $this->processBinaryMulAndDiv($node);

View File

@ -12,6 +12,7 @@ use PhpParser\Node\Expr\StaticCall;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeWithClassName;
use Rector\Core\Util\StaticInstanceOf;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\NodeTypeResolver;
@ -220,10 +221,6 @@ final class FluentChainMethodCallNodeAnalyzer
private function isCall(Expr $expr): bool
{
if ($expr instanceof MethodCall) {
return true;
}
return $expr instanceof StaticCall;
return StaticInstanceOf::isOneOf($expr, [MethodCall::class, StaticCall::class]);
}
}

View File

@ -13,6 +13,7 @@ use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Return_;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\Core\Util\StaticInstanceOf;
use Rector\Defluent\ValueObject\AssignAndRootExpr;
use Rector\Defluent\ValueObject\FluentCallsKind;
use Rector\Naming\Naming\PropertyNaming;
@ -84,10 +85,7 @@ final class FluentChainMethodCallRootExtractor
}
foreach ($methodCalls as $methodCall) {
if ($methodCall->var instanceof Variable) {
return $this->createAssignAndRootExprForVariableOrPropertyFetch($methodCall);
}
if ($methodCall->var instanceof PropertyFetch) {
if (StaticInstanceOf::isOneOf($methodCall->var, [Variable::class, PropertyFetch::class])) {
return $this->createAssignAndRootExprForVariableOrPropertyFetch($methodCall);
}
if ($methodCall->var instanceof New_) {

View File

@ -16,9 +16,11 @@ use PhpParser\Node\Param;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Util\StaticInstanceOf;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Webmozart\Assert\Assert;
/**
* @see \Rector\DowngradePhp80\Tests\Rector\ClassMethod\DowngradeTrailingCommasInParamUseRector\DowngradeTrailingCommasInParamUseRectorTest
@ -90,22 +92,16 @@ CODE_SAMPLE
*/
public function refactor(Node $node): ?Node
{
if ($node instanceof MethodCall) {
return $this->processArgs($node);
}
if ($node instanceof FuncCall) {
return $this->processArgs($node);
}
if ($node instanceof StaticCall) {
return $this->processArgs($node);
}
if ($node instanceof New_) {
if (StaticInstanceOf::isOneOf($node, [MethodCall::class, FuncCall::class, StaticCall::class, New_::class])) {
/** @var MethodCall|FuncCall|StaticCall|New_ $node */
return $this->processArgs($node);
}
if ($node instanceof Closure) {
$node = $this->processUses($node);
}
/** @var ClassMethod|Function_ $node */
return $this->processParams($node);
}

View File

@ -15,6 +15,7 @@ use PHPStan\Type\ObjectType;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\PhpParser\Node\Manipulator\PropertyFetchManipulator;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Util\StaticInstanceOf;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@ -102,10 +103,7 @@ CODE_SAMPLE
public function refactor(Node $node): ?Node
{
if ($node instanceof Assign) {
if ($node->var instanceof PropertyFetch) {
return $this->processMagicSet($node);
}
if ($node->var instanceof StaticPropertyFetch) {
if (StaticInstanceOf::isOneOf($node->var, [PropertyFetch::class, StaticPropertyFetch::class])) {
return $this->processMagicSet($node);
}
return null;

View File

@ -14,6 +14,7 @@ use PhpParser\Node\Stmt\Unset_;
use PHPStan\Type\ObjectType;
use Rector\Core\Exception\NotImplementedYetException;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\Util\StaticInstanceOf;
use Rector\Naming\ArrayDimFetchRenamer;
use Rector\NetteCodeQuality\NodeResolver\MethodNamesByInputNamesResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
@ -141,10 +142,7 @@ CODE_SAMPLE
}
$parent = $arrayDimFetch->getAttribute(AttributeKey::PARENT_NODE);
if ($parent instanceof Isset_) {
return ! $arrayDimFetch->dim instanceof Variable;
}
if ($parent instanceof Unset_) {
if (StaticInstanceOf::isOneOf($parent, [Isset_::class, Unset_::class])) {
return ! $arrayDimFetch->dim instanceof Variable;
}

View File

@ -26,6 +26,7 @@ use Rector\CodingStyle\Naming\ClassNaming;
use Rector\Core\Exception\NotImplementedException;
use Rector\Core\Exception\NotImplementedYetException;
use Rector\Core\PhpParser\Node\Value\ValueResolver;
use Rector\Core\Util\StaticInstanceOf;
use Rector\Core\Util\StaticRectorStrings;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\NodeTypeResolver;
@ -246,15 +247,11 @@ final class VariableNaming
private function isCall(?Node $node): bool
{
if ($node instanceof MethodCall) {
return true;
if ($node === null) {
return false;
}
if ($node instanceof NullsafeMethodCall) {
return true;
}
return $node instanceof StaticCall;
return StaticInstanceOf::isOneOf($node, [MethodCall::class, NullsafeMethodCall::class, StaticCall::class]);
}
private function resolveFromMethodCall(?Node $node): ?string

View File

@ -25,6 +25,7 @@ use PhpParser\Node\Stmt\Unset_;
use PhpParser\NodeTraverser;
use PHPStan\Analyser\Scope;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Util\StaticInstanceOf;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@ -186,10 +187,7 @@ CODE_SAMPLE
)) {
return true;
}
if ($parentNode instanceof Unset_) {
return true;
}
if ($parentNode instanceof UnsetCast) {
if (StaticInstanceOf::isOneOf($parentNode, [Unset_::class, UnsetCast::class])) {
return true;
}
@ -231,10 +229,7 @@ CODE_SAMPLE
{
if ($parentNode instanceof Node) {
$parentParentNode = $parentNode->getAttribute(AttributeKey::PARENT_NODE);
if ($parentParentNode instanceof List_) {
return true;
}
if ($parentParentNode instanceof Array_) {
if (StaticInstanceOf::isOneOf($parentParentNode, [List_::class, Array_::class])) {
return true;
}
}

View File

@ -27,6 +27,7 @@ use Rector\Core\PhpParser\Node\NodeFactory;
use Rector\Core\PhpParser\NodeTraverser\CallableNodeTraverser;
use Rector\Core\PhpParser\Printer\BetterStandardPrinter;
use Rector\Core\PHPStan\Reflection\CallReflectionResolver;
use Rector\Core\Util\StaticInstanceOf;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
@ -289,15 +290,14 @@ final class ClassMethodAssignManipulator
private function isExplicitlyReferenced(Node $node): bool
{
if ($node instanceof Arg) {
return $node->byRef;
}
if ($node instanceof ClosureUse) {
return $node->byRef;
}
if ($node instanceof Param) {
if (! property_exists($node, 'byRef')) {
return false;
}
if (StaticInstanceOf::isOneOf($node, [Arg::class, ClosureUse::class, Param::class])) {
return $node->byRef;
}
return false;
}

View File

@ -12,6 +12,7 @@ use PhpParser\Node\Param;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\NodeTraverser;
use Rector\Core\PhpParser\NodeTraverser\CallableNodeTraverser;
use Rector\Core\Util\StaticInstanceOf;
use Rector\NodeNameResolver\NodeNameResolver;
final class ClassMethodPropertyFetchManipulator
@ -56,10 +57,8 @@ final class ClassMethodPropertyFetchManipulator
if (! $this->nodeNameResolver->isName($node->var, $propertyName)) {
return null;
}
if ($node->expr instanceof MethodCall) {
return null;
}
if ($node->expr instanceof StaticCall) {
if (StaticInstanceOf::isOneOf($node->expr, [MethodCall::class, StaticCall::class])) {
return null;
}

View File

@ -13,6 +13,7 @@ use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\Core\Exception\NodeChanger\NodeMissingIdentifierException;
use Rector\Core\Util\StaticInstanceOf;
use Rector\NodeNameResolver\NodeNameResolver;
/**
@ -88,12 +89,14 @@ final class IdentifierManipulator
private function resolveOldMethodName(Node $node): ?string
{
if ($node instanceof StaticCall) {
if (! property_exists($node, 'name')) {
return $this->nodeNameResolver->getName($node);
}
if (StaticInstanceOf::isOneOf($node, [StaticCall::class, MethodCall::class])) {
return $this->nodeNameResolver->getName($node->name);
}
if ($node instanceof MethodCall) {
return $this->nodeNameResolver->getName($node->name);
}
return $this->nodeNameResolver->getName($node);
return null;
}
}

View File

@ -10,6 +10,7 @@ use PhpParser\Node\Expr\NullsafeMethodCall;
use PhpParser\Node\Expr\NullsafePropertyFetch;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Identifier;
use Rector\Core\Util\StaticInstanceOf;
use Rector\NodeTypeResolver\Node\AttributeKey;
final class NullsafeManipulator
@ -34,10 +35,8 @@ final class NullsafeManipulator
}
$parentIdentifier = $nextExprIdentifier->getAttribute(AttributeKey::PARENT_NODE);
if ($parentIdentifier instanceof MethodCall) {
return new NullsafeMethodCall($expr, $nextExprIdentifier);
}
if ($parentIdentifier instanceof NullsafeMethodCall) {
if (StaticInstanceOf::isOneOf($parentIdentifier, [MethodCall::class, NullsafeMethodCall::class])) {
return new NullsafeMethodCall($expr, $nextExprIdentifier);
}

View File

@ -16,6 +16,7 @@ use PhpParser\Node\Stmt;
use PhpParser\Parser;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\PhpParser\Printer\BetterStandardPrinter;
use Rector\Core\Util\StaticInstanceOf;
use Rector\NodeTypeResolver\NodeScopeAndMetadataDecorator;
final class InlineCodeParser
@ -96,13 +97,8 @@ final class InlineCodeParser
if ($content instanceof Concat) {
return $this->stringify($content->left) . $this->stringify($content->right);
}
if ($content instanceof Variable) {
return $this->betterStandardPrinter->print($content);
}
if ($content instanceof PropertyFetch) {
return $this->betterStandardPrinter->print($content);
}
if ($content instanceof StaticPropertyFetch) {
if (StaticInstanceOf::isOneOf($content, [Variable::class, PropertyFetch::class, StaticPropertyFetch::class])) {
return $this->betterStandardPrinter->print($content);
}

View File

@ -0,0 +1,29 @@
<?php
declare(strict_types=1);
namespace Rector\Core\Util;
/**
* @see \Rector\Core\Tests\Util\StaticInstanceOfTest
*/
final class StaticInstanceOf
{
/**
* @param class-string[] $array
*/
public static function isOneOf(?object $object, array $array): bool
{
if ($object === null) {
return false;
}
foreach ($array as $classLike) {
if (is_a($object, $classLike, true)) {
return true;
}
}
return false;
}
}

View File

@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
namespace Rector\Core\Tests\Util;
use DateTime;
use Iterator;
use PHPUnit\Framework\TestCase;
use Rector\Core\Util\StaticInstanceOf;
use stdClass;
final class StaticInstanceOfTest extends TestCase
{
/**
* @dataProvider provideIsOneOf()
* @param class-string[] $array
*/
public function testIsOneOf(?object $object, array $array, bool $expected): void
{
$this->assertSame($expected, StaticInstanceOf::isOneOf($object, $array));
}
public function provideIsOneOf(): Iterator
{
yield [new DateTime('now'), [DateTime::class, stdClass::class], true];
yield [new stdClass(), [DateTime::class, Iterator::class], false];
yield [null, [DateTime::class, Iterator::class], false];
}
}