Skip to content

fixed count() type after array_pop/array_shift #1691

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

Closed
wants to merge 1 commit into from
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
18 changes: 16 additions & 2 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Stmt\Break_;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Continue_;
Expand Down Expand Up @@ -129,6 +130,7 @@
use PHPStan\Type\Generic\GenericClassStringType;
use PHPStan\Type\Generic\TemplateTypeHelper;
use PHPStan\Type\Generic\TemplateTypeMap;
use PHPStan\Type\IntegerRangeType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\MixedType;
Expand Down Expand Up @@ -391,7 +393,7 @@ private function processStmtNode(
$nodeCallback($declare->value, $scope);
if (
$declare->key->name !== 'strict_types'
|| !($declare->value instanceof Node\Scalar\LNumber)
|| !($declare->value instanceof LNumber)
|| $declare->value->value !== 1
) {
continue;
Expand Down Expand Up @@ -1817,6 +1819,10 @@ function (MutatingScope $scope) use ($expr, $nodeCallback, $context): Expression
) {
$arrayArg = $expr->getArgs()[0]->value;
$arrayArgType = $scope->getType($arrayArg);

$countExpr = new FuncCall(new Name('count'), [$expr->getArgs()[0]]);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

atm this line depends on whether the code beeing analyzed is in a namespace or not

$decrementedCountType = $scope->getType(new BinaryOp\Minus($countExpr, new LNumber(1)));

$scope = $scope->invalidateExpression($arrayArg);

$functionName = $functionReflection->getName();
Expand All @@ -1833,6 +1839,14 @@ function (MutatingScope $scope) use ($expr, $nodeCallback, $context): Expression
return $type;
});

$validCount = IntegerRangeType::fromInterval(0, null);
if ($validCount->isSuperTypeOf($decrementedCountType)->yes()) {
$scope = $scope->assignExpression(
$countExpr,
$decrementedCountType,
);
}

$scope = $scope->assignExpression(
$arrayArg,
$arrayArgType,
Expand Down Expand Up @@ -3577,7 +3591,7 @@ static function (): void {
$throwPoints = array_merge($throwPoints, $itemResult->getThrowPoints());

if ($arrayItem->key === null) {
$dimExpr = new Node\Scalar\LNumber($i);
$dimExpr = new LNumber($i);
} else {
$dimExpr = $arrayItem->key;
}
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1008,6 +1008,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Arrays/data/slevomat-foreach-array-key-exists-bug.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/array-key-exists.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7909.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7804.php');
}

/**
Expand Down
42 changes: 42 additions & 0 deletions tests/PHPStan/Analyser/data/bug-7804.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php declare(strict_types = 1);

namespace Bug7804;

use function PHPStan\Testing\assertType;

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

/** @param array<int, string> $headers */
function pop2(array $headers): void
{
assertType('int<0, max>', count($headers));
array_pop($headers);
assertType('int<0, max>', count($headers));
}

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


/** @param array<int, string> $headers */
function shift2(array $headers): void
{
assertType('int<0, max>', count($headers));
array_shift($headers);
assertType('int<0, max>', count($headers));
}