1111use PhpParser \Node \Expr \BinaryOp \NotIdentical ;
1212use PhpParser \Node \Expr \ConstFetch ;
1313use PhpParser \Node \Expr \FuncCall ;
14- use PhpParser \Node \Identifier ;
1514use PhpParser \Node \Name ;
1615use PHPStan \Analyser \Scope ;
17- use PHPStan \Reflection \Native \NativeFunctionReflection ;
18- use Rector \NodeAnalyzer \ArgsAnalyzer ;
1916use Rector \NodeManipulator \BinaryOpManipulator ;
2017use Rector \NodeTypeResolver \Node \AttributeKey ;
21- use Rector \NodeTypeResolver \PHPStan \ParametersAcceptorSelectorVariantsWrapper ;
2218use Rector \Php71 \ValueObject \TwoNodeMatch ;
2319use Rector \PhpParser \Node \Value \ValueResolver ;
2420use Rector \Rector \AbstractRector ;
25- use Rector \Reflection \ReflectionResolver ;
2621use Rector \ValueObject \PhpVersionFeature ;
2722use Rector \ValueObject \PolyfillPackage ;
2823use Rector \VersionBonding \Contract \MinPhpVersionInterface ;
@@ -41,8 +36,6 @@ final class JsonValidateRector extends AbstractRector implements MinPhpVersionIn
4136
4237 public function __construct (
4338 private readonly BinaryOpManipulator $ binaryOpManipulator ,
44- private readonly ReflectionResolver $ reflectionResolver ,
45- private readonly ArgsAnalyzer $ argsAnalyzer ,
4639 private ValueResolver $ valueResolver ,
4740 ) {
4841 }
@@ -102,15 +95,12 @@ public function refactor(Node $node): ?Node
10295 }
10396
10497 $ args = $ funcCall ->getArgs ();
105- $ positions = $ this ->argsAnalyzer ->hasNamedArg ($ args )
106- ? $ this ->resolveNamedPositions ($ args )
107- : $ this ->resolveOriginalPositions ($ funcCall , $ scope );
10898
109- if ($ positions === []) {
99+ if ($ args === []) {
110100 return null ;
111101 }
112102
113- if (! $ this ->validateArgs ($ args , $ positions )) {
103+ if (! $ this ->validateArgs ($ funcCall )) {
114104 return null ;
115105 }
116106 $ funcCall ->name = new Name ('json_validate ' );
@@ -164,81 +154,25 @@ public function matchJsonValidateArg(BooleanAnd $booleanAnd): ?FuncCall
164154 return $ funcCall ;
165155 }
166156
167- /**
168- * @param Arg[] $args
169- * @param int[]|string[] $positions
170- */
171- protected function validateArgs (array $ args , array $ positions ): bool
157+ protected function validateArgs (FuncCall $ funcCall ): bool
172158 {
173- foreach ($ positions as $ position ) {
174- $ arg = $ args [$ position ] ?? '' ;
175- if ($ arg instanceof Arg && $ arg ->name instanceof Identifier && $ arg ->name ->toString () === 'flags ' ) {
176- $ flags = $ this ->valueResolver ->getValue ($ arg );
177- if ($ flags !== JSON_INVALID_UTF8_IGNORE ) {
178- return false ;
179- }
180- }
181- if ($ arg instanceof Arg && $ arg ->name instanceof Identifier && $ arg ->name ->toString () === 'depth ' ) {
182- $ depth = $ this ->valueResolver ->getValue ($ arg );
183- if ($ depth <= 0 ) {
184- return false ;
185- }
186- if ($ depth > self ::JSON_MAX_DEPTH ) {
187- return false ;
188- }
189- }
190- }
191-
192- return true ;
193- }
194-
195- /**
196- * @param Arg[] $args
197- * @return int[]|string[]
198- */
199- private function resolveNamedPositions (array $ args ): array
200- {
201- $ positions = [];
202-
203- foreach ($ args as $ position => $ arg ) {
204- if (! $ arg ->name instanceof Identifier) {
205- continue ;
206- }
159+ $ depth = $ funcCall ->getArg ('depth ' , 2 );
160+ $ flags = $ funcCall ->getArg ('flags ' , 3 );
207161
208- if (! $ this ->isNames ($ arg ->name , self ::ARG_NAMES )) {
209- continue ;
162+ if ($ flags instanceof Arg) {
163+ $ flagsValue = $ this ->valueResolver ->getValue ($ flags );
164+ if ($ flagsValue !== JSON_INVALID_UTF8_IGNORE ) {
165+ return false ;
210166 }
211-
212- $ positions [] = $ position ;
213167 }
214168
215- return $ positions ;
216- }
217-
218- /**
219- * @return int[]|string[]
220- */
221- private function resolveOriginalPositions (FuncCall $ funcCall , Scope $ scope ): array
222- {
223- $ functionReflection = $ this ->reflectionResolver ->resolveFunctionLikeReflectionFromCall ($ funcCall );
224- if (! $ functionReflection instanceof NativeFunctionReflection) {
225- return [];
226- }
227-
228- $ parametersAcceptor = ParametersAcceptorSelectorVariantsWrapper::select (
229- $ functionReflection ,
230- $ funcCall ,
231- $ scope
232- );
233-
234- $ positions = [];
235-
236- foreach ($ parametersAcceptor ->getParameters () as $ position => $ parameterReflection ) {
237- if (in_array ($ parameterReflection ->getName (), self ::ARG_NAMES , true )) {
238- $ positions [] = $ position ;
169+ if ($ depth instanceof Arg) {
170+ $ depthValue = $ this ->valueResolver ->getValue ($ depth );
171+ if ($ depthValue <= 0 || $ depthValue > self ::JSON_MAX_DEPTH ) {
172+ return false ;
239173 }
240174 }
241175
242- return $ positions ;
176+ return true ;
243177 }
244178}
0 commit comments