classMethodReturnTypeOverrideGuard = $classMethodReturnTypeOverrideGuard; $this->returnTypeInferer = $returnTypeInferer; } public function getRuleDefinition() : RuleDefinition { return new RuleDefinition('Add return type from return direct array', [new CodeSample(<<<'CODE_SAMPLE' final class AddReturnArray { public function getArray() { return [1, 2, 3]; } } CODE_SAMPLE , <<<'CODE_SAMPLE' final class AddReturnArray { public function getArray(): array { return [1, 2, 3]; } } CODE_SAMPLE )]); } /** * @return array> */ public function getNodeTypes() : array { return [ClassMethod::class, Function_::class, Closure::class, ArrowFunction::class]; } /** * @param ClassMethod|Function_|ArrowFunction $node */ public function refactorWithScope(Node $node, Scope $scope) : ?Node { if ($node->returnType !== null) { return null; } if ($node instanceof ClassMethod && $this->classMethodReturnTypeOverrideGuard->shouldSkipClassMethod($node, $scope)) { return null; } if (!$this->hasReturnArray($node)) { return null; } $type = $this->returnTypeInferer->inferFunctionLike($node); if (!$type->isArray()->yes()) { return null; } $node->returnType = new Identifier('array'); return $node; } public function provideMinPhpVersion() : int { return PhpVersionFeature::SCALAR_TYPES; } /** * @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_|\PhpParser\Node\Expr\Closure|\PhpParser\Node\Expr\ArrowFunction $functionLike */ private function hasReturnArray($functionLike) : bool { $stmts = $functionLike instanceof ArrowFunction ? $functionLike->getStmts() : $functionLike->stmts; if (!\is_array($stmts)) { return \false; } foreach ($stmts as $stmt) { if (!$stmt instanceof Return_) { continue; } if (!$stmt->expr instanceof Array_) { continue; } return \true; } return \false; } }