From b3150d451aa6356cfab43704cd7f5fe4e285025b Mon Sep 17 00:00:00 2001 From: Bruce Weirdan Date: Mon, 28 Aug 2023 01:58:02 +0200 Subject: [PATCH] Fix crash on property access to undefined property on a class with a missing dependncy --- src/Psalm/Storage/ClassLikeStorage.php | 4 ++++ .../Fetch/AtomicPropertyFetchAnalyzerTest.php | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/Psalm/Storage/ClassLikeStorage.php b/src/Psalm/Storage/ClassLikeStorage.php index 5be0d5221..f8564444a 100644 --- a/src/Psalm/Storage/ClassLikeStorage.php +++ b/src/Psalm/Storage/ClassLikeStorage.php @@ -501,6 +501,10 @@ final class ClassLikeStorage implements HasAttributesInterface } foreach ($this->parent_classes as $parent_class) { + // skip missing dependencies + if (!$codebase->classlike_storage_provider->has($parent_class)) { + continue; + } $parent_class_storage = $codebase->classlike_storage_provider->get($parent_class); if ($parent_class_storage->hasAttribute($fq_class_name)) { return true; diff --git a/tests/Internal/Analyzer/Statements/Expression/Fetch/AtomicPropertyFetchAnalyzerTest.php b/tests/Internal/Analyzer/Statements/Expression/Fetch/AtomicPropertyFetchAnalyzerTest.php index 250f52dae..e34a9ab87 100644 --- a/tests/Internal/Analyzer/Statements/Expression/Fetch/AtomicPropertyFetchAnalyzerTest.php +++ b/tests/Internal/Analyzer/Statements/Expression/Fetch/AtomicPropertyFetchAnalyzerTest.php @@ -5,11 +5,13 @@ declare(strict_types=1); namespace Psalm\Tests\Internal\Analyzer\Statements\Expression\Fetch; use Psalm\Tests\TestCase; +use Psalm\Tests\Traits\InvalidCodeAnalysisTestTrait; use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait; final class AtomicPropertyFetchAnalyzerTest extends TestCase { use ValidCodeAnalysisTestTrait; + use InvalidCodeAnalysisTestTrait; public function providerValidCodeParse(): iterable { @@ -72,4 +74,21 @@ final class AtomicPropertyFetchAnalyzerTest extends TestCase ], ]; } + + public function providerInvalidCodeParse(): iterable + { + return [ + 'undefinedPropertyAccessOnMissingDependency' => [ + 'code' => <<<'PHP' + prop; + PHP, + 'error_message' => 'UndefinedPropertyFetch', + 'ignored_issues' => ['MissingDependency'], + ], + ]; + } }