Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow properties on intersections with enum interfaces #10599

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading