nullableTypeAnalyzer = $nullableTypeAnalyzer; $this->valueResolver = $valueResolver; } public function getRuleDefinition() : RuleDefinition { return new RuleDefinition('Flip type control from null compare to use exclusive instanceof object', [new CodeSample(<<<'CODE_SAMPLE' function process(?DateTime $dateTime) { if ($dateTime === null) { return; } } CODE_SAMPLE , <<<'CODE_SAMPLE' function process(?DateTime $dateTime) { if (! $dateTime instanceof DateTime) { return; } } CODE_SAMPLE )]); } /** * @return array> */ public function getNodeTypes() : array { return [Identical::class, NotIdentical::class]; } /** * @param Identical|NotIdentical $node */ public function refactor(Node $node) : ?Node { $expr = $this->matchNullComparedExpr($node); if (!$expr instanceof Expr) { return null; } $nullableObjectType = $this->nullableTypeAnalyzer->resolveNullableObjectType($expr); if (!$nullableObjectType instanceof ObjectType) { return null; } return $this->processConvertToExclusiveType($nullableObjectType, $expr, $node); } /** * @param \PhpParser\Node\Expr\BinaryOp\Identical|\PhpParser\Node\Expr\BinaryOp\NotIdentical $binaryOp * @return \PhpParser\Node\Expr\BooleanNot|\PhpParser\Node\Expr\Instanceof_ */ private function processConvertToExclusiveType(ObjectType $objectType, Expr $expr, $binaryOp) { $fullyQualifiedType = $objectType instanceof ShortenedObjectType ? $objectType->getFullyQualifiedName() : $objectType->getClassName(); $instanceof = new Instanceof_($expr, new FullyQualified($fullyQualifiedType)); if ($binaryOp instanceof NotIdentical) { return $instanceof; } return new BooleanNot($instanceof); } /** * @param \PhpParser\Node\Expr\BinaryOp\Identical|\PhpParser\Node\Expr\BinaryOp\NotIdentical $binaryOp */ private function matchNullComparedExpr($binaryOp) : ?Expr { if ($this->valueResolver->isNull($binaryOp->left)) { return $binaryOp->right; } if ($this->valueResolver->isNull($binaryOp->right)) { return $binaryOp->left; } return null; } }