Skip to content

Commit

Permalink
[FinalizePublicClassConstantRector] Ignore final classes (#1730)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
reypm and samsonasik committed Jan 26, 2022
1 parent 7453122 commit 64d76cd
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
55 changes: 52 additions & 3 deletions build/target-repository/docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 517 Rules Overview
# 519 Rules Overview

<br>

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Rector\Tests\Php81\Rector\ClassConst\FinalizePublicClassConstantRector\Fixture;

final class SkipFinal
{
public const NAME = 'value';
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ public function getNodeTypes(): array
*/
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;
}
Expand Down

0 comments on commit 64d76cd

Please sign in to comment.