1111use PhpParser \Node \Expr \MethodCall ;
1212use PhpParser \Node \Expr \StaticCall ;
1313use PhpParser \Node \Expr \Variable ;
14+ use PhpParser \Node \Identifier ;
1415use PhpParser \Node \Name ;
1516use PhpParser \Node \Param ;
1617use PhpParser \Node \Stmt \Class_ ;
2425use Rector \Contract \Rector \ConfigurableRectorInterface ;
2526use Rector \Enum \ObjectReference ;
2627use Rector \Exception \ShouldNotHappenException ;
28+ use Rector \NodeAnalyzer \ArgsAnalyzer ;
2729use Rector \PhpParser \AstResolver ;
2830use Rector \PHPStanStaticTypeMapper \Enum \TypeKind ;
2931use Rector \Rector \AbstractRector ;
@@ -48,7 +50,8 @@ public function __construct(
4850 private readonly ArgumentAddingScope $ argumentAddingScope ,
4951 private readonly ChangedArgumentsDetector $ changedArgumentsDetector ,
5052 private readonly AstResolver $ astResolver ,
51- private readonly StaticTypeMapper $ staticTypeMapper
53+ private readonly StaticTypeMapper $ staticTypeMapper ,
54+ private readonly ArgsAnalyzer $ argsAnalyzer
5255 ) {
5356 }
5457
@@ -181,13 +184,21 @@ private function processMethodCall(
181184
182185 $ defaultValue = $ argumentAdder ->getArgumentDefaultValue ();
183186 $ arg = new Arg (BuilderHelpers ::normalizeValue ($ defaultValue ));
184- if (isset ($ methodCall ->args [$ position ])) {
185- return ;
187+
188+ // if there are named argyments, we just add it at the end as a new named argument
189+ if ($ this ->argsAnalyzer ->hasNamedArg ($ methodCall ->getArgs ())) {
190+ $ argumentName = $ argumentAdder ->getArgumentName ();
191+ if ($ argumentName === null ) {
192+ throw new ShouldNotHappenException ();
193+ }
194+
195+ $ arg ->name = new Identifier ($ argumentName );
196+ } else {
197+ $ this ->fillGapBetweenWithDefaultValue ($ methodCall , $ position );
186198 }
187199
188- $ this -> fillGapBetweenWithDefaultValue ( $ methodCall , $ position ) ;
200+ $ methodCall -> args [] = $ arg ;
189201
190- $ methodCall ->args [$ position ] = $ arg ;
191202 $ this ->hasChanged = true ;
192203 }
193204
@@ -249,7 +260,20 @@ private function shouldSkipParameter(
249260 return $ this ->changedArgumentsDetector ->isTypeChanged ($ param , $ argumentAdder ->getArgumentType ());
250261 }
251262
252- if (isset ($ node ->args [$ position ])) {
263+ $ arguments = $ node ->getArgs ();
264+ $ firstNamedArgumentPosition = $ this ->argsAnalyzer ->resolveFirstNamedArgPosition ($ arguments );
265+ // If named arguments exist
266+ if ($ firstNamedArgumentPosition !== null ) {
267+ // Check if the parameter we're trying to add is before the first named argument
268+ if ($ position < $ firstNamedArgumentPosition ) {
269+ return true ; //if that is the case, the parameter already exists, skip
270+ }
271+
272+ // Check if the parameter we're trying to add is already present as a named argument
273+ if ($ this ->argsAnalyzer ->resolveArgPosition ($ arguments , $ argumentName , -1 ) !== -1 ) {
274+ return true ; // if it exists as a named argument, skip
275+ }
276+ } elseif (isset ($ node ->args [$ position ])) {
253277 return true ;
254278 }
255279
@@ -333,9 +357,15 @@ private function processStaticCall(
333357 return ;
334358 }
335359
336- $ this ->fillGapBetweenWithDefaultValue ($ staticCall , $ position );
360+ // if there are named arguments, we just add it at the end as a new named argument
361+ $ arg = new Arg (new Variable ($ argumentName ));
362+ if ($ this ->argsAnalyzer ->hasNamedArg ($ staticCall ->getArgs ())) {
363+ $ arg ->name = new Identifier ($ argumentName );
364+ } else {
365+ $ this ->fillGapBetweenWithDefaultValue ($ staticCall , $ position );
366+ }
337367
338- $ staticCall ->args [$ position ] = new Arg ( new Variable ( $ argumentName )) ;
368+ $ staticCall ->args [] = $ arg ;
339369 $ this ->hasChanged = true ;
340370 }
341371
0 commit comments