mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-07 11:50:51 +00:00
[Core][EarlyReturn][Php73] Fix used along tweak between JsonThrowOnErrorRector + ChangeAndIfToEarlyReturnRector (#1979)
Co-authored-by: GitHub Action <action@github.com>
This commit is contained in:
parent
2a6403e5df
commit
a375ee2641
|
@ -7,7 +7,6 @@ namespace Rector\EarlyReturn\Rector\If_;
|
|||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
|
||||
use PhpParser\Node\Expr\FuncCall;
|
||||
use PhpParser\Node\Stmt\Break_;
|
||||
use PhpParser\Node\Stmt\Continue_;
|
||||
use PhpParser\Node\Stmt\Else_;
|
||||
|
@ -127,20 +126,6 @@ CODE_SAMPLE
|
|||
return $this->processReplaceIfs($node, $booleanAndConditions, $ifNextReturnClone, $afters);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Node[] $nodes
|
||||
*/
|
||||
private function hasJsonEncodeOrJsonDecode(array $nodes): bool
|
||||
{
|
||||
return (bool) $this->betterNodeFinder->findFirst($nodes, function (Node $subNode): bool {
|
||||
if (! $subNode instanceof FuncCall) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->nodeNameResolver->isNames($subNode, ['json_encode', 'json_decode']);
|
||||
});
|
||||
}
|
||||
|
||||
private function isInLoopWithoutContinueOrBreak(If_ $if): bool
|
||||
{
|
||||
if (! $this->contextAnalyzer->isInLoop($if)) {
|
||||
|
@ -157,19 +142,14 @@ CODE_SAMPLE
|
|||
/**
|
||||
* @param Expr[] $conditions
|
||||
* @param Node[] $afters
|
||||
* @return Node[]|null
|
||||
* @return Node[]
|
||||
*/
|
||||
private function processReplaceIfs(
|
||||
If_ $if,
|
||||
array $conditions,
|
||||
Return_ $ifNextReturnClone,
|
||||
array $afters
|
||||
): ?array {
|
||||
// handle for used along with JsonThrowOnErrorRector
|
||||
if ($this->hasJsonEncodeOrJsonDecode($afters)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
): array {
|
||||
$ifs = $this->invertedIfFactory->createFromConditions($if, $conditions, $ifNextReturnClone);
|
||||
$this->mirrorComments($ifs[0], $if);
|
||||
|
||||
|
|
|
@ -5,9 +5,11 @@ declare(strict_types=1);
|
|||
namespace Rector\Core\ProcessAnalyzer;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt;
|
||||
use Rector\Core\Contract\Rector\RectorInterface;
|
||||
use Rector\Core\ValueObject\Application\File;
|
||||
use Rector\Core\ValueObject\RectifiedNode;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
|
||||
/**
|
||||
* This service verify if the Node already rectified with same Rector rule before current Rector rule with condition
|
||||
|
@ -33,11 +35,7 @@ final class RectifiedAnalyzer
|
|||
|
||||
/** @var RectifiedNode $rectifiedNode */
|
||||
$rectifiedNode = $this->previousFileWithNodes[$realPath];
|
||||
if ($rectifiedNode->getRectorClass() !== $rector::class) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($rectifiedNode->getNode() !== $node) {
|
||||
if ($this->shouldContinue($rectifiedNode, $rector, $node)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -45,4 +43,25 @@ final class RectifiedAnalyzer
|
|||
$this->previousFileWithNodes[$realPath] = null;
|
||||
return $rectifiedNode;
|
||||
}
|
||||
|
||||
private function shouldContinue(RectifiedNode $rectifiedNode, RectorInterface $rector, Node $node): bool
|
||||
{
|
||||
if ($rectifiedNode->getRectorClass() === $rector::class && $rectifiedNode->getNode() === $node) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($node instanceof Stmt) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$stmt = $node->getAttribute(AttributeKey::CURRENT_STATEMENT);
|
||||
if ($stmt instanceof Stmt) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$startTokenPos = $node->getStartTokenPos();
|
||||
$endTokenPos = $node->getEndTokenPos();
|
||||
|
||||
return $startTokenPos < 0 || $endTokenPos < 0;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,9 +17,13 @@ namespace Rector\Core\Tests\Issues\JsonThrowWithChangeAndIf\Fixture;
|
|||
|
||||
function f()
|
||||
{
|
||||
if (true && true) {
|
||||
json_decode($a, null, 512, JSON_THROW_ON_ERROR);
|
||||
if (!true) {
|
||||
return;
|
||||
}
|
||||
if (!true) {
|
||||
return;
|
||||
}
|
||||
json_decode($a, null, 512, JSON_THROW_ON_ERROR);
|
||||
}
|
||||
|
||||
?>
|
||||
|
|
Loading…
Reference in New Issue
Block a user