-
-
Notifications
You must be signed in to change notification settings - Fork 333
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Php74] Add ParenthesizeNestedTernaryRector (#2859)
* [Php74] Add ParenthesizeNestedTernaryRector * phpstan * [ci-review] Rector Rectify * verify from parent * different fixture files for detection * phpstan * more explanation * more explanation * rename fixture * more explanation * clean up comment * regenerate docs Co-authored-by: GitHub Action <action@github.com>
- Loading branch information
1 parent
86ccf3f
commit 6db4c0e
Showing
12 changed files
with
231 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
rules-tests/Php74/Rector/Ternary/ParenthesizeNestedTernaryRector/Fixture/fixture.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php74\Rector\Ternary\ParenthesizeNestedTernaryRector\Fixture; | ||
|
||
function nestedTernaryNoParentheses() | ||
{ | ||
$a = 1; | ||
$b = 2; | ||
$c = 3; | ||
|
||
$value = $a ? $b : $a ?: null; | ||
$value = $a ? $b : $a ? $c : null; | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\Php74\Rector\Ternary\ParenthesizeNestedTernaryRector\Fixture; | ||
|
||
function nestedTernaryNoParentheses() | ||
{ | ||
$a = 1; | ||
$b = 2; | ||
$c = 3; | ||
|
||
$value = ($a ? $b : $a) ?: null; | ||
$value = ($a ? $b : $a) ? $c : null; | ||
} | ||
|
||
?> |
11 changes: 11 additions & 0 deletions
11
...Ternary/ParenthesizeNestedTernaryRector/Fixture/skip_nested_ternary_parenthesized.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php74\Rector\Ternary\ParenthesizeNestedTernaryRector\Fixture; | ||
|
||
function SkipNestedTernaryParenthesized() | ||
{ | ||
$a = 1; | ||
$b = 2; | ||
|
||
$value = ($a ? $b : $a) ?: null; | ||
} |
8 changes: 8 additions & 0 deletions
8
...ry/ParenthesizeNestedTernaryRector/Fixture/skip_nested_ternary_parenthesized_else.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php74\Rector\Ternary\ParenthesizeNestedTernaryRector\Fixture; | ||
|
||
function SkipNestedTernaryParenthesizedElse() | ||
{ | ||
1 ? 2 : (3 ? 4 : 5); | ||
} |
8 changes: 8 additions & 0 deletions
8
...nary/ParenthesizeNestedTernaryRector/Fixture/skip_nested_ternary_parenthesized_if.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php74\Rector\Ternary\ParenthesizeNestedTernaryRector\Fixture; | ||
|
||
function SkipNestedTernaryParenthesizedIf() | ||
{ | ||
(1 ? 2 : 3) ? 4 : 5; | ||
} |
11 changes: 11 additions & 0 deletions
11
...74/Rector/Ternary/ParenthesizeNestedTernaryRector/Fixture/skip_not_nested_ternary.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php74\Rector\Ternary\ParenthesizeNestedTernaryRector\Fixture; | ||
|
||
function skipNotNestedTernary() | ||
{ | ||
$a = 1; | ||
$b = 2; | ||
|
||
$value = $a ? $b : $a; | ||
} |
33 changes: 33 additions & 0 deletions
33
...74/Rector/Ternary/ParenthesizeNestedTernaryRector/ParenthesizeNestedTernaryRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\Php74\Rector\Ternary\ParenthesizeNestedTernaryRector; | ||
|
||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
use Symplify\SmartFileSystem\SmartFileInfo; | ||
|
||
final class ParenthesizeNestedTernaryRectorTest extends AbstractRectorTestCase | ||
{ | ||
/** | ||
* @dataProvider provideData() | ||
*/ | ||
public function test(SmartFileInfo $fileInfo): void | ||
{ | ||
$this->doTestFileInfo($fileInfo); | ||
} | ||
|
||
/** | ||
* @return Iterator<SmartFileInfo> | ||
*/ | ||
public function provideData(): Iterator | ||
{ | ||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
rules-tests/Php74/Rector/Ternary/ParenthesizeNestedTernaryRector/config/configured_rule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use Rector\Php74\Rector\Ternary\ParenthesizeNestedTernaryRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(ParenthesizeNestedTernaryRector::class); | ||
}; |
77 changes: 77 additions & 0 deletions
77
rules/Php74/Rector/Ternary/ParenthesizeNestedTernaryRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Php74\Rector\Ternary; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\Ternary; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Rector\Core\ValueObject\PhpVersionFeature; | ||
use Rector\NodeTypeResolver\Node\AttributeKey; | ||
use Rector\Php74\Tokenizer\ParenthesizedNestedTernaryAnalyzer; | ||
use Rector\VersionBonding\Contract\MinPhpVersionInterface; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @changelog https://www.php.net/manual/en/migration74.deprecated.php | ||
* @changelog https://3v4l.org/vhdlJ | ||
* @see \Rector\Tests\Php74\Rector\Ternary\ParenthesizeNestedTernaryRector\ParenthesizeNestedTernaryRectorTest | ||
*/ | ||
final class ParenthesizeNestedTernaryRector extends AbstractRector implements MinPhpVersionInterface | ||
{ | ||
public function __construct(private readonly ParenthesizedNestedTernaryAnalyzer $parenthesizedNestedTernaryAnalyzer) | ||
{ | ||
} | ||
|
||
public function provideMinPhpVersion(): int | ||
{ | ||
return PhpVersionFeature::DEPRECATE_NESTED_TERNARY; | ||
} | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition( | ||
'Add parentheses to nested ternary', | ||
[ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
$value = $a ? $b : $a ?: null; | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
$value = ($a ? $b : $a) ?: null; | ||
CODE_SAMPLE | ||
), | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [Ternary::class]; | ||
} | ||
|
||
/** | ||
* @param Ternary $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE); | ||
|
||
if (! $parentNode instanceof Ternary) { | ||
return null; | ||
} | ||
|
||
if ($this->parenthesizedNestedTernaryAnalyzer->isParenthesized($this->file, $parentNode)) { | ||
return null; | ||
} | ||
|
||
$node->setAttribute(AttributeKey::ORIGINAL_NODE, null); | ||
return $node; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
rules/Php74/Tokenizer/ParenthesizedNestedTernaryAnalyzer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Php74\Tokenizer; | ||
|
||
use PhpParser\Node\Expr\Ternary; | ||
use Rector\Core\ValueObject\Application\File; | ||
|
||
final class ParenthesizedNestedTernaryAnalyzer | ||
{ | ||
public function isParenthesized(File $file, Ternary $ternary): bool | ||
{ | ||
$oldTokens = $file->getOldTokens(); | ||
$startTokenPos = $ternary->getStartTokenPos(); | ||
|
||
return isset($oldTokens[$startTokenPos]) && $oldTokens[$startTokenPos] === '('; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters