diff --git a/packages/node-collector/src/NodeCollector/ParsedPropertyFetchNodeCollector.php b/packages/node-collector/src/NodeCollector/ParsedPropertyFetchNodeCollector.php index 5f8a0628c72..e0abf308aa3 100644 --- a/packages/node-collector/src/NodeCollector/ParsedPropertyFetchNodeCollector.php +++ b/packages/node-collector/src/NodeCollector/ParsedPropertyFetchNodeCollector.php @@ -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; } diff --git a/packages/node-name-resolver/src/NodeNameResolver.php b/packages/node-name-resolver/src/NodeNameResolver.php index dee37be9865..50b52acd3e3 100644 --- a/packages/node-name-resolver/src/NodeNameResolver.php +++ b/packages/node-name-resolver/src/NodeNameResolver.php @@ -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]); } /** diff --git a/packages/node-type-resolver/src/NodeTypeResolver.php b/packages/node-type-resolver/src/NodeTypeResolver.php index 1aa9dcefa9f..a6c59278e45 100644 --- a/packages/node-type-resolver/src/NodeTypeResolver.php +++ b/packages/node-type-resolver/src/NodeTypeResolver.php @@ -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); } diff --git a/packages/post-rector/src/Collector/NodesToAddCollector.php b/packages/post-rector/src/Collector/NodesToAddCollector.php index f1016d999e8..2436c716336 100644 --- a/packages/post-rector/src/Collector/NodesToAddCollector.php +++ b/packages/post-rector/src/Collector/NodesToAddCollector.php @@ -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) { diff --git a/packages/static-type-mapper/src/StaticTypeMapper.php b/packages/static-type-mapper/src/StaticTypeMapper.php index c4dbb0617ea..80c9e9f2797 100644 --- a/packages/static-type-mapper/src/StaticTypeMapper.php +++ b/packages/static-type-mapper/src/StaticTypeMapper.php @@ -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)); } diff --git a/rules/code-quality/src/Rector/For_/ForToForeachRector.php b/rules/code-quality/src/Rector/For_/ForToForeachRector.php index 0b98cf00555..7aaf90db630 100644 --- a/rules/code-quality/src/Rector/For_/ForToForeachRector.php +++ b/rules/code-quality/src/Rector/For_/ForToForeachRector.php @@ -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; diff --git a/rules/code-quality/src/Rector/Foreach_/SimplifyForeachToCoalescingRector.php b/rules/code-quality/src/Rector/Foreach_/SimplifyForeachToCoalescingRector.php index 2fec0b51ddc..163b316d51c 100644 --- a/rules/code-quality/src/Rector/Foreach_/SimplifyForeachToCoalescingRector.php +++ b/rules/code-quality/src/Rector/Foreach_/SimplifyForeachToCoalescingRector.php @@ -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; } diff --git a/rules/code-quality/src/Rector/FuncCall/SetTypeToCastRector.php b/rules/code-quality/src/Rector/FuncCall/SetTypeToCastRector.php index 43fe108a0a2..789d7e5d2e1 100644 --- a/rules/code-quality/src/Rector/FuncCall/SetTypeToCastRector.php +++ b/rules/code-quality/src/Rector/FuncCall/SetTypeToCastRector.php @@ -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; } diff --git a/rules/dead-code/src/NodeManipulator/LivingCodeManipulator.php b/rules/dead-code/src/NodeManipulator/LivingCodeManipulator.php index 88bdeeed72b..baedf4b056b 100644 --- a/rules/dead-code/src/NodeManipulator/LivingCodeManipulator.php +++ b/rules/dead-code/src/NodeManipulator/LivingCodeManipulator.php @@ -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) diff --git a/rules/dead-code/src/Rector/Assign/RemoveDoubleAssignRector.php b/rules/dead-code/src/Rector/Assign/RemoveDoubleAssignRector.php index 4dfbcf453c5..84175843130 100644 --- a/rules/dead-code/src/Rector/Assign/RemoveDoubleAssignRector.php +++ b/rules/dead-code/src/Rector/Assign/RemoveDoubleAssignRector.php @@ -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 diff --git a/rules/dead-code/src/Rector/ClassMethod/RemoveUnusedPrivateMethodRector.php b/rules/dead-code/src/Rector/ClassMethod/RemoveUnusedPrivateMethodRector.php index daeddc7fbcb..3291f30214e 100644 --- a/rules/dead-code/src/Rector/ClassMethod/RemoveUnusedPrivateMethodRector.php +++ b/rules/dead-code/src/Rector/ClassMethod/RemoveUnusedPrivateMethodRector.php @@ -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; } diff --git a/rules/dead-code/src/Rector/Plus/RemoveDeadZeroAndOneOperationRector.php b/rules/dead-code/src/Rector/Plus/RemoveDeadZeroAndOneOperationRector.php index 0d89c6b98d9..1b6044e12a0 100644 --- a/rules/dead-code/src/Rector/Plus/RemoveDeadZeroAndOneOperationRector.php +++ b/rules/dead-code/src/Rector/Plus/RemoveDeadZeroAndOneOperationRector.php @@ -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); diff --git a/rules/defluent/src/NodeAnalyzer/FluentChainMethodCallNodeAnalyzer.php b/rules/defluent/src/NodeAnalyzer/FluentChainMethodCallNodeAnalyzer.php index 2a42663ede7..e327c3f63c5 100644 --- a/rules/defluent/src/NodeAnalyzer/FluentChainMethodCallNodeAnalyzer.php +++ b/rules/defluent/src/NodeAnalyzer/FluentChainMethodCallNodeAnalyzer.php @@ -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]); } } diff --git a/rules/defluent/src/NodeAnalyzer/FluentChainMethodCallRootExtractor.php b/rules/defluent/src/NodeAnalyzer/FluentChainMethodCallRootExtractor.php index 643f8fd85bc..5106327f7c3 100644 --- a/rules/defluent/src/NodeAnalyzer/FluentChainMethodCallRootExtractor.php +++ b/rules/defluent/src/NodeAnalyzer/FluentChainMethodCallRootExtractor.php @@ -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_) { diff --git a/rules/downgrade-php80/src/Rector/ClassMethod/DowngradeTrailingCommasInParamUseRector.php b/rules/downgrade-php80/src/Rector/ClassMethod/DowngradeTrailingCommasInParamUseRector.php index 1bd2e660152..43d6e929f90 100644 --- a/rules/downgrade-php80/src/Rector/ClassMethod/DowngradeTrailingCommasInParamUseRector.php +++ b/rules/downgrade-php80/src/Rector/ClassMethod/DowngradeTrailingCommasInParamUseRector.php @@ -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); } diff --git a/rules/magic-disclosure/src/Rector/Assign/GetAndSetToMethodCallRector.php b/rules/magic-disclosure/src/Rector/Assign/GetAndSetToMethodCallRector.php index 4c70d4ec340..6b503ff1c78 100644 --- a/rules/magic-disclosure/src/Rector/Assign/GetAndSetToMethodCallRector.php +++ b/rules/magic-disclosure/src/Rector/Assign/GetAndSetToMethodCallRector.php @@ -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; diff --git a/rules/nette-code-quality/src/Rector/ArrayDimFetch/ChangeControlArrayAccessToAnnotatedControlVariableRector.php b/rules/nette-code-quality/src/Rector/ArrayDimFetch/ChangeControlArrayAccessToAnnotatedControlVariableRector.php index 0fcd1d8fe4b..a5844624164 100644 --- a/rules/nette-code-quality/src/Rector/ArrayDimFetch/ChangeControlArrayAccessToAnnotatedControlVariableRector.php +++ b/rules/nette-code-quality/src/Rector/ArrayDimFetch/ChangeControlArrayAccessToAnnotatedControlVariableRector.php @@ -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; } diff --git a/rules/nette-kdyby/src/Naming/VariableNaming.php b/rules/nette-kdyby/src/Naming/VariableNaming.php index 9d662fbf325..df1febc3697 100644 --- a/rules/nette-kdyby/src/Naming/VariableNaming.php +++ b/rules/nette-kdyby/src/Naming/VariableNaming.php @@ -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 diff --git a/rules/php56/src/Rector/FunctionLike/AddDefaultValueForUndefinedVariableRector.php b/rules/php56/src/Rector/FunctionLike/AddDefaultValueForUndefinedVariableRector.php index f5fc6b0e287..f1992ca6a69 100644 --- a/rules/php56/src/Rector/FunctionLike/AddDefaultValueForUndefinedVariableRector.php +++ b/rules/php56/src/Rector/FunctionLike/AddDefaultValueForUndefinedVariableRector.php @@ -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; } } diff --git a/src/PhpParser/Node/Manipulator/ClassMethodAssignManipulator.php b/src/PhpParser/Node/Manipulator/ClassMethodAssignManipulator.php index a915abe0e28..75dfb7f215f 100644 --- a/src/PhpParser/Node/Manipulator/ClassMethodAssignManipulator.php +++ b/src/PhpParser/Node/Manipulator/ClassMethodAssignManipulator.php @@ -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; } diff --git a/src/PhpParser/Node/Manipulator/ClassMethodPropertyFetchManipulator.php b/src/PhpParser/Node/Manipulator/ClassMethodPropertyFetchManipulator.php index 8118a3f50fa..fbdc312b78a 100644 --- a/src/PhpParser/Node/Manipulator/ClassMethodPropertyFetchManipulator.php +++ b/src/PhpParser/Node/Manipulator/ClassMethodPropertyFetchManipulator.php @@ -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; } diff --git a/src/PhpParser/Node/Manipulator/IdentifierManipulator.php b/src/PhpParser/Node/Manipulator/IdentifierManipulator.php index 0ee45464217..6ca3848a883 100644 --- a/src/PhpParser/Node/Manipulator/IdentifierManipulator.php +++ b/src/PhpParser/Node/Manipulator/IdentifierManipulator.php @@ -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; } } diff --git a/src/PhpParser/Node/Manipulator/NullsafeManipulator.php b/src/PhpParser/Node/Manipulator/NullsafeManipulator.php index 457b965e838..eabf9ba9700 100644 --- a/src/PhpParser/Node/Manipulator/NullsafeManipulator.php +++ b/src/PhpParser/Node/Manipulator/NullsafeManipulator.php @@ -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); } diff --git a/src/PhpParser/Parser/InlineCodeParser.php b/src/PhpParser/Parser/InlineCodeParser.php index e777981a81b..429dcec7489 100644 --- a/src/PhpParser/Parser/InlineCodeParser.php +++ b/src/PhpParser/Parser/InlineCodeParser.php @@ -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); } diff --git a/src/Util/StaticInstanceOf.php b/src/Util/StaticInstanceOf.php new file mode 100644 index 00000000000..5e0efcae936 --- /dev/null +++ b/src/Util/StaticInstanceOf.php @@ -0,0 +1,29 @@ +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]; + } +}