Skip to content

More precise count($array) after array_* functions #3954

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

Draft
wants to merge 2 commits into
base: 2.1.x
Choose a base branch
from
Draft
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
49 changes: 47 additions & 2 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\Int_;
use PhpParser\Node\Stmt\Break_;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Continue_;
Expand Down Expand Up @@ -174,6 +175,7 @@
use PHPStan\Type\Generic\TemplateTypeMap;
use PHPStan\Type\Generic\TemplateTypeVariance;
use PHPStan\Type\Generic\TemplateTypeVarianceMap;
use PHPStan\Type\IntegerRangeType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\MixedType;
Expand Down Expand Up @@ -2628,29 +2630,72 @@ static function (): void {
&& in_array($functionReflection->getName(), ['array_pop', 'array_shift'], true)
&& count($expr->getArgs()) >= 1
) {
$arrayArg = $expr->getArgs()[0]->value;
$args = $expr->getArgs();
$arrayArg = $args[0]->value;

$arrayArgType = $scope->getType($arrayArg);
$arrayArgNativeType = $scope->getNativeType($arrayArg);

$countArrayExpr = new FuncCall(new Name('count'), [$args[0]]);
$hasCountExpr = $scope->hasExpressionType($countArrayExpr)->yes();
if ($hasCountExpr) {
$countType = $scope->getType(new BinaryOp\Minus($countArrayExpr, new Int_(1)));
$countNativeType = $scope->getNativeType(new BinaryOp\Minus($countArrayExpr, new Int_(1)));
}

$isArrayPop = $functionReflection->getName() === 'array_pop';
$scope = $scope->invalidateExpression($arrayArg)->assignExpression(
$arrayArg,
$isArrayPop ? $arrayArgType->popArray() : $arrayArgType->shiftArray(),
$isArrayPop ? $arrayArgNativeType->popArray() : $arrayArgNativeType->shiftArray(),
);

if (
$hasCountExpr
&& IntegerRangeType::fromInterval(0, null)->isSuperTypeOf($countType)->yes()
&& IntegerRangeType::fromInterval(0, null)->isSuperTypeOf($countNativeType)->yes()
) {
$scope = $scope->assignExpression($countArrayExpr, $countType, $countNativeType);
}
}

if (
$functionReflection !== null
&& in_array($functionReflection->getName(), ['array_push', 'array_unshift'], true)
&& count($expr->getArgs()) >= 2
) {
$args = $expr->getArgs();
$arrayType = $this->getArrayFunctionAppendingType($functionReflection, $scope, $expr);
$arrayNativeType = $this->getArrayFunctionAppendingType($functionReflection, $scope->doNotTreatPhpDocTypesAsCertain(), $expr);

$arrayArg = $expr->getArgs()[0]->value;
$arrayArg = $args[0]->value;
$addedElementsCount = count($args) - 1;
for ($i = 1; $i < count($args); $i++) {
if ($args[$i]->unpack) {
$addedElementsCount = null;
break;
}
}

$countArrayExpr = new FuncCall(new Name('count'), [$args[0]]);
$hasCountExpr = $scope->hasExpressionType($countArrayExpr)->yes();
if ($hasCountExpr && $addedElementsCount !== null) {
$countType = $scope->getType(new BinaryOp\Plus($countArrayExpr, new Int_($addedElementsCount)));
$countNativeType = $scope->getNativeType(new BinaryOp\Plus($countArrayExpr, new Int_($addedElementsCount)));
} else {
$countType = IntegerRangeType::fromInterval($addedElementsCount, null);
$countNativeType = IntegerRangeType::fromInterval($addedElementsCount, null);
}

$scope = $scope->invalidateExpression($arrayArg)->assignExpression($arrayArg, $arrayType, $arrayNativeType);

if (
IntegerRangeType::fromInterval(0, null)->isSuperTypeOf($countType)->yes()
&& IntegerRangeType::fromInterval(0, null)->isSuperTypeOf($countNativeType)->yes()
) {
$scope = $scope->assignExpression($countArrayExpr, $countType, $countNativeType);
}

}

if (
Expand Down
6 changes: 3 additions & 3 deletions tests/PHPStan/Analyser/nsrt/bug-2750.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ function (array $input) {
\assert(count($input) > 0);
assertType('int<1, max>', count($input));
array_unshift($input, 'test');
assertType('int<1, max>', count($input));
assertType('int<2, max>', count($input));

\assert(count($input) > 0);
assertType('int<1, max>', count($input));
assertType('int<2, max>', count($input));
array_push($input, 'nope');
assertType('int<1, max>', count($input));
assertType('int<3, max>', count($input));
};
64 changes: 64 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-7804.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Bug7804;

use function PHPStan\Testing\assertType;

/** @param array<int, string> $headers */
function headers(array $headers): void
{
assertType('int<0, max>', count($headers));
if (count($headers) >= 4) {
assertType('int<4, max>', count($headers));
array_pop($headers);
assertType('int<3, max>', count($headers));
array_pop($headers);
assertType('int<2, max>', count($headers));
array_pop($headers);
assertType('int<1, max>', count($headers));
array_pop($headers);
assertType('int<0, max>', count($headers));
array_pop($headers);
assertType('int<0, max>', count($headers));
}
assertType('int<0, max>', count($headers));
}

function doPop(array $arr) {
assertType('int<0, max>', count($arr));
array_pop($arr);
assertType('int<0, max>', count($arr));

if (count($arr) === 2) {
assertType('2', count($arr));
array_pop($arr);
assertType('1', count($arr));
}
assertType('int<0, 1>|int<3, max>', count($arr));
}

function doShift(array $arr) {
assertType('int<0, max>', count($arr));
array_shift($arr);
assertType('int<0, max>', count($arr));
}

function doPush(array $arr, int $i) {
assertType('int<0, max>', count($arr));
array_push($arr, $i);
assertType('int<1, max>', count($arr));
array_push($arr, 3, $i, false, null);
assertType('int<5, max>', count($arr));
}

function doPushVariadic(array $arr, array $arr2) {
assertType('int<0, max>', count($arr));
array_push($arr, ...$arr2);
assertType('int<0, max>', count($arr));
}

function doUnshift(array $arr, bool $b) {
assertType('int<0, max>', count($arr));
array_unshift($arr, $b);
assertType('int<1, max>', count($arr));
}
Loading