Skip to content

Commit fc7bcff

Browse files
authored
sizeof() is an alias of count()
1 parent 7340657 commit fc7bcff

File tree

8 files changed

+62
-10
lines changed

8 files changed

+62
-10
lines changed

src/Analyser/TypeSpecifier.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ public function specifyTypesInCondition(
206206
&& $exprNode instanceof FuncCall
207207
&& count($exprNode->args) === 1
208208
&& $exprNode->name instanceof Name
209-
&& strtolower((string) $exprNode->name) === 'count'
209+
&& in_array(strtolower((string) $exprNode->name), ['count', 'sizeof'], true)
210210
&& $constantType instanceof ConstantIntegerType
211211
) {
212212
if ($context->truthy() || $constantType->getValue() === 0) {

src/Rules/Comparison/ImpossibleCheckTypeHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public function findSpecifiedType(
7878
], true)) {
7979
return null;
8080
}
81-
if ($functionName === 'count') {
81+
if (in_array($functionName, ['count', 'sizeof'], true)) {
8282
return null;
8383
} elseif ($functionName === 'defined') {
8484
return null;

src/Type/Php/CountFunctionReturnTypeExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class CountFunctionReturnTypeExtension implements \PHPStan\Type\DynamicFunctionR
1717

1818
public function isFunctionSupported(FunctionReflection $functionReflection): bool
1919
{
20-
return $functionReflection->getName() === 'count';
20+
return in_array($functionReflection->getName(), ['sizeof', 'count'], true);
2121
}
2222

2323
public function getTypeFromFunctionCall(

src/Type/Php/CountFunctionTypeSpecifyingExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function isFunctionSupported(
2727
{
2828
return !$context->null()
2929
&& count($node->args) >= 1
30-
&& $functionReflection->getName() === 'count';
30+
&& in_array($functionReflection->getName(), ['sizeof', 'count'], true);
3131
}
3232

3333
public function specifyTypes(

tests/PHPStan/Analyser/TypeSpecifierTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,28 @@ public function dataCondition(): array
584584
'$array' => 'nonEmpty',
585585
],
586586
],
587+
[
588+
new FuncCall(new Name('sizeof'), [
589+
new Arg(new Variable('array')),
590+
]),
591+
[
592+
'$array' => 'nonEmpty',
593+
],
594+
[
595+
'$array' => '~nonEmpty',
596+
],
597+
],
598+
[
599+
new BooleanNot(new FuncCall(new Name('sizeof'), [
600+
new Arg(new Variable('array')),
601+
])),
602+
[
603+
'$array' => '~nonEmpty',
604+
],
605+
[
606+
'$array' => 'nonEmpty',
607+
],
608+
],
587609
[
588610
new Variable('foo'),
589611
[

tests/PHPStan/Analyser/data/count-type.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public function doFoo(
1515
)
1616
{
1717
assertType('int<1, max>', count($nonEmpty));
18+
assertType('int<1, max>', sizeof($nonEmpty));
1819
}
1920

2021
}

tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -452,27 +452,27 @@ public function testForeach(): void
452452
],
453453
[
454454
'Undefined variable: $val',
455-
171,
455+
200,
456456
],
457457
[
458458
'Undefined variable: $test',
459-
172,
459+
201,
460460
],
461461
[
462462
'Undefined variable: $val',
463-
187,
463+
216,
464464
],
465465
[
466466
'Undefined variable: $test',
467-
188,
467+
217,
468468
],
469469
[
470470
'Variable $val might not be defined.',
471-
217,
471+
246,
472472
],
473473
[
474474
'Variable $test might not be defined.',
475-
218,
475+
247,
476476
],
477477
]);
478478
}

tests/PHPStan/Rules/Variables/data/foreach.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,35 @@ function (array $arr) {
107107

108108
};
109109

110+
function (array $arr) {
111+
112+
if (sizeof($arr) === 0) {
113+
return;
114+
}
115+
116+
foreach ($arr as $val) {
117+
$test = 1;
118+
}
119+
120+
echo $val;
121+
echo $test;
122+
123+
};
124+
125+
function (array $arr) {
126+
127+
if (sizeof($arr) === 0) {
128+
return;
129+
}
130+
131+
if ($arr) {
132+
$test = 1;
133+
}
134+
135+
echo $test;
136+
137+
};
138+
110139
/*function (array $arr) {
111140
112141
if (count($arr) > 0) {

0 commit comments

Comments
 (0)