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

Enums cannot implement Serializable #1713

Merged
merged 3 commits into from Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/Rules/Classes/EnumSanityRule.php
Expand Up @@ -4,9 +4,11 @@

use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\ShouldNotHappenException;
use Serializable;
use function array_key_exists;
use function sprintf;

Expand All @@ -22,6 +24,12 @@ class EnumSanityRule implements Rule
'__invoke' => true,
];

public function __construct(
private ReflectionProvider $reflectionProvider,
)
{
}

public function getNodeType(): string
{
return Node\Stmt\Enum_::class;
Expand Down Expand Up @@ -103,6 +111,17 @@ public function processNode(Node $node, Scope $scope): array
))->line($node->scalarType->getLine())->nonIgnorable()->build();
}

if ($this->reflectionProvider->hasClass($node->namespacedName->toString())) {
$classReflection = $this->reflectionProvider->getClass($node->namespacedName->toString());

if ($classReflection->implementsInterface(Serializable::class)) {
$errors[] = RuleErrorBuilder::message(sprintf(
'Enum %s cannot implement the Serializable interface.',
$node->namespacedName->toString(),
))->line($node->getLine())->nonIgnorable()->build();
}
}

return $errors;
}

Expand Down
10 changes: 7 additions & 3 deletions tests/PHPStan/Rules/Classes/EnumSanityRuleTest.php
Expand Up @@ -14,13 +14,13 @@ class EnumSanityRuleTest extends RuleTestCase

protected function getRule(): Rule
{
return new EnumSanityRule();
return new EnumSanityRule($this->createReflectionProvider());
}

public function testRule(): void
staabm marked this conversation as resolved.
Show resolved Hide resolved
{
if (PHP_VERSION_ID < 80000) {
$this->markTestSkipped('Test requires PHP 8.0');
if (PHP_VERSION_ID < 80100) {
$this->markTestSkipped('Test requires PHP 8.1');
}

$this->analyse([__DIR__ . '/data/enum-sanity.php'], [
Expand Down Expand Up @@ -76,6 +76,10 @@ public function testRule(): void
'Enum EnumSanity\EnumWithSerialize contains magic method __unserialize().',
81,
],
[
'Enum EnumSanity\EnumMayNotSerializable cannot implement the Serializable interface.',
86,
],
]);
}

Expand Down
9 changes: 8 additions & 1 deletion tests/PHPStan/Rules/Classes/data/enum-sanity.php
@@ -1,4 +1,4 @@
<?php
<?php // lint >= 8.1

namespace EnumSanity;

Expand Down Expand Up @@ -83,3 +83,10 @@ public function __unserialize(array $data) {
}
}

enum EnumMayNotSerializable implements \Serializable {

public function serialize() {
}
public function unserialize($data) {
}
}