Skip to content

Commit ff9424d

Browse files
committed
Merge branch 2.1.x into 2.2.x
2 parents f92aaba + 6a03691 commit ff9424d

18 files changed

Lines changed: 100 additions & 44 deletions

src/Analyser/ExprHandler/FuncCallHandler.php

Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@
6060
use PHPStan\Type\StringType;
6161
use PHPStan\Type\Type;
6262
use PHPStan\Type\TypeCombinator;
63-
use PHPStan\Type\TypeTraverser;
6463
use PHPStan\Type\UnionType;
6564
use Throwable;
6665
use 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) {

src/Analyser/ExprHandler/InstanceofHandler.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,7 @@ public function resolveType(MutatingScope $scope, Expr $expr): Type
9191
$classType = new ObjectType($className);
9292
}
9393
} else {
94-
$classType = $scope->getType($expr->class);
95-
$result = $classType->toObjectTypeForInstanceofCheck();
94+
$result = $scope->getType($expr->class)->toObjectTypeForInstanceofCheck();
9695
$classType = $result->type;
9796
$uncertainty = $result->uncertainty;
9897
}

src/Analyser/TypeSpecifier.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,7 @@ public function specifyTypesInCondition(
164164
return $this->create($exprNode, $type, $context, $scope)->setRootExpr($expr);
165165
}
166166

167-
$classType = $scope->getType($expr->class);
168-
$result = $classType->toObjectTypeForInstanceofCheck();
167+
$result = $scope->getType($expr->class)->toObjectTypeForInstanceofCheck();
169168
$type = $result->type;
170169
$uncertainty = $result->uncertainty;
171170

src/Type/Accessory/AccessoryArrayListType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,11 @@ public function shuffleArray(): Type
242242
return $this;
243243
}
244244

245+
public function sortArray(): Type
246+
{
247+
return $this;
248+
}
249+
245250
public function sliceArray(Type $offsetType, Type $lengthType, TrinaryLogic $preserveKeys): Type
246251
{
247252
if ($preserveKeys->no()) {

src/Type/Accessory/HasOffsetType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,11 @@ public function shuffleArray(): Type
202202
return new NonEmptyArrayType();
203203
}
204204

205+
public function sortArray(): Type
206+
{
207+
return $this;
208+
}
209+
205210
public function sliceArray(Type $offsetType, Type $lengthType, TrinaryLogic $preserveKeys): Type
206211
{
207212
if (

src/Type/Accessory/HasOffsetValueType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,11 @@ public function shuffleArray(): Type
291291
return new NonEmptyArrayType();
292292
}
293293

294+
public function sortArray(): Type
295+
{
296+
return $this;
297+
}
298+
294299
public function sliceArray(Type $offsetType, Type $lengthType, TrinaryLogic $preserveKeys): Type
295300
{
296301
if (

src/Type/Accessory/NonEmptyArrayType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,11 @@ public function shuffleArray(): Type
226226
return $this;
227227
}
228228

229+
public function sortArray(): Type
230+
{
231+
return $this;
232+
}
233+
229234
public function sliceArray(Type $offsetType, Type $lengthType, TrinaryLogic $preserveKeys): Type
230235
{
231236
if ((new ConstantIntegerType(0))->isSuperTypeOf($offsetType)->yes() && $lengthType->isNull()->yes()) {

src/Type/Accessory/OversizedArrayType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,11 @@ public function shuffleArray(): Type
214214
return $this;
215215
}
216216

217+
public function sortArray(): Type
218+
{
219+
return $this;
220+
}
221+
217222
public function sliceArray(Type $offsetType, Type $lengthType, TrinaryLogic $preserveKeys): Type
218223
{
219224
return $this;

src/Type/IntersectionType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,6 +1171,11 @@ public function shuffleArray(): Type
11711171
return $this->intersectTypes(static fn (Type $type): Type => $type->shuffleArray());
11721172
}
11731173

1174+
public function sortArray(): Type
1175+
{
1176+
return $this->intersectTypes(static fn (Type $type): Type => $type->sortArray());
1177+
}
1178+
11741179
public function sliceArray(Type $offsetType, Type $lengthType, TrinaryLogic $preserveKeys): Type
11751180
{
11761181
$result = $this->intersectTypes(static fn (Type $type): Type => $type->sliceArray($offsetType, $lengthType, $preserveKeys));

src/Type/MixedType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,11 @@ public function shuffleArray(): Type
289289
return new IntersectionType([new ArrayType(IntegerRangeType::createAllGreaterThanOrEqualTo(0), new MixedType($this->isExplicitMixed)), new AccessoryArrayListType()]);
290290
}
291291

292+
public function sortArray(): Type
293+
{
294+
return $this;
295+
}
296+
292297
public function sliceArray(Type $offsetType, Type $lengthType, TrinaryLogic $preserveKeys): Type
293298
{
294299
if ($this->isArray()->no()) {

0 commit comments

Comments
 (0)