Skip to content

Commit

Permalink
Fix crash on array access to undefined class
Browse files Browse the repository at this point in the history
  • Loading branch information
weirdan committed Aug 20, 2023
1 parent 0922fa1 commit 4c58c93
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
Expand Up @@ -1737,8 +1737,10 @@ private static function handleArrayAccessOnNamedObject(
?Union &$array_access_type,
bool &$has_array_access
): void {
$codebase = $statements_analyzer->getCodebase();
if (strtolower($type->value) === 'simplexmlelement'
|| $statements_analyzer->getCodebase()->classExtendsOrImplements($type->value, 'SimpleXMLElement')
|| ($codebase->classExists($type->value)
&& $codebase->classExtendsOrImplements($type->value, 'SimpleXMLElement'))
) {
$call_array_access_type = new Union([new TNull(), new TNamedObject('SimpleXMLElement')]);
} elseif (strtolower($type->value) === 'domnodelist' && $stmt->dim) {
Expand Down
21 changes: 21 additions & 0 deletions tests/ArrayAccessTest.php
Expand Up @@ -1199,6 +1199,27 @@ function f($p): int {
',
'assertions' => ['$a===' => "array{1: 'b'}"],
],
'noCrashOnUnknownClassArrayAccess' => [
'code' => <<<'PHP'
<?php
namespace Psalmtest\Psalmtest;
use SomeMissingClass;
class Test
{
public function f(): void {
/** @var SomeMissingClass */
$result = null;
if ($result['errors'] === true) {}
}
}
PHP,
'assertions' => [],
'error_levels' => ['UndefinedDocblockClass'],
],
];
}

Expand Down

0 comments on commit 4c58c93

Please sign in to comment.