Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix array_key_exists first argument false positive #8489

Merged
merged 5 commits into from
Sep 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3653,8 +3653,8 @@ private static function getArrayKeyExistsAssertions(
)
: 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_) {
Expand Down Expand Up @@ -3685,7 +3685,10 @@ private static function getArrayKeyExistsAssertions(
} 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))
) {
Expand Down
54 changes: 54 additions & 0 deletions tests/TypeReconciliation/ArrayKeyExistsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -430,4 +432,56 @@ function go(array $options): void {
],
];
}

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());
}
}