Skip to content

Commit 815f317

Browse files
authored
Fix count() regression
1 parent 327ac3e commit 815f317

File tree

2 files changed

+80
-2
lines changed

2 files changed

+80
-2
lines changed

src/Analyser/TypeSpecifier.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -1063,12 +1063,12 @@ private function specifyTypesForCountFuncCall(
10631063

10641064
$isConstantArray = $type->isConstantArray();
10651065
$isList = $type->isList();
1066-
$zeroOrMore = IntegerRangeType::fromInterval(0, null);
1066+
$oneOrMore = IntegerRangeType::fromInterval(1, null);
10671067
if (
10681068
!$isNormalCount->yes()
10691069
|| (!$isConstantArray->yes() && !$isList->yes())
10701070
|| $type->isIterableAtLeastOnce()->no() // array{} cannot be used for further narrowing
1071-
|| !$zeroOrMore->isSuperTypeOf($sizeType)->yes()
1071+
|| !$oneOrMore->isSuperTypeOf($sizeType)->yes()
10721072
) {
10731073
return null;
10741074
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace CountConstArray;
4+
5+
use function PHPStan\debugScope;
6+
use function PHPStan\dumpType;
7+
use function PHPStan\Testing\assertType;
8+
9+
final class Foo
10+
{
11+
12+
public function sayHello(): void
13+
{
14+
$expectedDaysResult = [
15+
'2019-01-04' => [
16+
'17:00',
17+
'evening',
18+
],
19+
'2019-01-05' => [
20+
'07:00',
21+
'morning',
22+
],
23+
'2019-01-06' => [
24+
'12:00',
25+
'afternoon',
26+
],
27+
'2019-01-07' => [
28+
'10:00',
29+
'11:00',
30+
'12:00',
31+
'13:00',
32+
'14:00',
33+
'15:00',
34+
'16:00',
35+
'17:00',
36+
'morning',
37+
'afternoon',
38+
'evening',
39+
],
40+
'2019-01-08' => [
41+
'07:00',
42+
'08:00',
43+
'13:00',
44+
'19:00',
45+
'morning',
46+
'afternoon',
47+
'evening',
48+
],
49+
'anyDay' => [
50+
'07:00',
51+
'08:00',
52+
'10:00',
53+
'11:00',
54+
'12:00',
55+
'13:00',
56+
'14:00',
57+
'15:00',
58+
'16:00',
59+
'17:00',
60+
'19:00',
61+
'morning',
62+
'afternoon',
63+
'evening',
64+
],
65+
];
66+
$actualEnabledDays = $this->getEnabledDays();
67+
assert(count($expectedDaysResult) === count($actualEnabledDays));
68+
assertType("array{2019-01-04: array{'17:00', 'evening'}, 2019-01-05: array{'07:00', 'morning'}, 2019-01-06: array{'12:00', 'afternoon'}, 2019-01-07: array{'10:00', '11:00', '12:00', '13:00', '14:00', '15:00', '16:00', '17:00', 'morning', 'afternoon', 'evening'}, 2019-01-08: array{'07:00', '08:00', '13:00', '19:00', 'morning', 'afternoon', 'evening'}, anyDay: array{'07:00', '08:00', '10:00', '11:00', '12:00', '13:00', '14:00', '15:00', '16:00', '17:00', '19:00', 'morning', 'afternoon', 'evening'}}", $expectedDaysResult);
69+
}
70+
71+
/**
72+
* @return array<string, array<int, string>>
73+
*/
74+
private function getEnabledDays(): array
75+
{
76+
return [];
77+
}
78+
}

0 commit comments

Comments
 (0)