Skip to content

Commit

Permalink
Use ParametersAcceptorSelector::selectFromArgs() instead of `select…
Browse files Browse the repository at this point in the history
…Single()` wherever possible
  • Loading branch information
ondrejmirtes committed Sep 26, 2024
1 parent 865c618 commit e283d3a
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 11 deletions.
12 changes: 10 additions & 2 deletions src/Internal/ContainerDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,19 @@ public function isMethodSupported(MethodReflection $methodReflection): bool
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
{
if (count($methodCall->getArgs()) === 0) {
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
return ParametersAcceptorSelector::selectFromArgs(
$scope,
$methodCall->getArgs(),
$methodReflection->getVariants(),
)->getReturnType();
}
$argType = $scope->getType($methodCall->getArgs()[0]->value);
if (!$argType instanceof ConstantStringType) {
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
return ParametersAcceptorSelector::selectFromArgs(
$scope,
$methodCall->getArgs(),
$methodReflection->getVariants(),
)->getReturnType();
}

$type = new ObjectType($argType->getValue());
Expand Down
6 changes: 5 additions & 1 deletion src/Type/Php/HashFunctionsReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,11 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo

public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
{
$defaultReturnType = ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType();
$defaultReturnType = ParametersAcceptorSelector::selectFromArgs(
$scope,
$functionCall->getArgs(),
$functionReflection->getVariants(),
)->getReturnType();

if (!isset($functionCall->getArgs()[0])) {
return $defaultReturnType;
Expand Down
6 changes: 5 additions & 1 deletion src/Type/Php/JsonThrowOnErrorDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ public function getTypeFromFunctionCall(
): Type
{
$argumentPosition = $this->argumentPositions[$functionReflection->getName()];
$defaultReturnType = ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType();
$defaultReturnType = ParametersAcceptorSelector::selectFromArgs(
$scope,
$functionCall->getArgs(),
$functionReflection->getVariants(),
)->getReturnType();

if ($functionReflection->getName() === 'json_decode') {
$defaultReturnType = $this->narrowTypeForJsonDecode($functionCall, $scope, $defaultReturnType);
Expand Down
6 changes: 5 additions & 1 deletion src/Type/Php/MbFunctionsReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo

public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
{
$returnType = ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType();
$returnType = ParametersAcceptorSelector::selectFromArgs(
$scope,
$functionCall->getArgs(),
$functionReflection->getVariants(),
)->getReturnType();
$positionEncodingParam = $this->encodingPositionMap[$functionReflection->getName()];

if (count($functionCall->getArgs()) < $positionEncodingParam) {
Expand Down
6 changes: 5 additions & 1 deletion src/Type/Php/MbStrlenFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,11 @@ public function getTypeFromFunctionCall(
$range = new ConstantIntegerType(0);
} else {
$range = TypeCombinator::remove(
ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType(),
ParametersAcceptorSelector::selectFromArgs(
$scope,
$functionCall->getArgs(),
$functionReflection->getVariants(),
)->getReturnType(),
new ConstantBooleanType(false),
);
}
Expand Down
6 changes: 5 additions & 1 deletion src/Type/Php/PregFilterFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo

public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
{
$defaultReturn = ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType();
$defaultReturn = ParametersAcceptorSelector::selectFromArgs(
$scope,
$functionCall->getArgs(),
$functionReflection->getVariants(),
)->getReturnType();

$argsCount = count($functionCall->getArgs());
if ($argsCount < 3) {
Expand Down
6 changes: 5 additions & 1 deletion src/Type/Php/StrtotimeFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo

public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
{
$defaultReturnType = ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType();
$defaultReturnType = ParametersAcceptorSelector::selectFromArgs(
$scope,
$functionCall->getArgs(),
$functionReflection->getVariants(),
)->getReturnType();
if (count($functionCall->getArgs()) === 0) {
return $defaultReturnType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ public function getType(Expr $expr, Scope $scope): ?Type
return null;
}

$returnType = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
$returnType = ParametersAcceptorSelector::selectFromArgs(
$scope,
$expr->getArgs(),
$methodReflection->getVariants(),
)->getReturnType();

if ($returnType instanceof StringType) {
return null;
Expand Down
4 changes: 2 additions & 2 deletions tests/PHPStan/Analyser/nsrt/preg_filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ function doFoo1() {

function doFoo2() {
$subject = 123;
assertType('list<string>|string|null', preg_filter('/\d/', '$0', $subject));
assertType('string|null', preg_filter('/\d/', '$0', $subject));

$subject = 123.123;
assertType('list<string>|string|null', preg_filter('/\d/', '$0', $subject));
assertType('string|null', preg_filter('/\d/', '$0', $subject));
}

public function dooFoo3(string $pattern, string $replace) {
Expand Down

0 comments on commit e283d3a

Please sign in to comment.