1818use PhpParser \Node \Name \FullyQualified as NameFullyQualified ;
1919use PhpParser \Node \Stmt \Expression ;
2020use PhpParser \Node \Stmt \If_ ;
21- use PhpParser \Node \VariadicPlaceholder ;
2221use Rector \DowngradePhp72 \NodeManipulator \JsonConstCleaner ;
2322use Rector \Enum \JsonConstant ;
2423use Rector \NodeAnalyzer \DefineFuncCallAnalyzer ;
2524use Rector \NodeTypeResolver \Node \AttributeKey ;
25+ use Rector \PhpParser \NodeTraverser \SimpleNodeTraverser ;
2626use Rector \Rector \AbstractRector ;
2727use Symplify \RuleDocGenerator \ValueObject \CodeSample \CodeSample ;
2828use Symplify \RuleDocGenerator \ValueObject \RuleDefinition ;
@@ -42,7 +42,7 @@ final class DowngradePhp73JsonConstRector extends AbstractRector
4242 /**
4343 * @var array<string>
4444 */
45- private const REFACTOR_FUNCS = ['json_decode ' , 'json_encode ' ];
45+ private const JSON_FUNCTIONS = ['json_decode ' , 'json_encode ' ];
4646
4747 public function __construct (
4848 private readonly JsonConstCleaner $ jsonConstCleaner ,
@@ -63,12 +63,12 @@ public function getRuleDefinition(): RuleDefinition
6363CODE_SAMPLE
6464 ,
6565 <<<'CODE_SAMPLE'
66- $json json_encode($content, 0 );
66+ $json = json_encode($content);
6767if (json_last_error() !== JSON_ERROR_NONE) {
6868 throw new \Exception(json_last_error_msg());
6969}
7070
71- $content = json_decode($json, null, 512, 0 );
71+ $content = json_decode($json, null, 512);
7272if (json_last_error() !== JSON_ERROR_NONE) {
7373 throw new \Exception(json_last_error_msg());
7474}
@@ -115,15 +115,12 @@ private function markConstantKnownInInnerStmts(If_ $if): void
115115 return ;
116116 }
117117
118- $ this ->traverseNodesWithCallable ($ if , static function (Node $ node ): null {
119- $ node ->setAttribute (self ::PHP73_JSON_CONSTANT_IS_KNOWN , true );
120- return null ;
121- });
118+ SimpleNodeTraverser::decorateWithAttributeValue ($ if , self ::PHP73_JSON_CONSTANT_IS_KNOWN , true );
122119 }
123120
124- private function resolveFuncCall (Expression $ Expression ): ?FuncCall
121+ private function resolveFuncCall (Expression $ expression ): ?FuncCall
125122 {
126- $ expr = $ Expression ->expr ;
123+ $ expr = $ expression ->expr ;
127124 if ($ expr instanceof Assign) {
128125 if ($ expr ->expr instanceof FuncCall) {
129126 return $ expr ->expr ;
@@ -155,21 +152,22 @@ private function refactorExpression(Expression $expression): ?array
155152 return null ;
156153 }
157154
158- // retrieve a `FuncCall`, if any, from the statement
159155 $ funcCall = $ this ->resolveFuncCall ($ expression );
160-
161- // Nothing to do if no `FuncCall` found
162156 if (! $ funcCall instanceof FuncCall) {
163157 return null ;
164158 }
165159
160+ if ($ funcCall ->isFirstClassCallable ()) {
161+ return null ;
162+ }
163+
166164 // Nothing to do if not a refactored function
167- if (! in_array ( $ this ->getName ($ funcCall) , self ::REFACTOR_FUNCS , true )) {
165+ if (! $ this ->isNames ($ funcCall , self ::JSON_FUNCTIONS )) {
168166 return null ;
169167 }
170168
171169 // Nothing to do if the flag `JSON_THROW_ON_ERROR` is not set in args
172- if (! $ this ->hasConstFetchInArgs ($ funcCall ->args , 'JSON_THROW_ON_ERROR ' )) {
170+ if (! $ this ->hasConstFetchInArgs ($ funcCall ->getArgs () , 'JSON_THROW_ON_ERROR ' )) {
173171 return null ;
174172 }
175173
@@ -196,18 +194,12 @@ private function refactorExpression(Expression $expression): ?array
196194
197195 /**
198196 * Search if a given constant is set within a list of `Arg`
199- * @param array< Arg|VariadicPlaceholder> $args
197+ * @param Arg[] $args
200198 */
201199 private function hasConstFetchInArgs (array $ args , string $ constName ): bool
202200 {
203201 foreach ($ args as $ arg ) {
204- // Only `Arg` instances are handled.
205- if (! $ arg instanceof Arg) {
206- return false ;
207- }
208-
209202 $ value = $ arg ->value ;
210-
211203 if ($ value instanceof ConstFetch && $ this ->getName ($ value ) === $ constName ) {
212204 return true ;
213205 }
0 commit comments