mirror of
https://github.com/danog/psalm.git
synced 2024-11-30 04:39:00 +01:00
Fix #1297 - prevent false positive TypeDoesNotContainType for class const array offset
This commit is contained in:
parent
a6755cc523
commit
e6f3948f7b
@ -803,6 +803,21 @@ class ExpressionAnalyzer
|
||||
$offset = '$' . $stmt->dim->name;
|
||||
} elseif ($stmt->dim instanceof PhpParser\Node\Expr\ConstFetch) {
|
||||
$offset = implode('\\', $stmt->dim->name->parts);
|
||||
} elseif (isset($stmt->dim->inferredType)) {
|
||||
if ($stmt->dim->inferredType->isSingleStringLiteral()) {
|
||||
$offset = '\'' . $stmt->dim->inferredType->getSingleStringLiteral()->value . '\'';
|
||||
} elseif ($stmt->dim->inferredType->isSingleIntLiteral()) {
|
||||
$offset = $stmt->dim->inferredType->getSingleIntLiteral()->value;
|
||||
}
|
||||
} elseif ($stmt->dim instanceof PhpParser\Node\Expr\ClassConstFetch
|
||||
&& $stmt->dim->name instanceof PhpParser\Node\Identifier
|
||||
) {
|
||||
/** @var string|null */
|
||||
$resolved_name = $stmt->dim->class->getAttribute('resolvedName');
|
||||
|
||||
if ($resolved_name) {
|
||||
$offset = $resolved_name . '::' . $stmt->dim->name;
|
||||
}
|
||||
}
|
||||
|
||||
return $root_var_id && $offset !== null ? $root_var_id . '[' . $offset . ']' : null;
|
||||
|
@ -285,6 +285,31 @@ class EmptyTest extends TestCase
|
||||
if (!empty($i)) {}
|
||||
}',
|
||||
],
|
||||
'allowEmptyClassConstantOffsetCheck' => [
|
||||
'<?php
|
||||
class Foo {
|
||||
const BAR = "bar";
|
||||
const ONE = 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string,string> $data
|
||||
*/
|
||||
function bat(array $data) : void {
|
||||
if (!empty($data["foo"])) {
|
||||
if (empty($data[Foo::BAR])) {}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int,string> $data
|
||||
*/
|
||||
function baz(array $data) : void {
|
||||
if (!empty($data[0])) {
|
||||
if (empty($data[Foo::ONE])) {}
|
||||
}
|
||||
}',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user