6060use PHPStan \Type \StringType ;
6161use PHPStan \Type \Type ;
6262use PHPStan \Type \TypeCombinator ;
63- use PHPStan \Type \TypeTraverser ;
6463use PHPStan \Type \UnionType ;
6564use Throwable ;
6665use function array_filter ;
@@ -274,8 +273,10 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
274273 $ isAlwaysTerminating = $ isAlwaysTerminating || $ argsResult ->isAlwaysTerminating ();
275274
276275 if ($ arrayWalkValueTypes !== null && $ arrayWalkArrayArg !== null ) {
277- $ newArrayType = $ this ->getArrayWalkResultType ($ arrayWalkOriginalArrayType , $ arrayWalkValueTypes [0 ]);
278- $ newArrayNativeType = $ this ->getArrayWalkResultType ($ arrayWalkOriginalArrayNativeType , $ arrayWalkValueTypes [1 ]);
276+ $ arrayWalkValueType = $ arrayWalkValueTypes [0 ];
277+ $ arrayWalkValueNativeType = $ arrayWalkValueTypes [1 ];
278+ $ newArrayType = $ arrayWalkOriginalArrayType ->mapValueType (static fn (Type $ type ): Type => $ arrayWalkValueType );
279+ $ newArrayNativeType = $ arrayWalkOriginalArrayNativeType ->mapValueType (static fn (Type $ type ): Type => $ arrayWalkValueNativeType );
279280
280281 $ scope = $ nodeScopeResolver ->processVirtualAssign (
281282 $ scope ,
@@ -462,7 +463,7 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
462463 $ storage ,
463464 $ stmt ,
464465 $ arrayArg ,
465- new NativeTypeExpr ($ this -> getArraySortPreserveListFunctionType ( $ scope ->getType ($ arrayArg )), $ this -> getArraySortPreserveListFunctionType ( $ scope ->getNativeType ($ arrayArg ))),
466+ new NativeTypeExpr ($ scope ->getType ($ arrayArg )-> sortArray ( ), $ scope ->getNativeType ($ arrayArg )-> sortArray ( )),
466467 $ nodeCallback ,
467468 )->getScope ();
468469 }
@@ -479,7 +480,7 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
479480 $ storage ,
480481 $ stmt ,
481482 $ arrayArg ,
482- new NativeTypeExpr ($ this -> getArraySortDoNotPreserveListFunctionType ( $ scope ->getType ($ arrayArg )), $ this -> getArraySortDoNotPreserveListFunctionType ( $ scope ->getNativeType ($ arrayArg ))),
483+ new NativeTypeExpr ($ scope ->getType ($ arrayArg )-> makeListMaybe ( ), $ scope ->getNativeType ($ arrayArg )-> makeListMaybe ( )),
483484 $ nodeCallback ,
484485 )->getScope ();
485486 }
@@ -722,41 +723,6 @@ static function (?Type $offsetType, Type $valueType, bool $optional) use (&$arra
722723 return $ arrayType ;
723724 }
724725
725- private function getArraySortPreserveListFunctionType (Type $ type ): Type
726- {
727- $ isIterableAtLeastOnce = $ type ->isIterableAtLeastOnce ();
728- if ($ isIterableAtLeastOnce ->no ()) {
729- return $ type ;
730- }
731-
732- return TypeTraverser::map ($ type , static function (Type $ type , callable $ traverse ) use ($ isIterableAtLeastOnce ): Type {
733- if ($ type instanceof UnionType || $ type instanceof IntersectionType) {
734- return $ traverse ($ type );
735- }
736-
737- if (!$ type instanceof ArrayType && !$ type instanceof ConstantArrayType) {
738- return $ type ;
739- }
740-
741- $ newArrayType = new IntersectionType ([new ArrayType (IntegerRangeType::createAllGreaterThanOrEqualTo (0 ), $ type ->getIterableValueType ()), new AccessoryArrayListType ()]);
742- if ($ isIterableAtLeastOnce ->yes ()) {
743- $ newArrayType = TypeCombinator::intersect ($ newArrayType , new NonEmptyArrayType ());
744- }
745-
746- return $ newArrayType ;
747- });
748- }
749-
750- private function getArraySortDoNotPreserveListFunctionType (Type $ type ): Type
751- {
752- return $ type ->makeListMaybe ();
753- }
754-
755- private function getArrayWalkResultType (Type $ arrayType , Type $ newValueType ): Type
756- {
757- return $ arrayType ->mapValueType (static fn (Type $ type ): Type => $ newValueType );
758- }
759-
760726 public function resolveType (MutatingScope $ scope , Expr $ expr ): Type
761727 {
762728 if ($ expr ->name instanceof Expr) {
0 commit comments