Skip to content

Commit

Permalink
Allows for dynamic hinting
Browse files Browse the repository at this point in the history
  • Loading branch information
peterfox committed Aug 21, 2024
1 parent 9c0c6a8 commit 18bc621
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ SomeClass::someDynamicCall(AnotherClass::class, function ($object) {
return $object;
});

SomeClass::someDynamicCallWithVar('test', function ($object) {
return $object;
});

?>
-----
<?php
Expand All @@ -24,4 +28,8 @@ SomeClass::someDynamicCall(AnotherClass::class, function (\SomeNamespace\Another
return $object;
});

SomeClass::someDynamicCallWithVar('test', function (string $object) {
return $object;
});

?>
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
use PhpParser\Node\Name;
use PHPStan\Type\ObjectType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use Rector\Config\RectorConfig;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\TypeDeclaration\Rector\FunctionLike\AddParamTypeForFunctionLikeWithinCallLikeArgDeclarationRector;
use Rector\TypeDeclaration\ValueObject\AddParamTypeForFunctionLikeWithinCallLikeArgDeclaration;
use Rector\ValueObject\PhpVersionFeature;
Expand Down Expand Up @@ -53,6 +55,21 @@ function (array $args): ?ObjectType {
return null;
},
),
new AddParamTypeForFunctionLikeWithinCallLikeArgDeclaration(
'SomeNamespace\SomeClass',
'someDynamicCallWithVar',
1,
0,
function (array $args, NodeTypeResolver $nodeTypeResolver): ?Type {
if ($args === [] || ! $args[0] instanceof Arg) {
return null;
}

$var = $args[0]->value;

return $nodeTypeResolver->getType($var);
},
),
]);

$rectorConfig->phpVersion(PhpVersionFeature::MIXED_TYPE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ private function refactorParameter(
$newParameterType = $addParamTypeForFunctionLikeWithinCallLikeArgDeclaration->getParamType();

if (is_callable($newParameterType)) {
$newParameterType = call_user_func($newParameterType, $args);
$newParameterType = call_user_func($newParameterType, $args, $this->nodeTypeResolver);
}

if (! $newParameterType instanceof Type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PhpParser\Node\Arg;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\Validation\RectorAssert;

/**
Expand All @@ -18,7 +19,7 @@
/**
* @param int<0, max>|string $callLikePosition
* @param int<0, max> $functionLikePosition
* @param Type|Closure(Arg[]): ?Type $paramType
* @param Type|Closure(Arg[], NodeTypeResolver): ?Type $paramType
*/
public function __construct(
private string $className,
Expand Down Expand Up @@ -57,7 +58,7 @@ public function getFunctionLikePosition(): int
}

/**
* @return Type|Closure(Arg[]): ?Type
* @return Type|Closure(Arg[], NodeTypeResolver): ?Type
*/
public function getParamType(): Type|Closure
{
Expand Down

0 comments on commit 18bc621

Please sign in to comment.