55
66use PhpParser \Node ;
77use PhpParser \Node \Arg ;
8- use PhpParser \Node \Expr ;
98use PhpParser \Node \Expr \ConstFetch ;
109use PhpParser \Node \Expr \FuncCall ;
1110use PhpParser \Node \Identifier ;
@@ -33,10 +32,6 @@ final class JsonThrowOnErrorRector extends AbstractRector implements MinPhpVersi
3332 */
3433 private BetterNodeFinder $ betterNodeFinder ;
3534 private bool $ hasChanged = \false;
36- /**
37- * @var mixed[]
38- */
39- private const FLAGS = ['JSON_THROW_ON_ERROR ' ];
4035 public function __construct (ValueResolver $ valueResolver , BetterNodeFinder $ betterNodeFinder )
4136 {
4237 $ this ->valueResolver = $ valueResolver ;
@@ -61,6 +56,9 @@ public function getNodeTypes(): array
6156 {
6257 return NodeGroup::STMTS_AWARE ;
6358 }
59+ /**
60+ * @param StmtsAware $node
61+ */
6462 public function refactor (Node $ node ): ?Node
6563 {
6664 // if found, skip it :)
@@ -111,28 +109,19 @@ private function shouldSkipFuncCall(FuncCall $funcCall): bool
111109 }
112110 return $ this ->isFirstValueStringOrArray ($ funcCall );
113111 }
114- private function processJsonEncode (FuncCall $ funcCall ): FuncCall
112+ private function processJsonEncode (FuncCall $ funcCall ): ? FuncCall
115113 {
116- $ flags = [];
117114 if (isset ($ funcCall ->args [1 ])) {
118- /** @var Arg $arg */
119- $ arg = $ funcCall ->args [1 ];
120- $ flags = $ this ->getFlags ($ arg );
121- }
122- $ newArg = $ this ->getArgWithFlags ($ flags );
123- if ($ newArg instanceof Arg) {
124- $ this ->hasChanged = \true;
125- $ funcCall ->args [1 ] = $ newArg ;
115+ return null ;
126116 }
117+ $ this ->hasChanged = \true;
118+ $ funcCall ->args [1 ] = new Arg ($ this ->createConstFetch ('JSON_THROW_ON_ERROR ' ));
127119 return $ funcCall ;
128120 }
129- private function processJsonDecode (FuncCall $ funcCall ): FuncCall
121+ private function processJsonDecode (FuncCall $ funcCall ): ? FuncCall
130122 {
131- $ flags = [];
132123 if (isset ($ funcCall ->args [3 ])) {
133- /** @var Arg $arg */
134- $ arg = $ funcCall ->args [3 ];
135- $ flags = $ this ->getFlags ($ arg );
124+ return null ;
136125 }
137126 // set default to inter-args
138127 if (!isset ($ funcCall ->args [1 ])) {
@@ -141,11 +130,8 @@ private function processJsonDecode(FuncCall $funcCall): FuncCall
141130 if (!isset ($ funcCall ->args [2 ])) {
142131 $ funcCall ->args [2 ] = new Arg (new Int_ (512 ));
143132 }
144- $ newArg = $ this ->getArgWithFlags ($ flags );
145- if ($ newArg instanceof Arg) {
146- $ this ->hasChanged = \true;
147- $ funcCall ->args [3 ] = $ newArg ;
148- }
133+ $ this ->hasChanged = \true;
134+ $ funcCall ->args [3 ] = new Arg ($ this ->createConstFetch ('JSON_THROW_ON_ERROR ' ));
149135 return $ funcCall ;
150136 }
151137 private function createConstFetch (string $ name ): ConstFetch
@@ -164,49 +150,4 @@ private function isFirstValueStringOrArray(FuncCall $funcCall): bool
164150 }
165151 return is_array ($ value );
166152 }
167- /**
168- * @param string[] $flags
169- * @return string[]
170- * @param \PhpParser\Node\Expr|\PhpParser\Node\Arg $arg
171- */
172- private function getFlags ($ arg , array $ flags = []): array
173- {
174- // Unwrap Arg
175- if ($ arg instanceof Arg) {
176- $ arg = $ arg ->value ;
177- }
178- // Single flag: SOME_CONST
179- if ($ arg instanceof ConstFetch) {
180- $ flags [] = $ arg ->name ->getFirst ();
181- return $ flags ;
182- }
183- // Multiple flags: FLAG_A | FLAG_B | FLAG_C
184- if ($ arg instanceof Node \Expr \BinaryOp \BitwiseOr) {
185- $ flags = $ this ->getFlags ($ arg ->left , $ flags );
186- $ flags = $ this ->getFlags ($ arg ->right , $ flags );
187- }
188- return array_values (array_unique ($ flags ));
189- // array_unique in case the same flag is written multiple times
190- }
191- /**
192- * @param string[] $flags
193- */
194- private function getArgWithFlags (array $ flags ): ?Arg
195- {
196- $ originalCount = count ($ flags );
197- $ flags = array_values (array_unique (array_merge ($ flags , self ::FLAGS )));
198- if ($ originalCount === count ($ flags )) {
199- return null ;
200- }
201- // Single flag
202- if (count ($ flags ) === 1 ) {
203- return new Arg ($ this ->createConstFetch ($ flags [0 ]));
204- }
205- // Build FLAG_A | FLAG_B | FLAG_C
206- $ expr = $ this ->createConstFetch (array_shift ($ flags ));
207- foreach ($ flags as $ flag ) {
208- $ expr = new Node \Expr \BinaryOp \BitwiseOr ($ expr , $ this ->createConstFetch ($ flag ));
209- }
210- return new Arg ($ expr );
211- }
212153}
0 commit comments