Skip to content

Commit

Permalink
[Php74] Skip parenthesize ternary if on ParenthesizeNestedTernaryRect…
Browse files Browse the repository at this point in the history
…or (#3980)

* [Php74] Skip parenthesize ternary if on ParenthesizeNestedTernaryRector

* more fixture

* fixture syntax

* [ci-review] Rector Rectify

* Fixed 🎉

* increase kernel cache key

* fix

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user committed May 26, 2023
1 parent 2f84187 commit 385a9cb
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\Php74\Rector\Ternary\ParenthesizeNestedTernaryRector\Fixture;

function skipParenthesizeTernaryElse(string $message): string
{
$jsonParser = new JsonParser();
return ! $jsonParser->lint($message) instanceof ParsingException
? 'application/problem+json'
: (strip_tags($message) === $message ? 'text/plain' : 'text/html');
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\Php74\Rector\Ternary\ParenthesizeNestedTernaryRector\Fixture;

function skipParenthesizeTernaryIf(string $message): string
{
$jsonParser = new JsonParser();
return $jsonParser->lint($message) instanceof ParsingException
? (strip_tags($message) === $message ? 'text/plain' : 'text/html')
: 'application/problem+json';
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,13 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
if ($node->cond instanceof Ternary || $node->if instanceof Ternary || $node->else instanceof Ternary) {
if ($node->if instanceof Ternary
&& ! $this->parenthesizedNestedTernaryAnalyzer->isParenthesized($this->file, $node)
&& $this->parenthesizedNestedTernaryAnalyzer->isParenthesized($this->file, $node->if)) {
return null;
}

if ($node->cond instanceof Ternary || $node->else instanceof Ternary) {
if ($this->parenthesizedNestedTernaryAnalyzer->isParenthesized($this->file, $node)) {
return null;
}
Expand Down
7 changes: 7 additions & 0 deletions rules/Php74/Tokenizer/ParenthesizedNestedTernaryAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ public function isParenthesized(File $file, Ternary $ternary): bool
$hasOpenParentheses = isset($oldTokens[$startTokenPos]) && $oldTokens[$startTokenPos] === '(';
$hasCloseParentheses = isset($oldTokens[$endTokenPos]) && $oldTokens[$endTokenPos] === ')';

if ($hasOpenParentheses || $hasCloseParentheses) {
return true;
}

$hasOpenParentheses = isset($oldTokens[$startTokenPos - 1]) && $oldTokens[$startTokenPos -1] === '(';
$hasCloseParentheses = isset($oldTokens[$endTokenPos + 1]) && $oldTokens[$endTokenPos + 1] === ')';

return $hasOpenParentheses || $hasCloseParentheses;
}
}
2 changes: 1 addition & 1 deletion src/Kernel/RectorKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class RectorKernel
/**
* @var string
*/
private const CACHE_KEY = 'v33';
private const CACHE_KEY = 'v34';

private ContainerInterface|null $container = null;

Expand Down

0 comments on commit 385a9cb

Please sign in to comment.