Skip to content
Open
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
5 changes: 5 additions & 0 deletions src/Php/PhpVersions.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@ public function supportsDeprecatedTraits(): TrinaryLogic
return IntegerRangeType::fromInterval(80500, null)->isSuperTypeOf($this->phpVersions)->result;
}

public function supportsDebugInfoOnEnums(): TrinaryLogic
{
return IntegerRangeType::fromInterval(80600, null)->isSuperTypeOf($this->phpVersions)->result;
}

public function arrayFunctionsReturnNullWithNonArray(): TrinaryLogic
{
return IntegerRangeType::fromInterval(null, 79999)->isSuperTypeOf($this->phpVersions)->result;
Expand Down
5 changes: 4 additions & 1 deletion src/Rules/Classes/EnumSanityRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ public function processNode(Node $node, Scope $scope): array
->line($methodNode->getStartLine())
->nonIgnorable()
->build();
} elseif (!array_key_exists($lowercasedMethodName, self::ALLOWED_MAGIC_METHODS)) {
} elseif (
!array_key_exists($lowercasedMethodName, self::ALLOWED_MAGIC_METHODS)
&& ($lowercasedMethodName !== '__debuginfo' || !$scope->getPhpVersion()->supportsDebugInfoOnEnums()->yes())
) {
$errors[] = RuleErrorBuilder::message(sprintf(
'Enum %s contains magic method %s().',
$classReflection->getDisplayName(),
Expand Down
45 changes: 45 additions & 0 deletions tests/PHPStan/Rules/Classes/EnumSanityRuleConfigPhpTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Classes;

use PHPStan\Reflection\InitializerExprTypeResolver;
use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use PHPUnit\Framework\Attributes\RequiresPhp;

/**
* @extends RuleTestCase<EnumSanityRule>
*/
class EnumSanityRuleConfigPhpTest extends RuleTestCase
{

protected function getRule(): Rule
{
return new EnumSanityRule(
self::getContainer()->getByType(InitializerExprTypeResolver::class),
);
}

#[RequiresPhp('>= 8.1.0')]
public function testDebugInfoPhpVersionRangeSpanning86(): void
{
$this->analyse([__DIR__ . '/data/enum-debug-info-php-versions.php'], [
[
'Enum EnumDebugInfoPhpVersions\UnsupportedInBranch contains magic method __debugInfo().',
18,
],
[
'Enum EnumDebugInfoPhpVersions\DependsOnPhpVersion contains magic method __debugInfo().',
27,
],
]);
}

public static function getAdditionalConfigFiles(): array
{
return [
__DIR__ . '/data/enum-debug-info-php-version.neon',
];
}

}
20 changes: 20 additions & 0 deletions tests/PHPStan/Rules/Classes/EnumSanityRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use PHPUnit\Framework\Attributes\RequiresPhp;
use const PHP_VERSION_ID;

/**
* @extends RuleTestCase<EnumSanityRule>
Expand Down Expand Up @@ -190,4 +191,23 @@ public function testBug11891(): void
]);
}

#[RequiresPhp('>= 8.1.0')]
public function testDebugInfoPhpVersions(): void
{
$errors = [
[
'Enum EnumDebugInfoPhpVersions\\UnsupportedInBranch contains magic method __debugInfo().',
18,
],
];
if (PHP_VERSION_ID < 80600) {
$errors[] = [
'Enum EnumDebugInfoPhpVersions\\DependsOnPhpVersion contains magic method __debugInfo().',
27,
];
}

$this->analyse([__DIR__ . '/data/enum-debug-info-php-versions.php'], $errors);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
parameters:
phpVersion:
min: 80500
max: 80600
31 changes: 31 additions & 0 deletions tests/PHPStan/Rules/Classes/data/enum-debug-info-php-versions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php // lint >= 8.1

namespace EnumDebugInfoPhpVersions;

if (PHP_VERSION_ID >= 80600) {
enum SupportedInBranch
{
public function __debugInfo(): array
{
return [];
}
}
}

if (PHP_VERSION_ID < 80600) {
enum UnsupportedInBranch
{
public function __debugInfo(): array
{
return [];
}
}
}

enum DependsOnPhpVersion
{
public function __debugInfo(): array
{
return [];
}
}
Loading