Skip to content

Commit

Permalink
[Php81] Use PHPStan ParametersAcceptor to detect allowed argument typ…
Browse files Browse the repository at this point in the history
…es on NullToStrictStringFuncCallArgRector (#5829)
  • Loading branch information
samsonasik authored Apr 18, 2024
1 parent 56e2031 commit 25c1b43
Showing 1 changed file with 26 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\Cast\String_ as CastString_;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\FuncCall;
Expand All @@ -17,7 +16,10 @@
use PhpParser\Node\Scalar\String_;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\Native\NativeFunctionReflection;
use PHPStan\Reflection\Native\NativeParameterWithPhpDocsReflection;
use PHPStan\Reflection\ParametersAcceptor;
use PHPStan\Type\ErrorType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NullType;
Expand Down Expand Up @@ -113,9 +115,23 @@ public function refactor(Node $node): ?Node
$classReflection = $scope->getClassReflection();
$isTrait = $classReflection instanceof ClassReflection && $classReflection->isTrait();

$functionReflection = $this->reflectionResolver->resolveFunctionLikeReflectionFromCall($node);
if (! $functionReflection instanceof FunctionReflection) {
return null;
}

$parametersAcceptor = ParametersAcceptorSelectorVariantsWrapper::select($functionReflection, $node, $scope);
$isChanged = false;

foreach ($positions as $position) {
$result = $this->processNullToStrictStringOnNodePosition($node, $args, $position, $isTrait, $scope);
$result = $this->processNullToStrictStringOnNodePosition(
$node,
$args,
$position,
$isTrait,
$scope,
$parametersAcceptor
);
if ($result instanceof Node) {
$node = $result;
$isChanged = true;
Expand Down Expand Up @@ -167,7 +183,8 @@ private function processNullToStrictStringOnNodePosition(
array $args,
int|string $position,
bool $isTrait,
Scope $scope
Scope $scope,
ParametersAcceptor $parametersAcceptor
): ?FuncCall {
if (! isset($args[$position])) {
return null;
Expand Down Expand Up @@ -208,8 +225,12 @@ private function processNullToStrictStringOnNodePosition(
return null;
}

if ($type instanceof MixedType && $args[$position]->value instanceof ArrayDimFetch) {
return null;
$parameter = $parametersAcceptor->getParameters()[$position] ?? null;
if ($parameter instanceof NativeParameterWithPhpDocsReflection && $parameter->getType() instanceof UnionType) {
$parameterType = $parameter->getType();
if (! $this->isValidUnionType($parameterType)) {
return null;
}
}

$args[$position]->value = new CastString_($argValue);
Expand Down

0 comments on commit 25c1b43

Please sign in to comment.