Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/Analyser/ExprHandler/FuncCallHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use PHPStan\Node\Expr\PossiblyImpureCallExpr;
use PHPStan\Node\Expr\TypeExpr;
use PHPStan\Reflection\Callables\CallableParametersAcceptor;
use PHPStan\Reflection\ExtendedParameterReflection;
use PHPStan\Reflection\Callables\SimpleImpurePoint;
use PHPStan\Reflection\Callables\SimpleThrowPoint;
use PHPStan\Reflection\FunctionReflection;
Expand Down Expand Up @@ -208,6 +209,51 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
$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();

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;
}
Comment thread
staabm marked this conversation as resolved.
Outdated

$byRefType = $innerParameter instanceof ExtendedParameterReflection && $innerParameter->getOutType() !== null
? $innerParameter->getOutType()
: $innerParameter->getType();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is

					if ($currentParameter === null) {
						throw new ShouldNotHappenException();
					}

					$argValue = $arg->value;
					if (!$argValue instanceof Variable || $argValue->name !== 'this') {
						$paramOutType = $this->getParameterOutExtensionsType($callLike, $calleeReflection, $currentParameter, $scope);
						if ($paramOutType !== null) {
							$byRefType = $paramOutType;
						} elseif (
							$currentParameter instanceof ExtendedParameterReflection
							&& $currentParameter->getOutType() !== null
						) {
							$byRefType = $currentParameter->getOutType();
						} elseif (
							$calleeReflection instanceof MethodReflection
							&& !$calleeReflection->getDeclaringClass()->isBuiltin()
						) {
							$byRefType = $currentParameter->getType();
						} elseif (
							$calleeReflection instanceof FunctionReflection
							&& !$calleeReflection->isBuiltin()
						) {
							$byRefType = $currentParameter->getType();
						} else {
							$byRefType = new MixedType();
						}

						$scope = $this->processVirtualAssign(
							$scope,
							$storage,
							$stmt,
							$argValue,
							new TypeExpr($byRefType),
							$nodeCallback,
						)->getScope();
						$scope = $this->lookForUnsetAllowedUndefinedExpressions($scope, $argValue);
					}
				}

in NodeScopeResolver

There is certainly something to refactor and use here.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushed. Here's a summary of the changes addressing VincentLanglet's review:

  1. Extracted resolveByRefParameterType() on NodeScopeResolver - New public method centralizing the by-reference parameter type resolution logic (extension types via getParameterOutExtensionsType, @param-out via getOutType(), builtin vs user-defined distinction, MixedType fallback). Used in both processArgs and FuncCallHandler.

  2. Added resolveCallUserFuncCalleeReflection() on FuncCallHandler - Resolves the inner callee reflection from the callback expression: handles Name-based function resolution, constant string function names, and array-based method callables via findTypeAndMethodNames().

  3. Added lookForUnsetAllowedUndefinedExpressions() call in FuncCallHandler after the virtual assignment, matching the NodeScopeResolver pattern.

  4. Removed the ExtendedParameterReflection import from FuncCallHandler since that logic now lives in resolveByRefParameterType.

$scope = $nodeScopeResolver->processVirtualAssign(
$scope,
$storage,
$stmt,
$argValue,
new TypeExpr($byRefType),
$nodeCallback,
)->getScope();
}
}
}

if ($normalizedExpr->name instanceof Expr) {
$nameType = $scope->getType($normalizedExpr->name);
if (
Expand Down
54 changes: 54 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-6799.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php declare(strict_types = 1);

namespace Bug6799;
Comment thread
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);
}
}
Loading