Skip to content

Commit 69db4ae

Browse files
herndlmondrejmirtes
authored andcommitted
Avoid falsely specifying never types via count()
1 parent 40d7179 commit 69db4ae

File tree

4 files changed

+36
-29
lines changed

4 files changed

+36
-29
lines changed

src/Analyser/TypeSpecifier.php

+1
Original file line numberDiff line numberDiff line change
@@ -1068,6 +1068,7 @@ private function specifyTypesForCountFuncCall(
10681068
!$isNormalCount->yes()
10691069
|| (!$isConstantArray->yes() && !$isList->yes())
10701070
|| !$oneOrMore->isSuperTypeOf($sizeType)->yes()
1071+
|| $sizeType->isSuperTypeOf($type->getArraySize())->yes()
10711072
) {
10721073
return null;
10731074
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace CountConstArray2;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
class HelloWorld
8+
{
9+
/**
10+
* @param int<1, max> $limit
11+
* @return list<\stdClass>
12+
*/
13+
public function searchRecommendedMinPrices(int $limit): array
14+
{
15+
$bestMinPrice = new \stdClass();
16+
$limit--;
17+
if ($limit === 0) {
18+
return [$bestMinPrice];
19+
}
20+
21+
$otherMinPrices = [new \stdClass()];
22+
while (count($otherMinPrices) < $limit) {
23+
$otherMinPrice = new \stdClass();
24+
if (rand(0, 1)) {
25+
$otherMinPrice = null;
26+
}
27+
if ($otherMinPrice === null) {
28+
break;
29+
}
30+
array_unshift($otherMinPrices, $otherMinPrice);
31+
}
32+
assertType('non-empty-list<stdClass>', $otherMinPrices);
33+
return [$bestMinPrice, ...$otherMinPrices];
34+
}
35+
}

tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.php

-5
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,6 @@ public function testBug1216(): void
139139
]);
140140
}
141141

142-
public function testBug1311(): void
143-
{
144-
$this->analyse([__DIR__ . '/data/bug-1311.php'], []);
145-
}
146-
147142
public function testTypesAssignedToPropertiesExpressionNames(): void
148143
{
149144
$this->analyse([__DIR__ . '/data/properties-from-array-into-object.php'], [

tests/PHPStan/Rules/Properties/data/bug-1311.php

-24
This file was deleted.

0 commit comments

Comments
 (0)