callerParamMatcher = $callerParamMatcher; $this->parentClassMethodTypeOverrideGuard = $parentClassMethodTypeOverrideGuard; $this->paramTypeAddGuard = $paramTypeAddGuard; $this->betterNodeFinder = $betterNodeFinder; } public function getRuleDefinition() : RuleDefinition { return new RuleDefinition('Change param type based on passed method call type', [new CodeSample(<<<'CODE_SAMPLE' class SomeTypedService { public function run(string $name) { } } final class UseDependency { public function __construct( private SomeTypedService $someTypedService ) { } public function go($value) { $this->someTypedService->run($value); } } CODE_SAMPLE , <<<'CODE_SAMPLE' class SomeTypedService { public function run(string $name) { } } final class UseDependency { public function __construct( private SomeTypedService $someTypedService ) { } public function go(string $value) { $this->someTypedService->run($value); } } CODE_SAMPLE )]); } /** * @return array> */ public function getNodeTypes() : array { return [Class_::class]; } /** * @param Class_ $node */ public function refactorWithScope(Node $node, Scope $scope) : ?Node { $hasChanged = \false; foreach ($node->getMethods() as $classMethod) { if ($this->shouldSkipClassMethod($classMethod)) { continue; } /** @var array $callers */ $callers = $this->betterNodeFinder->findInstancesOf($classMethod, [StaticCall::class, MethodCall::class, FuncCall::class]); $hasClassMethodChanged = $this->refactorClassMethod($classMethod, $callers, $scope); if ($hasClassMethodChanged) { $hasChanged = \true; } } if ($hasChanged) { return $node; } return null; } private function shouldSkipClassMethod(ClassMethod $classMethod) : bool { if ($classMethod->params === []) { return \true; } return $this->parentClassMethodTypeOverrideGuard->hasParentClassMethod($classMethod); } /** * @param \PhpParser\Node\Identifier|\PhpParser\Node\Name|\PhpParser\Node\NullableType|\PhpParser\Node\UnionType|\PhpParser\Node\ComplexType $paramType */ private function mirrorParamType(Param $decoratedParam, $paramType) : void { // mimic type $newParamType = $paramType; $this->traverseNodesWithCallable($newParamType, static function (Node $node) { // original node has to removed to avoid tokens crashing from origin positions $node->setAttribute(AttributeKey::ORIGINAL_NODE, null); return null; }); $decoratedParam->type = $newParamType; } private function shouldSkipParam(Param $param, ClassMethod $classMethod) : bool { // already has type, skip if ($param->type !== null) { return \true; } if ($param->variadic) { return \true; } return !$this->paramTypeAddGuard->isLegal($param, $classMethod); } /** * @param array $callers */ private function refactorClassMethod(ClassMethod $classMethod, array $callers, Scope $scope) : bool { $hasChanged = \false; foreach ($classMethod->params as $param) { if ($this->shouldSkipParam($param, $classMethod)) { continue; } foreach ($callers as $caller) { $paramType = $this->callerParamMatcher->matchCallParamType($caller, $param, $scope); if ($paramType === null) { continue; } $this->mirrorParamType($param, $paramType); $hasChanged = \true; } } return $hasChanged; } }