typeComparator = $typeComparator; $this->phpVersionProvider = $phpVersionProvider; $this->staticTypeMapper = $staticTypeMapper; } public function getRuleDefinition() : RuleDefinition { return new RuleDefinition('Add param types where needed', [new ConfiguredCodeSample(<<<'CODE_SAMPLE' (new SomeClass)->process(function ($parameter) {}); CODE_SAMPLE , <<<'CODE_SAMPLE' (new SomeClass)->process(function (string $parameter) {}); CODE_SAMPLE , [new AddParamTypeForFunctionLikeWithinCallLikeArgDeclaration('SomeClass', 'process', 0, 0, new StringType())])]); } /** * @return array> */ public function getNodeTypes() : array { return [MethodCall::class, StaticCall::class]; } /** * @param CallLike $node */ public function refactor(Node $node) : ?Node { $this->hasChanged = \false; foreach ($this->addParamTypeForFunctionLikeParamDeclarations as $addParamTypeForFunctionLikeParamDeclaration) { switch (\true) { case $node instanceof MethodCall: $type = $node->var; break; case $node instanceof StaticCall: $type = $node->class; break; default: $type = null; break; } if ($type === null) { continue; } if (!$this->isObjectType($type, $addParamTypeForFunctionLikeParamDeclaration->getObjectType())) { continue; } if (($node->name ?? null) === null) { continue; } if (!$node->name instanceof Identifier) { continue; } if (!$this->isName($node->name, $addParamTypeForFunctionLikeParamDeclaration->getMethodName())) { continue; } $this->processFunctionLike($node, $addParamTypeForFunctionLikeParamDeclaration); } if (!$this->hasChanged) { return null; } return $node; } /** * @param mixed[] $configuration */ public function configure(array $configuration) : void { Assert::allIsAOf($configuration, AddParamTypeForFunctionLikeWithinCallLikeArgDeclaration::class); $this->addParamTypeForFunctionLikeParamDeclarations = $configuration; } private function processFunctionLike(CallLike $callLike, AddParamTypeForFunctionLikeWithinCallLikeArgDeclaration $addParamTypeForFunctionLikeWithinCallLikeArgDeclaration) : void { if ($callLike->isFirstClassCallable()) { return; } if (\is_int($addParamTypeForFunctionLikeWithinCallLikeArgDeclaration->getCallLikePosition())) { if ($callLike->getArgs() === []) { return; } $arg = $callLike->args[$addParamTypeForFunctionLikeWithinCallLikeArgDeclaration->getCallLikePosition()] ?? null; if (!$arg instanceof Arg) { return; } // int positions shouldn't have names if ($arg->name !== null) { return; } } else { $args = \array_filter($callLike->getArgs(), static function (Arg $arg) use($addParamTypeForFunctionLikeWithinCallLikeArgDeclaration) : bool { if ($arg->name === null) { return \false; } return $arg->name->name === $addParamTypeForFunctionLikeWithinCallLikeArgDeclaration->getCallLikePosition(); }); if ($args === []) { return; } $arg = \array_values($args)[0]; } $functionLike = $arg->value; if (!$functionLike instanceof FunctionLike) { return; } if (!isset($functionLike->params[$addParamTypeForFunctionLikeWithinCallLikeArgDeclaration->getFunctionLikePosition()])) { return; } $this->refactorParameter($functionLike->params[$addParamTypeForFunctionLikeWithinCallLikeArgDeclaration->getFunctionLikePosition()], $addParamTypeForFunctionLikeWithinCallLikeArgDeclaration); } private function refactorParameter(Param $param, AddParamTypeForFunctionLikeWithinCallLikeArgDeclaration $addParamTypeForFunctionLikeWithinCallLikeArgDeclaration) : void { // already set → no change if ($param->type !== null) { $currentParamType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($param->type); if ($this->typeComparator->areTypesEqual($currentParamType, $addParamTypeForFunctionLikeWithinCallLikeArgDeclaration->getParamType())) { return; } } $paramTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($addParamTypeForFunctionLikeWithinCallLikeArgDeclaration->getParamType(), TypeKind::PARAM); $this->hasChanged = \true; // remove it if ($addParamTypeForFunctionLikeWithinCallLikeArgDeclaration->getParamType() instanceof MixedType) { if ($this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::MIXED_TYPE)) { $param->type = $paramTypeNode; return; } $param->type = null; return; } $param->type = $paramTypeNode; } }