1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 05:41:20 +01:00

Merge pull request #10599 from weirdan/allow-properties-on-intersections-with-enum-interfaces

This commit is contained in:
Bruce Weirdan 2024-01-29 16:23:20 -04:00 committed by GitHub
commit fd1294eef4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 37 additions and 1 deletions

View File

@ -1141,6 +1141,8 @@ final class AtomicPropertyFetchAnalyzer
$override_property_visibility = $interface_storage->override_property_visibility;
$intersects_with_enum = false;
foreach ($intersection_types as $intersection_type) {
if ($intersection_type instanceof TNamedObject
&& $codebase->classExists($intersection_type->value)
@ -1149,12 +1151,19 @@ final class AtomicPropertyFetchAnalyzer
$class_exists = true;
return;
}
if ($intersection_type instanceof TNamedObject
&& (in_array($intersection_type->value, ['UnitEnum', 'BackedEnum'], true)
|| in_array('UnitEnum', $codebase->getParentInterfaces($intersection_type->value)))
) {
$intersects_with_enum = true;
}
}
if (!$class_exists &&
//interfaces can't have properties. Except when they do... In PHP Core, they can
!in_array($fq_class_name, ['UnitEnum', 'BackedEnum'], true) &&
!in_array('UnitEnum', $codebase->getParentInterfaces($fq_class_name))
!in_array('UnitEnum', $codebase->getParentInterfaces($fq_class_name)) &&
!$intersects_with_enum
) {
if (IssueBuffer::accepts(
new NoInterfaceProperties(

View File

@ -630,6 +630,33 @@ class EnumTest extends TestCase
'ignored_issues' => [],
'php_version' => '8.1',
],
'allowPropertiesOnIntersectionsWithEnumInterfaces' => [
'code' => <<<'PHP'
<?php
interface I {}
interface UE extends UnitEnum {}
interface BE extends BackedEnum {}
function f(I $i): void {
if ($i instanceof BackedEnum) {
echo $i->name;
}
if ($i instanceof UnitEnum) {
echo $i->name;
}
if ($i instanceof UE) {
echo $i->name;
}
if ($i instanceof BE) {
echo $i->name;
}
}
PHP,
'assertions' => [],
'ignored_issues' => [],
'php_version' => '8.1',
],
];
}