Skip to content

Commit

Permalink
[Php83] add type to final class constant (#5662)
Browse files Browse the repository at this point in the history
Since PHP8.1 it has been possible to mark a class constant as final.
A "final public const FOO" should be treated the same as
a "public const FOO" within a "final class" and the type should be
added, which isn't currently the case.
  • Loading branch information
philbates35 committed Feb 24, 2024
1 parent 3ef3e35 commit b6f5a03
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
@@ -0,0 +1,45 @@
<?php

namespace Rector\Tests\Php83\Rector\ClassConst\AddTypeToConstRector\Fixture;

class ApplyTypesWhenConstFinal
{
final public const STRING = 'some_type';
final public const INT = 1;
final public const FLOAT = 1.0;
final public const BOOL = true;
final public const NULL = null;
final public const ARRAY = [];
final public const CONCAT = self::STRING . 'concat';
}

?>
-----
<?php

namespace Rector\Tests\Php83\Rector\ClassConst\AddTypeToConstRector\Fixture;

class ApplyTypesWhenConstFinal
{
final public const string STRING = 'some_type';
final public const int INT = 1;
final public const float FLOAT = 1.0;
final public const bool BOOL = true;
final public const null NULL = null;
final public const array ARRAY = [];
final public const string CONCAT = self::STRING . 'concat';
}

?>
2 changes: 1 addition & 1 deletion rules/Php83/Rector/ClassConst/AddTypeToConstRector.php
Expand Up @@ -191,6 +191,6 @@ private function getParentReflections(string $className): array

private function canBeInherited(ClassConst $classConst, Class_ $class): bool
{
return ! $class->isFinal() && ! $classConst->isPrivate();
return ! $class->isFinal() && ! $classConst->isPrivate() && !$classConst->isFinal();
}
}

0 comments on commit b6f5a03

Please sign in to comment.