strictReturnClassConstReturnTypeAnalyzer = $strictReturnClassConstReturnTypeAnalyzer; $this->classMethodReturnTypeOverrideGuard = $classMethodReturnTypeOverrideGuard; $this->staticTypeMapper = $staticTypeMapper; } public function getRuleDefinition() : RuleDefinition { return new RuleDefinition('Add strict type declaration based on returned constants', [new CodeSample(<<<'CODE_SAMPLE' class SomeClass { public const NAME = 'name'; public function run() { return self::NAME; } } CODE_SAMPLE , <<<'CODE_SAMPLE' class SomeClass { public const NAME = 'name'; public function run(): string { return self::NAME; } } CODE_SAMPLE )]); } /** * @return array> */ public function getNodeTypes() : array { return [ClassMethod::class]; } /** * @param ClassMethod $node */ public function refactorWithScope(Node $node, Scope $scope) : ?Node { if ($node->returnType instanceof Node) { return null; } if ($this->classMethodReturnTypeOverrideGuard->shouldSkipClassMethod($node, $scope)) { return null; } $matchedType = $this->strictReturnClassConstReturnTypeAnalyzer->matchAlwaysReturnConstFetch($node); if (!$matchedType instanceof Type) { return null; } $returnTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($matchedType, TypeKind::RETURN); if (!$returnTypeNode instanceof Node) { return null; } $node->returnType = $returnTypeNode; return $node; } /** * @return PhpVersion::* */ public function provideMinPhpVersion() : int { return PhpVersion::PHP_70; } }