Skip to content

Commit 3240c15

Browse files
committed
Test by-reference lookups of properties passed to functions
Cover arguments past the last parameter of variadic and non-variadic functions, functions that do not exist, and by-reference arguments of a call not known to have side effects. Fold the by-value cache into ??=, whose early return no test could tell apart from recomputing. Amp-Thread-ID: https://ampcode.com/threads/T-01a2074e-a91e-75cb-924e-1122f0be2e73
1 parent 1b2b451 commit 3240c15

2 files changed

Lines changed: 80 additions & 6 deletions

File tree

‎src/Analyser/PreservedProperties/PreservedPropertiesResolver.php‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,7 @@ public function forgetWrittenProperties(MutatingScope $scope, Expr $call): Mutat
9090
*/
9191
private function isFunctionArgumentByValue(array $functionNames, int $position): bool
9292
{
93-
$key = implode('|', $functionNames) . '#' . $position;
94-
if (isset($this->byValueCache[$key])) {
95-
return $this->byValueCache[$key];
96-
}
97-
98-
return $this->byValueCache[$key] = $this->findIsFunctionArgumentByValue($functionNames, $position);
93+
return $this->byValueCache[implode('|', $functionNames) . '#' . $position] ??= $this->findIsFunctionArgumentByValue($functionNames, $position);
9994
}
10095

10196
/**

‎tests/PHPStan/Analyser/nsrt/method-call-preserved-properties.php‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,85 @@ private function callFunctions(): void
913913

914914
}
915915

916+
function noParameters(): void
917+
{
918+
}
919+
920+
function lastParameterByRef(int $a, ?int &$b): void
921+
{
922+
}
923+
924+
function variadicByRef(int $a, ?int &...$rest): void
925+
{
926+
}
927+
928+
/**
929+
* An argument past the last parameter is by value, unless the function is variadic
930+
* and its variadic parameter is by reference; a function that does not exist can write it.
931+
*/
932+
final class FunctionArgumentPositions
933+
{
934+
935+
private int $extraForNoParameters = 1;
936+
937+
private int $extraForLastByRef = 1;
938+
939+
private int $extraForVariadicByRef = 1;
940+
941+
private int $unknownFunction = 1;
942+
943+
public function run(): void
944+
{
945+
$this->extraForNoParameters = 2;
946+
$this->extraForLastByRef = 2;
947+
$this->extraForVariadicByRef = 2;
948+
$this->unknownFunction = 2;
949+
$this->callFunctions();
950+
assertType('2', $this->extraForNoParameters);
951+
assertType('2', $this->extraForLastByRef);
952+
assertType('int', $this->extraForVariadicByRef);
953+
assertType('int', $this->unknownFunction);
954+
}
955+
956+
private function callFunctions(): void
957+
{
958+
$x = null;
959+
noParameters($this->extraForNoParameters);
960+
lastParameterByRef(1, $x, $this->extraForLastByRef);
961+
variadicByRef(1, $x, $this->extraForVariadicByRef);
962+
doesNotExist($this->unknownFunction);
963+
}
964+
965+
}
966+
967+
/** A call not known to have side effects forgets a property it passes to a by-reference parameter. */
968+
final class MaybeImpureFunctionArguments
969+
{
970+
971+
/** @var list<int> */
972+
private array $counted = [1];
973+
974+
/** @var list<int> */
975+
private array $sorted = [1];
976+
977+
public function run(): void
978+
{
979+
$this->counted = [2];
980+
$this->sorted = [2];
981+
$this->countAndSort();
982+
assertType('array{2}', $this->counted);
983+
assertType('list<int>', $this->sorted);
984+
}
985+
986+
private function countAndSort(): bool
987+
{
988+
sort($this->sorted);
989+
990+
return count($this->counted) > 0;
991+
}
992+
993+
}
994+
916995
/** What the class hands out - first-class callables and closures - runs when outside code runs. */
917996
final class HandedOutCallables
918997
{

0 commit comments

Comments
 (0)