Skip to content

Commit 69e68a7

Browse files
committed
Fix infinite recursion with count() calls in TypeSpecifier
1 parent 3d6461c commit 69e68a7

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

src/Analyser/TypeSpecifier.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,11 @@ public function specifyTypesInCondition(
376376
&& $expr->left->name instanceof Name
377377
&& strtolower((string) $expr->left->name) === 'count'
378378
&& $rightType instanceof ConstantIntegerType
379+
&& (
380+
!$expr->right instanceof FuncCall
381+
|| !$expr->right->name instanceof Name
382+
|| strtolower((string) $expr->right->name) !== 'count'
383+
)
379384
) {
380385
$inverseOperator = $expr instanceof Node\Expr\BinaryOp\Smaller
381386
? new Node\Expr\BinaryOp\SmallerOrEqual($expr->right, $expr->left)

tests/PHPStan/Analyser/AnalyserIntegrationTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,14 @@ public function testBug4097(): void
291291
$this->assertCount(0, $errors);
292292
}
293293

294+
public function testBug4300(): void
295+
{
296+
$errors = $this->runAnalyse(__DIR__ . '/data/bug-4300.php');
297+
$this->assertCount(1, $errors);
298+
$this->assertSame('Comparison operation ">" between 0 and 0 is always false.', $errors[0]->getMessage());
299+
$this->assertSame(13, $errors[0]->getLine());
300+
}
301+
294302
/**
295303
* @param string $file
296304
* @return \PHPStan\Analyser\Error[]
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Bug4300;
4+
5+
class Widget
6+
{
7+
8+
public function getDefault(): int
9+
{
10+
$column1 = [];
11+
$column2 = [];
12+
13+
$column = count($column1) > count($column2) ? 2 : 1;
14+
15+
return $column;
16+
}
17+
18+
}

0 commit comments

Comments
 (0)