1212use PhpParser \Node \Name ;
1313use PhpParser \Node \Stmt \ClassConst ;
1414use PhpParser \Node \Stmt \ClassLike ;
15- use PHPStan \Analyser \MutatingScope ;
1615use PHPStan \Type \Type ;
1716use Rector \DowngradePhp81 \NodeAnalyzer \ArraySpreadAnalyzer ;
1817use Rector \DowngradePhp81 \NodeFactory \ArrayMergeFromArraySpreadFactory ;
2322use Rector \StaticTypeMapper \ValueObject \Type \FullyQualifiedObjectType ;
2423use Symplify \RuleDocGenerator \ValueObject \CodeSample \CodeSample ;
2524use Symplify \RuleDocGenerator \ValueObject \RuleDefinition ;
25+ use PHPStan \Analyser \MutatingScope ;
2626
2727/**
2828 * @changelog https://wiki.php.net/rfc/spread_operator_for_array
@@ -123,12 +123,19 @@ private function refactorUnderClassConst(ClassConst $classConst): ?ClassConst
123123
124124 $ hasChanged = false ;
125125
126- foreach ($ arrays as $ array ) {
127- $ refactorArrayConstValue = $ this ->refactorArrayConstValue ($ array );
126+ $ this ->traverseNodesWithCallable ($ classConst ->consts , function (Node $ subNode ) use (&$ hasChanged ): ?Node {
127+ if (! $ subNode instanceof Array_) {
128+ return null ;
129+ }
130+
131+ $ refactorArrayConstValue = $ this ->refactorArrayConstValue ($ subNode );
128132 if ($ refactorArrayConstValue instanceof Array_) {
129133 $ hasChanged = true ;
134+ return $ refactorArrayConstValue ;
130135 }
131- }
136+
137+ return null ;
138+ });
132139
133140 if ($ hasChanged ) {
134141 return $ classConst ;
@@ -166,9 +173,11 @@ private function refactorArrayConstValue(Array_ $array): ?Array_
166173 {
167174 $ hasChanged = false ;
168175
169- foreach ($ array ->items as $ key => $ item ) {
176+ $ newArray = new Array_ ();
177+ foreach ($ array ->items as $ item ) {
170178 $ type = $ this ->resolveItemType ($ item );
171179 if (! $ type instanceof FullyQualifiedObjectType) {
180+ $ newArray ->items [] = $ item ;
172181 continue ;
173182 }
174183
@@ -179,6 +188,7 @@ private function refactorArrayConstValue(Array_ $array): ?Array_
179188 /** @var Identifier $name */
180189 $ classLike = $ this ->astResolver ->resolveClassFromName ($ type ->getClassName ());
181190 if (! $ classLike instanceof ClassLike) {
191+ $ newArray ->items [] = $ item ;
182192 continue ;
183193 }
184194
@@ -188,16 +198,15 @@ private function refactorArrayConstValue(Array_ $array): ?Array_
188198 $ const = $ constant ->consts [0 ];
189199
190200 if ($ const ->name ->toString () === $ name ->toString () && $ const ->value instanceof Array_) {
191- unset($ array ->items [$ key ]);
192- array_splice ($ array ->items , $ key , 0 , $ const ->value ->items );
201+ $ newArray ->items = array_merge ($ newArray ->items , $ const ->value ->items );
193202
194203 $ hasChanged = true ;
195204 }
196205 }
197206 }
198207
199208 if ($ hasChanged ) {
200- return $ array ;
209+ return $ newArray ;
201210 }
202211
203212 return null ;
0 commit comments