diff --git a/rules/Php73/Rector/FuncCall/JsonThrowOnErrorRector.php b/rules/Php73/Rector/FuncCall/JsonThrowOnErrorRector.php index b7b3f2ac5d2..38342c24806 100644 --- a/rules/Php73/Rector/FuncCall/JsonThrowOnErrorRector.php +++ b/rules/Php73/Rector/FuncCall/JsonThrowOnErrorRector.php @@ -7,6 +7,7 @@ use PhpParser\Node; use PhpParser\Node\Arg; use PhpParser\Node\Expr; +use PhpParser\Node\Expr\BinaryOp\BitwiseOr; use PhpParser\Node\Expr\ConstFetch; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Identifier; @@ -26,9 +27,10 @@ */ final class JsonThrowOnErrorRector extends AbstractRector implements MinPhpVersionInterface { - private bool $hasChanged = false; private const array FLAGS = ['JSON_THROW_ON_ERROR']; + private bool $hasChanged = false; + public function __construct( private readonly ValueResolver $valueResolver, private readonly BetterNodeFinder $betterNodeFinder @@ -140,11 +142,13 @@ private function processJsonEncode(FuncCall $funcCall): FuncCall $arg = $funcCall->args[1]; $flags = $this->getFlags($arg); } + $newArg = $this->getArgWithFlags($flags); if ($newArg instanceof Arg) { $this->hasChanged = true; $funcCall->args[1] = $newArg; } + return $funcCall; } @@ -171,6 +175,7 @@ private function processJsonDecode(FuncCall $funcCall): FuncCall $this->hasChanged = true; $funcCall->args[3] = $newArg; } + return $funcCall; } @@ -213,10 +218,11 @@ private function getFlags(Expr|Arg $arg, array $flags = []): array } // Multiple flags: FLAG_A | FLAG_B | FLAG_C - if ($arg instanceof Node\Expr\BinaryOp\BitwiseOr) { + if ($arg instanceof BitwiseOr) { $flags = $this->getFlags($arg->left, $flags); $flags = $this->getFlags($arg->right, $flags); } + return array_values(array_unique($flags)); // array_unique in case the same flag is written multiple times } @@ -230,18 +236,17 @@ private function getArgWithFlags(array $flags): ?Arg if ($originalCount === count($flags)) { return null; } + // Single flag if (count($flags) === 1) { return new Arg($this->createConstFetch($flags[0])); } + // Build FLAG_A | FLAG_B | FLAG_C $expr = $this->createConstFetch(array_shift($flags)); foreach ($flags as $flag) { - $expr = new Node\Expr\BinaryOp\BitwiseOr( - $expr, - $this->createConstFetch($flag) - ); + $expr = new BitwiseOr($expr, $this->createConstFetch($flag)); } return new Arg($expr); diff --git a/src/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php b/src/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php index e7427f62abb..b5e27242e6d 100644 --- a/src/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php +++ b/src/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php @@ -333,11 +333,6 @@ public function processNodes( return; } - if ($node instanceof ArrayItem) { - $this->processArrayItem($node, $mutatingScope); - return; - } - if ($node instanceof NullableType) { $node->type->setAttribute(AttributeKey::SCOPE, $mutatingScope); return;