diff --git a/src/Psalm/Internal/Analyzer/ClassLikeAnalyzer.php b/src/Psalm/Internal/Analyzer/ClassLikeAnalyzer.php index 6693bf7b3..76301097f 100644 --- a/src/Psalm/Internal/Analyzer/ClassLikeAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/ClassLikeAnalyzer.php @@ -183,7 +183,8 @@ abstract class ClassLikeAnalyzer extends SourceAnalyzer implements StatementsSou $fq_class_name, CodeLocation $code_location, array $suppressed_issues, - $inferred = true + $inferred = true, + bool $allow_trait = false ) { $codebase = $statements_source->getCodebase(); if (empty($fq_class_name)) { @@ -233,7 +234,7 @@ abstract class ClassLikeAnalyzer extends SourceAnalyzer implements StatementsSou $interface_exists = $codebase->interfaceExists($fq_class_name); if (!$class_exists && !$interface_exists) { - if (!$codebase->classlikes->traitExists($fq_class_name)) { + if (!$allow_trait || !$codebase->classlikes->traitExists($fq_class_name)) { if (IssueBuffer::accepts( new UndefinedClass( 'Class or interface ' . $fq_class_name . ' does not exist', diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/ConstFetchAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/ConstFetchAnalyzer.php index b04cc83d0..d52356503 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/ConstFetchAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/ConstFetchAnalyzer.php @@ -131,7 +131,8 @@ class ConstFetchAnalyzer $fq_class_name, new CodeLocation($statements_analyzer->getSource(), $stmt->class), $statements_analyzer->getSuppressedIssues(), - false + false, + true ) === false) { return false; } diff --git a/tests/TraitTest.php b/tests/TraitTest.php index 10e42e9fb..66202c342 100644 --- a/tests/TraitTest.php +++ b/tests/TraitTest.php @@ -1024,6 +1024,16 @@ class TraitTest extends TestCase }', 'error_message' => 'InaccessibleMethod' ], + 'preventTraitPropertyType' => [ + ' 'UndefinedClass' + ], ]; } }