77use PhpParser \Node ;
88use PhpParser \Node \Arg ;
99use PhpParser \Node \Expr ;
10+ use PhpParser \Node \Expr \BinaryOp \BitwiseOr ;
1011use PhpParser \Node \Expr \ConstFetch ;
1112use PhpParser \Node \Expr \FuncCall ;
1213use PhpParser \Node \Identifier ;
2627 */
2728final class JsonThrowOnErrorRector extends AbstractRector implements MinPhpVersionInterface
2829{
29- private bool $ hasChanged = false ;
3030 private const array FLAGS = ['JSON_THROW_ON_ERROR ' ];
3131
32+ private bool $ hasChanged = false ;
33+
3234 public function __construct (
3335 private readonly ValueResolver $ valueResolver ,
3436 private readonly BetterNodeFinder $ betterNodeFinder
@@ -140,11 +142,13 @@ private function processJsonEncode(FuncCall $funcCall): FuncCall
140142 $ arg = $ funcCall ->args [1 ];
141143 $ flags = $ this ->getFlags ($ arg );
142144 }
145+
143146 $ newArg = $ this ->getArgWithFlags ($ flags );
144147 if ($ newArg instanceof Arg) {
145148 $ this ->hasChanged = true ;
146149 $ funcCall ->args [1 ] = $ newArg ;
147150 }
151+
148152 return $ funcCall ;
149153 }
150154
@@ -171,6 +175,7 @@ private function processJsonDecode(FuncCall $funcCall): FuncCall
171175 $ this ->hasChanged = true ;
172176 $ funcCall ->args [3 ] = $ newArg ;
173177 }
178+
174179 return $ funcCall ;
175180 }
176181
@@ -213,10 +218,11 @@ private function getFlags(Expr|Arg $arg, array $flags = []): array
213218 }
214219
215220 // Multiple flags: FLAG_A | FLAG_B | FLAG_C
216- if ($ arg instanceof Node \ Expr \ BinaryOp \ BitwiseOr) {
221+ if ($ arg instanceof BitwiseOr) {
217222 $ flags = $ this ->getFlags ($ arg ->left , $ flags );
218223 $ flags = $ this ->getFlags ($ arg ->right , $ flags );
219224 }
225+
220226 return array_values (array_unique ($ flags )); // array_unique in case the same flag is written multiple times
221227 }
222228
@@ -230,18 +236,17 @@ private function getArgWithFlags(array $flags): ?Arg
230236 if ($ originalCount === count ($ flags )) {
231237 return null ;
232238 }
239+
233240 // Single flag
234241 if (count ($ flags ) === 1 ) {
235242 return new Arg ($ this ->createConstFetch ($ flags [0 ]));
236243 }
244+
237245 // Build FLAG_A | FLAG_B | FLAG_C
238246 $ expr = $ this ->createConstFetch (array_shift ($ flags ));
239247
240248 foreach ($ flags as $ flag ) {
241- $ expr = new Node \Expr \BinaryOp \BitwiseOr (
242- $ expr ,
243- $ this ->createConstFetch ($ flag )
244- );
249+ $ expr = new BitwiseOr ($ expr , $ this ->createConstFetch ($ flag ));
245250 }
246251
247252 return new Arg ($ expr );
0 commit comments