1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00

Support sizeof alias of count

This commit is contained in:
Javier Sanahuja 2023-02-22 23:11:10 +01:00
parent b0e19045d8
commit 4f83324a91
2 changed files with 24 additions and 8 deletions

View File

@ -1542,7 +1542,7 @@ class AssertionFinder
) {
if ($conditional->left instanceof PhpParser\Node\Expr\FuncCall
&& $conditional->left->name instanceof PhpParser\Node\Name
&& strtolower($conditional->left->name->parts[0]) === 'count'
&& in_array(strtolower($conditional->left->name->parts[0]), ['count', 'sizeof'])
&& $conditional->left->getArgs()
&& ($conditional instanceof BinaryOp\Greater || $conditional instanceof BinaryOp\GreaterOrEqual)
) {
@ -1551,7 +1551,7 @@ class AssertionFinder
$comparison_adjustment = $conditional instanceof BinaryOp\Greater ? 1 : 0;
} elseif ($conditional->right instanceof PhpParser\Node\Expr\FuncCall
&& $conditional->right->name instanceof PhpParser\Node\Name
&& strtolower($conditional->right->name->parts[0]) === 'count'
&& in_array(strtolower($conditional->right->name->parts[0]), ['count', 'sizeof'])
&& $conditional->right->getArgs()
&& ($conditional instanceof BinaryOp\Smaller || $conditional instanceof BinaryOp\SmallerOrEqual)
) {
@ -1584,7 +1584,7 @@ class AssertionFinder
) {
$left_count = $conditional->left instanceof PhpParser\Node\Expr\FuncCall
&& $conditional->left->name instanceof PhpParser\Node\Name
&& strtolower($conditional->left->name->parts[0]) === 'count'
&& in_array(strtolower($conditional->left->name->parts[0]), ['count', 'sizeof'])
&& $conditional->left->getArgs();
$operator_less_than_or_equal =
@ -1603,7 +1603,7 @@ class AssertionFinder
$right_count = $conditional->right instanceof PhpParser\Node\Expr\FuncCall
&& $conditional->right->name instanceof PhpParser\Node\Name
&& strtolower($conditional->right->name->parts[0]) === 'count'
&& in_array(strtolower($conditional->right->name->parts[0]), ['count', 'sizeof'])
&& $conditional->right->getArgs();
$operator_greater_than_or_equal =
@ -1633,7 +1633,7 @@ class AssertionFinder
) {
$left_count = $conditional->left instanceof PhpParser\Node\Expr\FuncCall
&& $conditional->left->name instanceof PhpParser\Node\Name
&& strtolower($conditional->left->name->parts[0]) === 'count'
&& in_array(strtolower($conditional->left->name->parts[0]), ['count', 'sizeof'])
&& $conditional->left->getArgs();
if ($left_count && $conditional->right instanceof PhpParser\Node\Scalar\LNumber) {
@ -1644,7 +1644,7 @@ class AssertionFinder
$right_count = $conditional->right instanceof PhpParser\Node\Expr\FuncCall
&& $conditional->right->name instanceof PhpParser\Node\Name
&& strtolower($conditional->right->name->parts[0]) === 'count'
&& in_array(strtolower($conditional->right->name->parts[0]), ['count', 'sizeof'])
&& $conditional->right->getArgs();
if ($right_count && $conditional->left instanceof PhpParser\Node\Scalar\LNumber) {
@ -1785,7 +1785,7 @@ class AssertionFinder
) {
$left_count = $conditional->left instanceof PhpParser\Node\Expr\FuncCall
&& $conditional->left->name instanceof PhpParser\Node\Name
&& strtolower($conditional->left->name->parts[0]) === 'count';
&& in_array(strtolower($conditional->left->name->parts[0]), ['count', 'sizeof']);
$right_number = $conditional->right instanceof PhpParser\Node\Scalar\LNumber
&& $conditional->right->value === (
@ -2044,7 +2044,7 @@ class AssertionFinder
protected static function hasNonEmptyCountCheck(PhpParser\Node\Expr\FuncCall $stmt): bool
{
return $stmt->name instanceof PhpParser\Node\Name && strtolower($stmt->name->parts[0]) === 'count';
return $stmt->name instanceof PhpParser\Node\Name && in_array(strtolower($stmt->name->parts[0]), ['count', 'sizeof']);
}
protected static function hasArrayKeyExistsCheck(PhpParser\Node\Expr\FuncCall $stmt): bool

View File

@ -145,6 +145,22 @@ class ArrayAccessTest extends TestCase
$this->analyzeFile('somefile.php', new Context());
}
public function testNoIssueWhenUsingArrayValuesOnNonEmptyArrayCheckedWithSizeof(): void
{
Config::getInstance()->ensure_array_int_offsets_exist = true;
$this->addFile(
'somefile.php',
'<?php
/** @param string[][] $arr */
function foo(array $arr) : void {
if (sizeof($arr) === 1 && sizeof(array_values($arr)[0]) === 1) {}
}',
);
$this->analyzeFile('somefile.php', new Context());
}
public function testNoIssueAfterManyIssets(): void
{
Config::getInstance()->ensure_array_int_offsets_exist = true;