Skip to content

Commit

Permalink
Test that enum is final
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Dec 20, 2021
1 parent a03a78f commit 2ee0aa7
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 2 deletions.
1 change: 1 addition & 0 deletions build/rector-downgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
$parameters->set(Option::PHP_VERSION_FEATURES, $targetPhpVersionId);
$parameters->set(Option::SKIP, [
'tests/*/data/*',
'tests/*/Fixture/*',
'tests/PHPStan/Analyser/traits/*',
'tests/PHPStan/Generics/functions.php',
'tests/e2e/resultCache_1.php',
Expand Down
1 change: 1 addition & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@
</properties>
</rule>
<exclude-pattern>tests/*/data</exclude-pattern>
<exclude-pattern>tests/*/Fixture</exclude-pattern>
<exclude-pattern>tests/e2e/resultCache_1.php</exclude-pattern>
<exclude-pattern>tests/e2e/resultCache_2.php</exclude-pattern>
<exclude-pattern>tests/e2e/resultCache_3.php</exclude-pattern>
Expand Down
7 changes: 5 additions & 2 deletions src/Reflection/ClassReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -945,10 +945,13 @@ public function isInternal(): bool

public function isFinal(): bool
{
if ($this->isFinalByKeyword()) {
return true;
}

if ($this->isFinal === null) {
$resolvedPhpDoc = $this->getResolvedPhpDoc();
$this->isFinal = $this->reflection->isFinal()
|| ($resolvedPhpDoc !== null && $resolvedPhpDoc->isFinal());
$this->isFinal = $resolvedPhpDoc !== null && $resolvedPhpDoc->isFinal();
}

return $this->isFinal;
Expand Down
12 changes: 12 additions & 0 deletions tests/PHPStan/Fixture/TestEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php declare(strict_types = 1); // lint >= 8.1

namespace PHPStan\Fixture;

enum TestEnum: int implements TestEnumInterface
{

case ONE = 1;
case TWO = 2;
const CONST_ONE = 1;

}
8 changes: 8 additions & 0 deletions tests/PHPStan/Fixture/TestEnumInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types = 1); // lint >= 8.1

namespace PHPStan\Fixture;

interface TestEnumInterface
{

}
16 changes: 16 additions & 0 deletions tests/PHPStan/Reflection/ClassReflectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@
use NestedTraits\BazTrait;
use NestedTraits\NoTrait;
use PHPStan\Broker\Broker;
use PHPStan\Fixture\TestEnum;
use PHPStan\Php\PhpVersion;
use PHPStan\PhpDoc\PhpDocInheritanceResolver;
use PHPStan\PhpDoc\StubPhpDocProvider;
use PHPStan\Testing\PHPStanTestCase;
use PHPStan\Type\FileTypeMapper;
use ReflectionClass;
use ReflectionEnum;
use WrongClassConstantFile\SecuredRouter;
use function array_map;
use function array_values;
Expand Down Expand Up @@ -324,4 +326,18 @@ public function dataNestedRecursiveTraits(): array
];
}

public function testEnumIsFinal(): void
{
if (PHP_VERSION_ID < 80100) {
$this->markTestSkipped('Test requires PHP 8.1.');
}

$reflectionProvider = $this->createReflectionProvider();
$enum = $reflectionProvider->getClass(TestEnum::class);
$this->assertTrue($enum->isEnum());
$this->assertInstanceOf(ReflectionEnum::class, $enum->getNativeReflection());
$this->assertTrue($enum->isFinal());
$this->assertTrue($enum->isFinalByKeyword());
}

}

0 comments on commit 2ee0aa7

Please sign in to comment.