staticTypeMapper = $staticTypeMapper; } public function getRuleDefinition() : RuleDefinition { return new RuleDefinition('Change empty() on nullable object to instanceof check', [new CodeSample(<<<'CODE_SAMPLE' class SomeClass { public function run(?AnotherObject $anotherObject) { if (empty($anotherObject)) { return false; } return true; } } CODE_SAMPLE , <<<'CODE_SAMPLE' class SomeClass { public function run(?AnotherObject $anotherObject) { if (! $anotherObject instanceof AnotherObject) { return false; } return true; } } CODE_SAMPLE )]); } /** * @return array> */ public function getNodeTypes() : array { return [Empty_::class, BooleanNot::class]; } /** * @param Empty_|BooleanNot $node * @return null|\PhpParser\Node\Expr\Instanceof_|\PhpParser\Node\Expr\BooleanNot */ public function refactorWithScope(Node $node, Scope $scope) { if ($node instanceof BooleanNot) { if (!$node->expr instanceof Empty_) { return null; } $isNegated = \true; $empty = $node->expr; } else { $empty = $node; $isNegated = \false; } if ($empty->expr instanceof ArrayDimFetch) { return null; } $exprType = $scope->getNativeType($empty->expr); if (!$exprType instanceof UnionType) { return null; } $exprType = TypeCombinator::removeNull($exprType); if (!$exprType instanceof ObjectType) { return null; } $objectType = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($exprType, TypeKind::RETURN); if (!$objectType instanceof Name) { return null; } $instanceof = new Instanceof_($empty->expr, $objectType); if ($isNegated) { return $instanceof; } return new BooleanNot($instanceof); } }