mirror of
https://github.com/danog/psalm.git
synced 2025-01-22 13:51:54 +01:00
Merge pull request #8489 from hirokinoue/fix-array_key_exists-false-positive
Fix array_key_exists first argument false positive
This commit is contained in:
commit
3724a8357a
@ -3653,8 +3653,8 @@ class AssertionFinder
|
||||
)
|
||||
: null;
|
||||
|
||||
if ($array_root) {
|
||||
if ($first_var_name === null && isset($expr->getArgs()[0])) {
|
||||
if ($array_root && isset($expr->getArgs()[0])) {
|
||||
if ($first_var_name === null) {
|
||||
$first_arg = $expr->getArgs()[0];
|
||||
|
||||
if ($first_arg->value instanceof PhpParser\Node\Scalar\String_) {
|
||||
@ -3685,7 +3685,10 @@ class AssertionFinder
|
||||
} else {
|
||||
$first_var_name = null;
|
||||
}
|
||||
} elseif ($expr->getArgs()[0]->value instanceof PhpParser\Node\Expr\Variable
|
||||
} elseif (($expr->getArgs()[0]->value instanceof PhpParser\Node\Expr\Variable
|
||||
|| $expr->getArgs()[0]->value instanceof PhpParser\Node\Expr\PropertyFetch
|
||||
|| $expr->getArgs()[0]->value instanceof PhpParser\Node\Expr\StaticPropertyFetch
|
||||
)
|
||||
&& $source instanceof StatementsAnalyzer
|
||||
&& ($first_var_type = $source->node_data->getType($expr->getArgs()[0]->value))
|
||||
) {
|
||||
|
@ -2,6 +2,8 @@
|
||||
|
||||
namespace Psalm\Tests\TypeReconciliation;
|
||||
|
||||
use Psalm\Config;
|
||||
use Psalm\Context;
|
||||
use Psalm\Tests\TestCase;
|
||||
use Psalm\Tests\Traits\InvalidCodeAnalysisTestTrait;
|
||||
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait;
|
||||
@ -430,4 +432,56 @@ class ArrayKeyExistsTest extends TestCase
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public function testAllowPropertyFetchAsNeedle(): void
|
||||
{
|
||||
Config::getInstance()->ensure_array_int_offsets_exist = true;
|
||||
|
||||
$this->addFile(
|
||||
'somefile.php',
|
||||
'<?php
|
||||
class Foo {
|
||||
/** @var self::STATE_* $status */
|
||||
public int $status = self::STATE_A;
|
||||
public const STATE_A = 0;
|
||||
public const STATE_B = 1;
|
||||
}
|
||||
|
||||
$foo = new Foo;
|
||||
|
||||
/** @var array<string> $bar */
|
||||
$bar = [];
|
||||
|
||||
if (array_key_exists($foo->status, $bar)) {
|
||||
echo $bar[$foo->status];
|
||||
}'
|
||||
);
|
||||
|
||||
$this->analyzeFile('somefile.php', new Context());
|
||||
}
|
||||
|
||||
public function testAllowStaticPropertyFetchAsNeedle(): void
|
||||
{
|
||||
Config::getInstance()->ensure_array_int_offsets_exist = true;
|
||||
|
||||
$this->addFile(
|
||||
'somefile.php',
|
||||
'<?php
|
||||
class Foo {
|
||||
/** @var self::STATE_* $status */
|
||||
public static int $status = self::STATE_A;
|
||||
public const STATE_A = 0;
|
||||
public const STATE_B = 1;
|
||||
}
|
||||
|
||||
/** @var array<string> $bar */
|
||||
$bar = [];
|
||||
|
||||
if (array_key_exists(Foo::$status, $bar)) {
|
||||
echo $bar[Foo::$status];
|
||||
}'
|
||||
);
|
||||
|
||||
$this->analyzeFile('somefile.php', new Context());
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user