Skip to content

Commit 26d29ec

Browse files
committed
array_shift() should invalidate remembered count() call
1 parent ca044c4 commit 26d29ec

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

src/Analyser/NodeScopeResolver.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -1478,6 +1478,7 @@ function (MutatingScope $scope) use ($expr, $nodeCallback, $context): Expression
14781478
) {
14791479
$arrayArg = $expr->args[0]->value;
14801480
$constantArrays = TypeUtils::getConstantArrays($scope->getType($arrayArg));
1481+
$scope = $scope->invalidateExpression($arrayArg);
14811482
if (count($constantArrays) > 0) {
14821483
$resultArrayTypes = [];
14831484

@@ -1489,14 +1490,14 @@ function (MutatingScope $scope) use ($expr, $nodeCallback, $context): Expression
14891490
}
14901491
}
14911492

1492-
$scope = $scope->invalidateExpression($arrayArg)->specifyExpressionType(
1493+
$scope = $scope->specifyExpressionType(
14931494
$arrayArg,
14941495
TypeCombinator::union(...$resultArrayTypes)
14951496
);
14961497
} else {
14971498
$arrays = TypeUtils::getAnyArrays($scope->getType($arrayArg));
14981499
if (count($arrays) > 0) {
1499-
$scope = $scope->invalidateExpression($arrayArg)->specifyExpressionType($arrayArg, TypeCombinator::union(...$arrays));
1500+
$scope = $scope->specifyExpressionType($arrayArg, TypeCombinator::union(...$arrays));
15001501
}
15011502
}
15021503
}

tests/PHPStan/Analyser/NodeScopeResolverTest.php

+6
Original file line numberDiff line numberDiff line change
@@ -10240,6 +10240,11 @@ public function dataBug3991(): array
1024010240
return $this->gatherAssertTypes(__DIR__ . '/data/bug-3991.php');
1024110241
}
1024210242

10243+
public function dataBug3993(): array
10244+
{
10245+
return $this->gatherAssertTypes(__DIR__ . '/data/bug-3993.php');
10246+
}
10247+
1024310248
/**
1024410249
* @dataProvider dataBug2574
1024510250
* @dataProvider dataBug2577
@@ -10330,6 +10335,7 @@ public function dataBug3991(): array
1033010335
* @dataProvider dataArraySliceNonEmpty
1033110336
* @dataProvider dataBug3990
1033210337
* @dataProvider dataBug3991
10338+
* @dataProvider dataBug3993
1033310339
* @param string $assertType
1033410340
* @param string $file
1033510341
* @param mixed ...$args
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Bug3993;
4+
5+
use function PHPStan\Analyser\assertType;
6+
7+
class Foo
8+
{
9+
10+
public function doFoo($arguments)
11+
{
12+
if (!isset($arguments) || count($arguments) === 0) {
13+
return;
14+
}
15+
16+
assertType('mixed~null', $arguments);
17+
18+
array_shift($arguments);
19+
20+
assertType('mixed~null', $arguments);
21+
assertType('int<0, max>', count($arguments));
22+
}
23+
24+
}

0 commit comments

Comments
 (0)