returnStrictTypeAnalyzer = $returnStrictTypeAnalyzer; } public function getRuleDefinition() : RuleDefinition { return new RuleDefinition('Cleanup unneeded nullsafe operator', [new CodeSample(<<<'CODE_SAMPLE' class HelloWorld { public function getString(): string { return 'hello world'; } } function get(): HelloWorld { return new HelloWorld(); } echo get()?->getString(); CODE_SAMPLE , <<<'CODE_SAMPLE' class HelloWorld { public function getString(): string { return 'hello world'; } } function get(): HelloWorld { return new HelloWorld(); } echo get()->getString(); CODE_SAMPLE )]); } /** * @return array> */ public function getNodeTypes() : array { return [NullsafeMethodCall::class]; } /** * @param NullsafeMethodCall $node */ public function refactor(Node $node) : ?Node { if (!$node->name instanceof Identifier) { return null; } if (!$node->var instanceof FuncCall && !$node->var instanceof MethodCall && !$node->var instanceof StaticCall) { return null; } $returnNode = $this->returnStrictTypeAnalyzer->resolveMethodCallReturnNode($node->var); if (!$returnNode instanceof Node) { return null; } $type = $this->getType($returnNode); if (!$type instanceof FullyQualifiedObjectType) { return null; } return new MethodCall($node->var, $node->name, $node->args); } public function provideMinPhpVersion() : int { return PhpVersionFeature::NULLSAFE_OPERATOR; } }