Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
66 changes: 66 additions & 0 deletions src/Analyser/ExprHandler/FuncCallHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,72 @@
$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) {
$argValue = $innerArg->value;
if ($argValue instanceof Variable && $argValue->name === 'this') {
continue;
}

$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;
}

$innerCalleeReflections = [];
if ($innerFuncCall->name instanceof Expr) {
$calledOnType = $scope->getType($innerFuncCall->name);
if (!$calledOnType->isCallable()->no()) {

Check warning on line 244 in src/Analyser/ExprHandler/FuncCallHandler.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ $innerCalleeReflections = []; if ($innerFuncCall->name instanceof Expr) { $calledOnType = $scope->getType($innerFuncCall->name); - if (!$calledOnType->isCallable()->no()) { + if ($calledOnType->isCallable()->yes()) { $innerCalleeReflections = array_map( static fn (CallableParametersAcceptor $callableAcceptor) => $callableAcceptor->getCalleeReflection(), $calledOnType->getCallableParametersAcceptors($scope),

Check warning on line 244 in src/Analyser/ExprHandler/FuncCallHandler.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ $innerCalleeReflections = []; if ($innerFuncCall->name instanceof Expr) { $calledOnType = $scope->getType($innerFuncCall->name); - if (!$calledOnType->isCallable()->no()) { + if ($calledOnType->isCallable()->yes()) { $innerCalleeReflections = array_map( static fn (CallableParametersAcceptor $callableAcceptor) => $callableAcceptor->getCalleeReflection(), $calledOnType->getCallableParametersAcceptors($scope),
$innerCalleeReflections = array_map(
static fn (CallableParametersAcceptor $callableAcceptor) => $callableAcceptor->getCalleeReflection(),
$calledOnType->getCallableParametersAcceptors($scope),
);
}
} elseif ($this->reflectionProvider->hasFunction($innerFuncCall->name, $scope)) {
$innerCalleeReflections = [$this->reflectionProvider->getFunction($innerFuncCall->name, $scope)];
}

if ($innerCalleeReflections === []) {
$byRefType = $nodeScopeResolver->resolveByRefParameterType($innerFuncCall, null, $innerParameter, $scope);
} else {
$byRefTypes = [];
foreach ($innerCalleeReflections as $innerCalleeReflection) {
$byRefTypes[] = $nodeScopeResolver->resolveByRefParameterType($innerFuncCall, $innerCalleeReflection, $innerParameter, $scope);
}
$byRefType = TypeCombinator::union(...$byRefTypes);
}

$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 (
Expand Down
53 changes: 32 additions & 21 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3475,27 +3475,7 @@ public function processArgs(

$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();
}
$byRefType = $this->resolveByRefParameterType($callLike, $calleeReflection, $currentParameter, $scope);

$scope = $this->processVirtualAssign(
$scope,
Expand Down Expand Up @@ -3605,6 +3585,37 @@ private function getParameterTypeFromParameterClosureTypeExtension(CallLike $cal
return null;
}

public function resolveByRefParameterType(CallLike $callLike, MethodReflection|FunctionReflection|null $calleeReflection, ParameterReflection $currentParameter, MutatingScope $scope): Type
{
$paramOutType = $this->getParameterOutExtensionsType($callLike, $calleeReflection, $currentParameter, $scope);
if ($paramOutType !== null) {
return $paramOutType;
}

if (
$currentParameter instanceof ExtendedParameterReflection
&& $currentParameter->getOutType() !== null
) {
return $currentParameter->getOutType();
}

if (
$calleeReflection instanceof MethodReflection
&& !$calleeReflection->getDeclaringClass()->isBuiltin()
) {
return $currentParameter->getType();
}

if (
$calleeReflection instanceof FunctionReflection
&& !$calleeReflection->isBuiltin()
) {
return $currentParameter->getType();
}

return new MixedType();
}

/**
* @param MethodReflection|FunctionReflection|null $calleeReflection
*/
Expand Down
4 changes: 4 additions & 0 deletions src/Reflection/Callables/CallableParametersAcceptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use PHPStan\Node\InvalidateExprNode;
use PHPStan\Reflection\Assertions;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptor;
use PHPStan\TrinaryLogic;

Expand Down Expand Up @@ -60,4 +62,6 @@ public function mustUseReturnValue(): TrinaryLogic;

public function getAsserts(): Assertions;

public function getCalleeReflection(): FunctionReflection|MethodReflection|null;

}
5 changes: 5 additions & 0 deletions src/Reflection/Callables/FunctionCallableVariant.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,9 @@ public function getAsserts(): Assertions
return $this->function->getAsserts();
}

public function getCalleeReflection(): FunctionReflection|ExtendedMethodReflection
{
return $this->function;
}

}
7 changes: 7 additions & 0 deletions src/Reflection/ExtendedCallableFunctionVariant.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use PHPStan\Reflection\Callables\CallableParametersAcceptor;
use PHPStan\Reflection\Callables\SimpleImpurePoint;
use PHPStan\Reflection\Callables\SimpleThrowPoint;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\MethodReflection;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Generic\TemplateTypeMap;
use PHPStan\Type\Generic\TemplateTypeVarianceMap;
Expand Down Expand Up @@ -92,4 +94,9 @@ public function getAsserts(): Assertions
return $this->assertions ?? Assertions::createEmpty();
}

public function getCalleeReflection(): FunctionReflection|MethodReflection|null
{
return null;
}

}
5 changes: 5 additions & 0 deletions src/Reflection/InaccessibleMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,9 @@ public function getAsserts(): Assertions
return Assertions::createEmpty();
}

public function getCalleeReflection(): ExtendedMethodReflection
{
return $this->methodReflection;
}

}
7 changes: 7 additions & 0 deletions src/Reflection/ResolvedFunctionVariantWithCallable.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use PHPStan\Reflection\Callables\CallableParametersAcceptor;
use PHPStan\Reflection\Callables\SimpleImpurePoint;
use PHPStan\Reflection\Callables\SimpleThrowPoint;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\MethodReflection;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Generic\TemplateTypeMap;
use PHPStan\Type\Generic\TemplateTypeVarianceMap;
Expand Down Expand Up @@ -124,4 +126,9 @@ public function getAsserts(): Assertions
return $this->assertions ?? Assertions::createEmpty();
}

public function getCalleeReflection(): FunctionReflection|MethodReflection|null
{
return null;
}

}
7 changes: 7 additions & 0 deletions src/Reflection/TrivialParametersAcceptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use PHPStan\Reflection\Callables\CallableParametersAcceptor;
use PHPStan\Reflection\Callables\SimpleImpurePoint;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\MethodReflection;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Generic\TemplateTypeMap;
use PHPStan\Type\Generic\TemplateTypeVarianceMap;
Expand Down Expand Up @@ -108,4 +110,9 @@ public function getAsserts(): Assertions
return Assertions::createEmpty();
}

public function getCalleeReflection(): FunctionReflection|MethodReflection|null
{
return null;
}

}
7 changes: 7 additions & 0 deletions src/Type/CallableType.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use PHPStan\Reflection\Callables\SimpleImpurePoint;
use PHPStan\Reflection\Callables\SimpleThrowPoint;
use PHPStan\Reflection\ClassMemberAccessAnswerer;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ExtendedParameterReflection;
use PHPStan\Reflection\Native\NativeParameterReflection;
use PHPStan\Reflection\ParameterReflection;
Expand Down Expand Up @@ -404,6 +406,11 @@ public function getAsserts(): Assertions
return Assertions::createEmpty();
}

public function getCalleeReflection(): FunctionReflection|MethodReflection|null
{
return null;
}

public function toNumber(): Type
{
return new ErrorType();
Expand Down
7 changes: 7 additions & 0 deletions src/Type/ClosureType.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
use PHPStan\Reflection\Callables\SimpleImpurePoint;
use PHPStan\Reflection\Callables\SimpleThrowPoint;
use PHPStan\Reflection\ClassConstantReflection;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ClassMemberAccessAnswerer;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ExtendedMethodReflection;
Expand Down Expand Up @@ -138,6 +140,11 @@ public function getAsserts(): Assertions
return $this->assertions;
}

public function getCalleeReflection(): FunctionReflection|MethodReflection|null
{
return null;
}

/**
* @return array<non-empty-string, TemplateTag>
*/
Expand Down
116 changes: 116 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-6799.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php declare(strict_types = 1);

namespace Bug6799;

use function PHPStan\Testing\assertType;

/**
* @param array<int> $items
*/
function functionProcessInts(array &$items): void
{
$items = [1, 2];
}

/**
* @param array<string> $items
*/
function functionProcessStrings(array &$items): void
{
$items = ['a', 'b'];
}

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);
}

/**
* @param array<int> $items
*/
protected function processInts(array &$items): void
{
$items = [1, 2];
}

/**
* @param array<string> $items
*/
protected function processStrings(array &$items): void
{
$items = ['a', 'b'];
}

/**
* @param 'Bug6799\functionProcessInts'|'Bug6799\functionProcessStrings' $function
*/
protected function testUnionStringCallbacks(string $function): void
{
$items = [];
call_user_func_array($function, [&$items]);
assertType('array<int|string>', $items);
}

/**
* @param array{$this, 'processInts'}|array{$this, 'processStrings'} $callback
*/
protected function testUnionArrayCallbacks(array $callback): void
{
$items = [];
call_user_func_array($callback, [&$items]);
assertType('array<int|string>', $items);
}

/**
* @param 'Bug6799\functionProcessInts'|array{$this, 'processStrings'} $callback
*/
protected function testMixedUnionCallback($callback): void
{
$items = [];
call_user_func_array($callback, [&$items]);
assertType('array<int|string>', $items);
}
}
Loading
Loading