Skip to content

Commit 618985c

Browse files
committed
Allow __debugInfo() in enums on PHP 8.6+
Closes phpstan/phpstan#15368
1 parent 0e156a6 commit 618985c

4 files changed

Lines changed: 59 additions & 1 deletion

File tree

‎src/Php/PhpVersions.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,11 @@ public function supportsDeprecatedTraits(): TrinaryLogic
193193
return IntegerRangeType::fromInterval(80500, null)->isSuperTypeOf($this->phpVersions)->result;
194194
}
195195

196+
public function supportsDebugInfoOnEnums(): TrinaryLogic
197+
{
198+
return IntegerRangeType::fromInterval(80600, null)->isSuperTypeOf($this->phpVersions)->result;
199+
}
200+
196201
public function arrayFunctionsReturnNullWithNonArray(): TrinaryLogic
197202
{
198203
return IntegerRangeType::fromInterval(null, 79999)->isSuperTypeOf($this->phpVersions)->result;

‎src/Rules/Classes/EnumSanityRule.php‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,10 @@ public function processNode(Node $node, Scope $scope): array
8080
->line($methodNode->getStartLine())
8181
->nonIgnorable()
8282
->build();
83-
} elseif (!array_key_exists($lowercasedMethodName, self::ALLOWED_MAGIC_METHODS)) {
83+
} elseif (
84+
!array_key_exists($lowercasedMethodName, self::ALLOWED_MAGIC_METHODS)
85+
&& !($lowercasedMethodName === '__debuginfo' && $scope->getPhpVersion()->supportsDebugInfoOnEnums()->yes())
86+
) {
8487
$errors[] = RuleErrorBuilder::message(sprintf(
8588
'Enum %s contains magic method %s().',
8689
$classReflection->getDisplayName(),

‎tests/PHPStan/Rules/Classes/EnumSanityRuleTest.php‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,4 +190,23 @@ public function testBug11891(): void
190190
]);
191191
}
192192

193+
#[RequiresPhp('>= 8.1.0')]
194+
public function testDebugInfoPhpVersions(): void
195+
{
196+
$errors = [
197+
[
198+
'Enum EnumDebugInfoPhpVersions\\UnsupportedInBranch contains magic method __debugInfo().',
199+
18,
200+
],
201+
];
202+
if (PHP_VERSION_ID < 80600) {
203+
$errors[] = [
204+
'Enum EnumDebugInfoPhpVersions\\DependsOnPhpVersion contains magic method __debugInfo().',
205+
27,
206+
];
207+
}
208+
209+
$this->analyse([__DIR__ . '/data/enum-debug-info-php-versions.php'], $errors);
210+
}
211+
193212
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php // lint >= 8.1
2+
3+
namespace EnumDebugInfoPhpVersions;
4+
5+
if (PHP_VERSION_ID >= 80600) {
6+
enum SupportedInBranch
7+
{
8+
public function __debugInfo(): array
9+
{
10+
return [];
11+
}
12+
}
13+
}
14+
15+
if (PHP_VERSION_ID < 80600) {
16+
enum UnsupportedInBranch
17+
{
18+
public function __debugInfo(): array
19+
{
20+
return [];
21+
}
22+
}
23+
}
24+
25+
enum DependsOnPhpVersion
26+
{
27+
public function __debugInfo(): array
28+
{
29+
return [];
30+
}
31+
}

0 commit comments

Comments
 (0)