Skip to content

Commit

Permalink
Merge pull request #10599 from weirdan/allow-properties-on-intersecti…
Browse files Browse the repository at this point in the history
…ons-with-enum-interfaces
  • Loading branch information
weirdan authored Jan 29, 2024
2 parents 8354ff3 + c1e22dd commit fd1294e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1141,6 +1141,8 @@ private static function handleNonExistentClass(

$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)
Expand All @@ -1149,12 +1151,19 @@ private static function handleNonExistentClass(
$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(
Expand Down
27 changes: 27 additions & 0 deletions tests/EnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,33 @@ enum BarEnum: int {
'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',
],
];
}

Expand Down

0 comments on commit fd1294e

Please sign in to comment.