binaryOpManipulator = $binaryOpManipulator; $this->valueResolver = $valueResolver; } public function getRuleDefinition() : RuleDefinition { return new RuleDefinition('Changes comparison with get_class to instanceof', [new CodeSample('if (EventsListener::class === get_class($event->job)) { }', 'if ($event->job instanceof EventsListener) { }')]); } /** * @return array> */ public function getNodeTypes() : array { return [Identical::class, NotIdentical::class]; } /** * @param Identical|NotIdentical $node */ public function refactor(Node $node) : ?Node { $twoNodeMatch = $this->binaryOpManipulator->matchFirstAndSecondConditionNode($node, function (Node $node) : bool { return $this->isClassReference($node); }, function (Node $node) : bool { return $this->isGetClassFuncCallNode($node); }); if (!$twoNodeMatch instanceof TwoNodeMatch) { return null; } /** @var ClassConstFetch|String_ $firstExpr */ $firstExpr = $twoNodeMatch->getFirstExpr(); /** @var FuncCall $secondExpr */ $secondExpr = $twoNodeMatch->getSecondExpr(); if ($secondExpr->isFirstClassCallable()) { return null; } if (!isset($secondExpr->getArgs()[0])) { return null; } $firstArg = $secondExpr->getArgs()[0]; $varNode = $firstArg->value; if ($firstExpr instanceof String_) { $className = $this->valueResolver->getValue($firstExpr); } else { $className = $this->getName($firstExpr->class); } if ($className === null) { return null; } if ($className === ObjectReference::PARENT) { return null; } $class = \in_array($className, self::NO_NAMESPACED_CLASSNAMES, \true) ? new Name($className) : new FullyQualified($className); $instanceof = new Instanceof_($varNode, $class); if ($node instanceof NotIdentical) { return new BooleanNot($instanceof); } return $instanceof; } private function isClassReference(Node $node) : bool { if (!$node instanceof ClassConstFetch) { // might be return $node instanceof String_; } return $this->isName($node->name, 'class'); } private function isGetClassFuncCallNode(Node $node) : bool { if (!$node instanceof FuncCall) { return \false; } return $this->isName($node, 'get_class'); } }