Skip to content

Commit 2ee0aa7

Browse files
committed
Test that enum is final
1 parent a03a78f commit 2ee0aa7

File tree

6 files changed

+43
-2
lines changed

6 files changed

+43
-2
lines changed

build/rector-downgrade.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
$parameters->set(Option::PHP_VERSION_FEATURES, $targetPhpVersionId);
2828
$parameters->set(Option::SKIP, [
2929
'tests/*/data/*',
30+
'tests/*/Fixture/*',
3031
'tests/PHPStan/Analyser/traits/*',
3132
'tests/PHPStan/Generics/functions.php',
3233
'tests/e2e/resultCache_1.php',

phpcs.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@
166166
</properties>
167167
</rule>
168168
<exclude-pattern>tests/*/data</exclude-pattern>
169+
<exclude-pattern>tests/*/Fixture</exclude-pattern>
169170
<exclude-pattern>tests/e2e/resultCache_1.php</exclude-pattern>
170171
<exclude-pattern>tests/e2e/resultCache_2.php</exclude-pattern>
171172
<exclude-pattern>tests/e2e/resultCache_3.php</exclude-pattern>

src/Reflection/ClassReflection.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -945,10 +945,13 @@ public function isInternal(): bool
945945

946946
public function isFinal(): bool
947947
{
948+
if ($this->isFinalByKeyword()) {
949+
return true;
950+
}
951+
948952
if ($this->isFinal === null) {
949953
$resolvedPhpDoc = $this->getResolvedPhpDoc();
950-
$this->isFinal = $this->reflection->isFinal()
951-
|| ($resolvedPhpDoc !== null && $resolvedPhpDoc->isFinal());
954+
$this->isFinal = $resolvedPhpDoc !== null && $resolvedPhpDoc->isFinal();
952955
}
953956

954957
return $this->isFinal;

tests/PHPStan/Fixture/TestEnum.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php declare(strict_types = 1); // lint >= 8.1
2+
3+
namespace PHPStan\Fixture;
4+
5+
enum TestEnum: int implements TestEnumInterface
6+
{
7+
8+
case ONE = 1;
9+
case TWO = 2;
10+
const CONST_ONE = 1;
11+
12+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php declare(strict_types = 1); // lint >= 8.1
2+
3+
namespace PHPStan\Fixture;
4+
5+
interface TestEnumInterface
6+
{
7+
8+
}

tests/PHPStan/Reflection/ClassReflectionTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@
2828
use NestedTraits\BazTrait;
2929
use NestedTraits\NoTrait;
3030
use PHPStan\Broker\Broker;
31+
use PHPStan\Fixture\TestEnum;
3132
use PHPStan\Php\PhpVersion;
3233
use PHPStan\PhpDoc\PhpDocInheritanceResolver;
3334
use PHPStan\PhpDoc\StubPhpDocProvider;
3435
use PHPStan\Testing\PHPStanTestCase;
3536
use PHPStan\Type\FileTypeMapper;
3637
use ReflectionClass;
38+
use ReflectionEnum;
3739
use WrongClassConstantFile\SecuredRouter;
3840
use function array_map;
3941
use function array_values;
@@ -324,4 +326,18 @@ public function dataNestedRecursiveTraits(): array
324326
];
325327
}
326328

329+
public function testEnumIsFinal(): void
330+
{
331+
if (PHP_VERSION_ID < 80100) {
332+
$this->markTestSkipped('Test requires PHP 8.1.');
333+
}
334+
335+
$reflectionProvider = $this->createReflectionProvider();
336+
$enum = $reflectionProvider->getClass(TestEnum::class);
337+
$this->assertTrue($enum->isEnum());
338+
$this->assertInstanceOf(ReflectionEnum::class, $enum->getNativeReflection());
339+
$this->assertTrue($enum->isFinal());
340+
$this->assertTrue($enum->isFinalByKeyword());
341+
}
342+
327343
}

0 commit comments

Comments
 (0)