Skip to content

Commit 66e1706

Browse files
committed
allow \Drupal static in enums
fixes #500
1 parent aea0372 commit 66e1706

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

src/Rules/Drupal/GlobalDrupalDependencyInjectionRule.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ public function processNode(Node $node, Scope $scope): array
2929
}
3030
$scopeClassReflection = $scope->getClassReflection();
3131

32+
// Enums cannot have dependency injection.
33+
if ($scopeClassReflection->isEnum()) {
34+
return [];
35+
}
36+
3237
$allowed_list = [
3338
// Ignore tests.
3439
'PHPUnit\Framework\Test',

tests/src/Rules/GlobalDrupalDependencyInjectionRuleTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ public function resultData(): \Generator
5252
__DIR__ . '/../../fixtures/drupal/modules/phpstan_fixtures/src/Entity/ReflectionEntityTest.php',
5353
[],
5454
];
55+
56+
if (PHP_VERSION_ID >= 80100) {
57+
yield [
58+
__DIR__ . '/data/bug-500.php',
59+
[],
60+
];
61+
}
62+
5563
}
5664

5765

tests/src/Rules/data/bug-500.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Bug500;
4+
5+
enum MyEnum {
6+
case ONE;
7+
case TWO;
8+
public function getTitle(): string {
9+
return match($this) {
10+
self::ONE => (string) \Drupal::translation()->translate('One'),
11+
self::TWO => (string) \Drupal::translation()->translate('Two'),
12+
};
13+
}
14+
}

0 commit comments

Comments
 (0)