-
Notifications
You must be signed in to change notification settings - Fork 574
Fix phpstan/phpstan#6799: wrongly reported empty array #5292
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
50749d7
2d7ce79
e30122d
7e864e4
da76a4b
45fb724
67ba647
6af1bb8
9ee8886
45ba2fd
0487f0a
98edaa0
c295032
54b6aec
3f143a8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,6 +35,7 @@ | |
| use PHPStan\Reflection\Callables\SimpleImpurePoint; | ||
| use PHPStan\Reflection\Callables\SimpleThrowPoint; | ||
| use PHPStan\Reflection\FunctionReflection; | ||
| use PHPStan\Reflection\MethodReflection; | ||
| use PHPStan\Reflection\ParametersAcceptor; | ||
| use PHPStan\Reflection\ParametersAcceptorSelector; | ||
| use PHPStan\Reflection\ReflectionProvider; | ||
|
|
@@ -208,6 +209,52 @@ | |
| $impurePoints = array_merge($impurePoints, $argsResult->getImpurePoints()); | ||
| $isAlwaysTerminating = $isAlwaysTerminating || $argsResult->isAlwaysTerminating(); | ||
|
|
||
| if ( | ||
| $functionReflection !== null | ||
| && in_array($functionReflection->getName(), ['call_user_func_array', 'call_user_func'], true) | ||
| ) { | ||
| $innerResult = $functionReflection->getName() === 'call_user_func_array' | ||
| ? ArgumentsNormalizer::reorderCallUserFuncArrayArguments($expr, $scope) | ||
| : ArgumentsNormalizer::reorderCallUserFuncArguments($expr, $scope); | ||
|
|
||
| if ($innerResult !== null) { | ||
| [$innerParametersAcceptor, $innerFuncCall] = $innerResult; | ||
| $innerParameters = $innerParametersAcceptor->getParameters(); | ||
| $innerArgs = $innerFuncCall->getArgs(); | ||
|
|
||
| $innerCalleeReflection = $this->resolveCallUserFuncCalleeReflection($innerFuncCall, $scope); | ||
|
|
||
| foreach ($innerArgs as $i => $innerArg) { | ||
| $innerParameter = null; | ||
| if (isset($innerParameters[$i])) { | ||
| $innerParameter = $innerParameters[$i]; | ||
| } elseif (count($innerParameters) > 0 && $innerParametersAcceptor->isVariadic()) { | ||
| $innerParameter = $innerParameters[count($innerParameters) - 1]; | ||
| } | ||
|
|
||
| if ($innerParameter === null || !$innerParameter->passedByReference()->createsNewVariable()) { | ||
| continue; | ||
| } | ||
|
|
||
| $argValue = $innerArg->value; | ||
| if ($argValue instanceof Variable && $argValue->name === 'this') { | ||
| continue; | ||
| } | ||
|
staabm marked this conversation as resolved.
Outdated
|
||
|
|
||
| $byRefType = $nodeScopeResolver->resolveByRefParameterType($innerFuncCall, $innerCalleeReflection, $innerParameter, $scope); | ||
| $scope = $nodeScopeResolver->processVirtualAssign( | ||
| $scope, | ||
| $storage, | ||
| $stmt, | ||
| $argValue, | ||
| new TypeExpr($byRefType), | ||
| $nodeCallback, | ||
| )->getScope(); | ||
| $scope = $nodeScopeResolver->lookForUnsetAllowedUndefinedExpressions($scope, $argValue); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if ($normalizedExpr->name instanceof Expr) { | ||
| $nameType = $scope->getType($normalizedExpr->name); | ||
| if ( | ||
|
|
@@ -820,6 +867,46 @@ | |
| return VoidToNullTypeTransformer::transform($parametersAcceptor->getReturnType(), $expr); | ||
| } | ||
|
|
||
| /** | ||
| * @return FunctionReflection|MethodReflection|null | ||
|
staabm marked this conversation as resolved.
Outdated
|
||
| */ | ||
| private function resolveCallUserFuncCalleeReflection(FuncCall $innerFuncCall, MutatingScope $scope) | ||
| { | ||
| if ($innerFuncCall->name instanceof Name && $this->reflectionProvider->hasFunction($innerFuncCall->name, $scope)) { | ||
| return $this->reflectionProvider->getFunction($innerFuncCall->name, $scope); | ||
| } | ||
|
|
||
| if (!$innerFuncCall->name instanceof Expr) { | ||
| return null; | ||
| } | ||
|
|
||
| $callbackType = $scope->getType($innerFuncCall->name); | ||
|
|
||
| foreach ($callbackType->getConstantStrings() as $constantString) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Those two foreach loops are not correct since they return the first result meaning that a call to Let's just simplify and support only when there is 1 value.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. The change simplifies All tests pass (11656 tests, 78894 assertions) and |
||
| if ($constantString->getValue() === '') { | ||
| continue; | ||
| } | ||
| $funcName = new Name($constantString->getValue()); | ||
| if ($this->reflectionProvider->hasFunction($funcName, $scope)) { | ||
| return $this->reflectionProvider->getFunction($funcName, $scope); | ||
| } | ||
| } | ||
|
staabm marked this conversation as resolved.
Outdated
|
||
|
|
||
| foreach ($callbackType->getConstantArrays() as $constantArray) { | ||
| foreach ($constantArray->findTypeAndMethodNames() as $typeAndMethod) { | ||
| if ($typeAndMethod->isUnknown() || !$typeAndMethod->getCertainty()->yes()) { | ||
|
Check warning on line 897 in src/Analyser/ExprHandler/FuncCallHandler.php
|
||
| continue; | ||
| } | ||
| $methodType = $typeAndMethod->getType(); | ||
| if ($methodType->hasMethod($typeAndMethod->getMethod())->yes()) { | ||
|
Check warning on line 901 in src/Analyser/ExprHandler/FuncCallHandler.php
|
||
| return $methodType->getMethod($typeAndMethod->getMethod(), $scope); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| private function getDynamicFunctionReturnType(MutatingScope $scope, FuncCall $normalizedNode, FunctionReflection $functionReflection): ?Type | ||
| { | ||
| foreach ($this->dynamicReturnTypeExtensionRegistryProvider->getRegistry()->getDynamicFunctionReturnTypeExtensions($functionReflection) as $dynamicFunctionReturnTypeExtension) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Bug6799; | ||
|
staabm marked this conversation as resolved.
|
||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| class HelloWorld | ||
| { | ||
| /** | ||
| * @param string[] $where | ||
| * @param string $sqlTableName | ||
| * @param mixed[] $filter | ||
| * @param string $value | ||
| */ | ||
| protected function listingAddWhereFilterAtableDefault(array &$where, string $sqlTableName, array $filter, string $value): void | ||
| { | ||
| if ($value != "" && !empty($filter) && !empty($filter['sql']) && is_string($filter['sql'])) { | ||
| $where[] = "`" . $sqlTableName . "`.`" . (string)$filter['sql'] . "` = '" . $value . "'"; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @param string[] $filterValues | ||
| * @param string[] $where | ||
| * @param string[] $tables | ||
| * @param mixed[] $filters | ||
| */ | ||
| protected function listingAddWhereFilterAtable(array $filterValues, array &$where, array &$tables, array $filters): void | ||
| { | ||
| if (!empty($filterValues) && !empty($filters)) { | ||
| $whereFilter = array(); | ||
| foreach ($filterValues as $type => $value) { | ||
| call_user_func_array(array($this, 'listingAddWhereFilterAtableDefault'), array(&$whereFilter, 'xxxx', $filters[$type], $value)); | ||
| } | ||
| assertType('array<string>', $whereFilter); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @param array<mixed> $items | ||
| * @param-out list<int> $items | ||
| */ | ||
| protected function processWithParamOut(array &$items): void | ||
| { | ||
| $items = [1, 2, 3]; | ||
| } | ||
|
|
||
| protected function testParamOut(): void | ||
| { | ||
| $items = []; | ||
| call_user_func_array([$this, 'processWithParamOut'], [&$items]); | ||
| assertType('list<int>', $items); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.