[FinalizePublicClassConstantRector] Ignore final classes (#1730)

* Fixed issue with Rector changing const to final when class is already final

* Fixed PHPStan issues

* Applied suggested fix

* Applied suggested fix

* use ->findParentType()

Co-authored-by: Abdul Malik Ikhsan <samsonasik@gmail.com>
This commit is contained in:
ReynierPM 2022-01-26 05:04:44 -05:00 committed by GitHub
parent 7453122793
commit 64d76cd11e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 70 additions and 3 deletions

View File

@ -1,4 +1,4 @@
# 517 Rules Overview
# 519 Rules Overview
<br>
@ -28,11 +28,11 @@
- [DowngradePhp56](#downgradephp56) (5)
- [DowngradePhp70](#downgradephp70) (18)
- [DowngradePhp70](#downgradephp70) (19)
- [DowngradePhp71](#downgradephp71) (10)
- [DowngradePhp72](#downgradephp72) (4)
- [DowngradePhp72](#downgradephp72) (5)
- [DowngradePhp73](#downgradephp73) (6)
@ -4464,6 +4464,29 @@ Downgrade calling a value that is not directly callable in PHP 5 (property, stat
<br>
### DowngradeUnnecessarilyParenthesizedExpressionRector
Remove parentheses around expressions allowed by Uniform variable syntax RFC where they are not necessary to prevent parse errors on PHP 5.
- class: [`Rector\DowngradePhp70\Rector\Expr\DowngradeUnnecessarilyParenthesizedExpressionRector`](../rules/DowngradePhp70/Rector/Expr/DowngradeUnnecessarilyParenthesizedExpressionRector.php)
```diff
-($f)['foo'];
-($f)->foo;
-($f)->foo();
-($f)::$foo;
-($f)::foo();
-($f)();
+$f['foo'];
+$f->foo;
+$f->foo();
+$f::$foo;
+$f::foo();
+$f();
```
<br>
### SplitGroupedUseImportsRector
Refactor grouped use imports to standalone lines
@ -4679,6 +4702,32 @@ Downgrade Symmetric array destructuring to `list()` function
## DowngradePhp72
### DowngradeJsonDecodeNullAssociativeArgRector
Downgrade `json_decode()` with null associative argument function
- class: [`Rector\DowngradePhp72\Rector\FuncCall\DowngradeJsonDecodeNullAssociativeArgRector`](../rules/DowngradePhp72/Rector/FuncCall/DowngradeJsonDecodeNullAssociativeArgRector.php)
```diff
declare(strict_types=1);
function exactlyNull(string $json)
{
- $value = json_decode($json, null);
+ $value = json_decode($json, true);
}
function possiblyNull(string $json, ?bool $associative)
{
+ if ($associative === null) {
+ $associative = true;
+ }
$value = json_decode($json, $associative);
}
```
<br>
### DowngradeObjectTypeDeclarationRector
Remove the "object" param and return type, add a `@param` and `@return` tags instead

View File

@ -0,0 +1,8 @@
<?php
namespace Rector\Tests\Php81\Rector\ClassConst\FinalizePublicClassConstantRector\Fixture;
final class SkipFinal
{
public const NAME = 'value';
}

View File

@ -60,6 +60,16 @@ CODE_SAMPLE
*/
public function refactor(Node $node): ?Node
{
$parentClass = $this->betterNodeFinder->findParentType($node, Node\Stmt\Class_::class);
if (! $parentClass instanceof Node\Stmt\Class_) {
return null;
}
if ($parentClass->isFinal()) {
return null;
}
if ($node->isPrivate()) {
return null;
}