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) 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
{
$newParameterType = $addParamTypeForFunctionLikeWithinCallLikeArgDeclaration->getParamType();
// already set → no change
if ($param->type !== null) {
$currentParamType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($param->type);
if ($this->typeComparator->areTypesEqual($currentParamType, $newParameterType)) {
return;
}
}
$paramTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($newParameterType, TypeKind::PARAM);
$this->hasChanged = \true;
// remove it
if ($newParameterType instanceof MixedType) {
if ($this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::MIXED_TYPE)) {
$param->type = $paramTypeNode;
return;
}
$param->type = null;
return;
}
$param->type = $paramTypeNode;
}
}