Skip to content

Commit

Permalink
Fix infinite recursion with count() calls in TypeSpecifier
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Dec 31, 2020
1 parent 3d6461c commit 69e68a7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,11 @@ public function specifyTypesInCondition(
&& $expr->left->name instanceof Name
&& strtolower((string) $expr->left->name) === 'count'
&& $rightType instanceof ConstantIntegerType
&& (
!$expr->right instanceof FuncCall
|| !$expr->right->name instanceof Name
|| strtolower((string) $expr->right->name) !== 'count'
)
) {
$inverseOperator = $expr instanceof Node\Expr\BinaryOp\Smaller
? new Node\Expr\BinaryOp\SmallerOrEqual($expr->right, $expr->left)
Expand Down
8 changes: 8 additions & 0 deletions tests/PHPStan/Analyser/AnalyserIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,14 @@ public function testBug4097(): void
$this->assertCount(0, $errors);
}

public function testBug4300(): void
{
$errors = $this->runAnalyse(__DIR__ . '/data/bug-4300.php');
$this->assertCount(1, $errors);
$this->assertSame('Comparison operation ">" between 0 and 0 is always false.', $errors[0]->getMessage());
$this->assertSame(13, $errors[0]->getLine());
}

/**
* @param string $file
* @return \PHPStan\Analyser\Error[]
Expand Down
18 changes: 18 additions & 0 deletions tests/PHPStan/Analyser/data/bug-4300.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php declare(strict_types=1);

namespace Bug4300;

class Widget
{

public function getDefault(): int
{
$column1 = [];
$column2 = [];

$column = count($column1) > count($column2) ? 2 : 1;

return $column;
}

}

0 comments on commit 69e68a7

Please sign in to comment.