Skip to content

Commit 866382a

Browse files
authored
Merge pull request #11693 from kkmuffme/trait-class-collision-crash
Report regular issue instead of throw/crash
2 parents 22baf56 + 7d26a5b commit 866382a

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

src/Psalm/Internal/Codebase/Populator.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
namespace Psalm\Internal\Codebase;
66

77
use BackedEnum;
8-
use Exception;
98
use InvalidArgumentException;
109
use Psalm\Internal\Analyzer\ClassLikeAnalyzer;
1110
use Psalm\Internal\MethodIdentifier;
1211
use Psalm\Internal\Provider\ClassLikeStorageProvider;
1312
use Psalm\Internal\Provider\FileReferenceProvider;
1413
use Psalm\Internal\Provider\FileStorageProvider;
1514
use Psalm\Issue\CircularReference;
15+
use Psalm\Issue\UndefinedTrait;
1616
use Psalm\IssueBuffer;
1717
use Psalm\Progress\Progress;
1818
use Psalm\Storage\ClassConstantStorage;
@@ -440,7 +440,7 @@ private function populateDataFromTrait(
440440
$storage->pseudo_property_set_types += $trait_storage->pseudo_property_set_types;
441441

442442
$storage->pseudo_static_methods += $trait_storage->pseudo_static_methods;
443-
443+
444444
$storage->pseudo_methods += $trait_storage->pseudo_methods;
445445
$storage->declaring_pseudo_method_ids += $trait_storage->declaring_pseudo_method_ids;
446446
}
@@ -876,8 +876,29 @@ private function inheritConstantsFromTrait(
876876
ClassLikeStorage $trait_storage,
877877
): void {
878878
if (!$trait_storage->is_trait) {
879-
throw new Exception('Class like storage is not for a trait.');
879+
$location = $storage->location ?? $storage->stmt_location;
880+
if (!$location) {
881+
return;
882+
}
883+
884+
$trait_real_type = 'Class';
885+
if ($trait_storage->is_enum) {
886+
$trait_real_type = 'Enum';
887+
} elseif ($trait_storage->is_interface) {
888+
$trait_real_type = 'Interface';
889+
}
890+
891+
IssueBuffer::maybeAdd(
892+
new UndefinedTrait(
893+
$trait_real_type . ' ' . $trait_storage->name . ' is not a trait',
894+
$location,
895+
),
896+
$storage->suppressed_issues,
897+
);
898+
899+
return;
880900
}
901+
881902
foreach ($trait_storage->constants as $constant_name => $class_constant_storage) {
882903
$trait_alias_map_cased = array_flip($storage->trait_alias_map_cased);
883904
if (isset($trait_alias_map_cased[$constant_name])) {

tests/TraitTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,6 +1071,24 @@ class B {
10711071
}',
10721072
'error_message' => 'UndefinedTrait',
10731073
],
1074+
'isClassNoTrait' => [
1075+
'code' => '<?php
1076+
class B {}
1077+
1078+
class A {
1079+
use B;
1080+
}',
1081+
'error_message' => 'UndefinedTrait',
1082+
],
1083+
'isInterfaceNoTrait' => [
1084+
'code' => '<?php
1085+
Interface B {}
1086+
1087+
class A {
1088+
use B;
1089+
}',
1090+
'error_message' => 'UndefinedTrait',
1091+
],
10741092
'missingPropertyType' => [
10751093
'code' => '<?php
10761094
trait T {

0 commit comments

Comments
 (0)