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 crash on array access to undefined class #10134

Merged
merged 1 commit into from Aug 20, 2023
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
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' => [],
'ignored_issues' => ['UndefinedDocblockClass'],
],
];
}

Expand Down