Skip to content

Commit 8467479

Browse files
committed
refactor
1 parent e4ca868 commit 8467479

17 files changed

+40
-39
lines changed

Diff for: src/Type/Accessory/AccessoryArrayListType.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ public function unsetOffset(Type $offsetType): Type
172172
return new ErrorType();
173173
}
174174

175-
public function getKeysArray(): Type
175+
public function getKeysArray(?Type $filterValueType = null, bool $strict = false): Type
176176
{
177177
return $this;
178178
}

Diff for: src/Type/Accessory/HasOffsetType.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
343343
return new BooleanType();
344344
}
345345

346-
public function getKeysArray(): Type
346+
public function getKeysArray(?Type $filterValueType = null, bool $strict = false): Type
347347
{
348348
return new NonEmptyArrayType();
349349
}

Diff for: src/Type/Accessory/HasOffsetValueType.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ public function unsetOffset(Type $offsetType): Type
195195
return $this;
196196
}
197197

198-
public function getKeysArray(): Type
198+
public function getKeysArray(?Type $filterValueType = null, bool $strict = false): Type
199199
{
200200
return new NonEmptyArrayType();
201201
}

Diff for: src/Type/Accessory/NonEmptyArrayType.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public function unsetOffset(Type $offsetType): Type
159159
return new ErrorType();
160160
}
161161

162-
public function getKeysArray(): Type
162+
public function getKeysArray(?Type $filterValueType = null, bool $strict = false): Type
163163
{
164164
return $this;
165165
}

Diff for: src/Type/Accessory/OversizedArrayType.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public function unsetOffset(Type $offsetType): Type
154154
return new ErrorType();
155155
}
156156

157-
public function getKeysArray(): Type
157+
public function getKeysArray(?Type $filterValueType = null, bool $strict = false): Type
158158
{
159159
return $this;
160160
}

Diff for: src/Type/ArrayType.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public function generalizeValues(): self
170170
return new self($this->keyType, $this->itemType->generalize(GeneralizePrecision::lessSpecific()));
171171
}
172172

173-
public function getKeysArray(): Type
173+
public function getKeysArray(?Type $filterValueType = null, bool $strict = false): Type
174174
{
175175
return TypeCombinator::intersect(new self(new IntegerType(), $this->getIterableKeyType()), new AccessoryArrayListType());
176176
}

Diff for: src/Type/Constant/ConstantArrayType.php

+14-2
Original file line numberDiff line numberDiff line change
@@ -1261,9 +1261,21 @@ public function generalizeValues(): self
12611261
return new self($this->keyTypes, $valueTypes, $this->nextAutoIndexes, $this->optionalKeys, $this->isList);
12621262
}
12631263

1264-
public function getKeysArray(): self
1264+
public function getKeysArray(?Type $filterValueType = null, bool $strict = false): Type
12651265
{
1266-
return $this->getKeysOrValuesArray($this->keyTypes);
1266+
$keysArray = $this->getKeysOrValuesArray($this->keyTypes);
1267+
1268+
if ($filterValueType !== null) {
1269+
return TypeCombinator::intersect(
1270+
new ArrayType(
1271+
$keysArray->getIterableKeyType()->generalize(GeneralizePrecision::lessSpecific()),
1272+
$keysArray->getIterableValueType()->generalize(GeneralizePrecision::lessSpecific()),
1273+
),
1274+
new AccessoryArrayListType(),
1275+
);
1276+
}
1277+
1278+
return $keysArray;
12671279
}
12681280

12691281
public function getValuesArray(): self

Diff for: src/Type/IntersectionType.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -799,9 +799,9 @@ public function unsetOffset(Type $offsetType): Type
799799
return $this->intersectTypes(static fn (Type $type): Type => $type->unsetOffset($offsetType));
800800
}
801801

802-
public function getKeysArray(): Type
802+
public function getKeysArray(?Type $filterValueType = null, bool $strict = false): Type
803803
{
804-
return $this->intersectTypes(static fn (Type $type): Type => $type->getKeysArray());
804+
return $this->intersectTypes(static fn (Type $type): Type => $type->getKeysArray($filterValueType, $strict));
805805
}
806806

807807
public function getValuesArray(): Type

Diff for: src/Type/MixedType.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ public function unsetOffset(Type $offsetType): Type
179179
return $this;
180180
}
181181

182-
public function getKeysArray(): Type
182+
public function getKeysArray(?Type $filterValueType = null, bool $strict = false): Type
183183
{
184184
if ($this->isArray()->no()) {
185185
return new ErrorType();

Diff for: src/Type/NeverType.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ public function unsetOffset(Type $offsetType): Type
273273
return new NeverType();
274274
}
275275

276-
public function getKeysArray(): Type
276+
public function getKeysArray(?Type $filterValueType = null, bool $strict = false): Type
277277
{
278278
return new NeverType();
279279
}

Diff for: src/Type/Php/ArrayKeysFunctionDynamicReturnTypeExtension.php

+7-18
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,10 @@
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Php\PhpVersion;
88
use PHPStan\Reflection\FunctionReflection;
9-
use PHPStan\Type\Accessory\AccessoryArrayListType;
10-
use PHPStan\Type\ArrayType;
119
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
12-
use PHPStan\Type\GeneralizePrecision;
1310
use PHPStan\Type\NeverType;
1411
use PHPStan\Type\NullType;
1512
use PHPStan\Type\Type;
16-
use PHPStan\Type\TypeCombinator;
1713
use function count;
1814
use function strtolower;
1915

@@ -40,22 +36,15 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
4036
return $this->phpVersion->arrayFunctionsReturnNullWithNonArray() ? new NullType() : new NeverType();
4137
}
4238

43-
$keysArray = $arrayType->getKeysArray();
44-
if (count($functionCall->getArgs()) === 1) {
45-
return $keysArray;
39+
$strict = false;
40+
$filterType = null;
41+
if (count($functionCall->getArgs()) >= 2) {
42+
$filterType = $scope->getType($functionCall->getArgs()[1]->value);
4643
}
47-
48-
$newArrayType = $keysArray;
49-
if (!$keysArray->isConstantArray()->no()) {
50-
$newArrayType = new ArrayType(
51-
$keysArray->getIterableKeyType()->generalize(GeneralizePrecision::lessSpecific()),
52-
$keysArray->getIterableValueType()->generalize(GeneralizePrecision::lessSpecific()),
53-
);
54-
}
55-
if ($keysArray->isList()->yes()) {
56-
$newArrayType = TypeCombinator::intersect($newArrayType, new AccessoryArrayListType());
44+
if (count($functionCall->getArgs()) >= 3) {
45+
$strict = $scope->getType($functionCall->getArgs()[2]->value)->isTrue()->yes();
5746
}
58-
return $newArrayType;
47+
return $arrayType->getKeysArray($filterType, $strict);
5948
}
6049

6150
}

Diff for: src/Type/StaticType.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -396,9 +396,9 @@ public function unsetOffset(Type $offsetType): Type
396396
return $this->getStaticObjectType()->unsetOffset($offsetType);
397397
}
398398

399-
public function getKeysArray(): Type
399+
public function getKeysArray(?Type $filterValueType = null, bool $strict = false): Type
400400
{
401-
return $this->getStaticObjectType()->getKeysArray();
401+
return $this->getStaticObjectType()->getKeysArray($filterValueType, $strict);
402402
}
403403

404404
public function getValuesArray(): Type

Diff for: src/Type/Traits/LateResolvableTypeTrait.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,9 @@ public function unsetOffset(Type $offsetType): Type
248248
return $this->resolve()->unsetOffset($offsetType);
249249
}
250250

251-
public function getKeysArray(): Type
251+
public function getKeysArray(?Type $filterValueType = null, bool $strict = false): Type
252252
{
253-
return $this->resolve()->getKeysArray();
253+
return $this->resolve()->getKeysArray($filterValueType, $strict);
254254
}
255255

256256
public function getValuesArray(): Type

Diff for: src/Type/Traits/MaybeArrayTypeTrait.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function isList(): TrinaryLogic
3939
return TrinaryLogic::createMaybe();
4040
}
4141

42-
public function getKeysArray(): Type
42+
public function getKeysArray(?Type $filterValueType = null, bool $strict = false): Type
4343
{
4444
return new ErrorType();
4545
}

Diff for: src/Type/Traits/NonArrayTypeTrait.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function isList(): TrinaryLogic
3939
return TrinaryLogic::createNo();
4040
}
4141

42-
public function getKeysArray(): Type
42+
public function getKeysArray(?Type $filterValueType = null, bool $strict = false): Type
4343
{
4444
return new ErrorType();
4545
}

Diff for: src/Type/Type.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public function setExistingOffsetValueType(Type $offsetType, Type $valueType): T
134134

135135
public function unsetOffset(Type $offsetType): Type;
136136

137-
public function getKeysArray(): Type;
137+
public function getKeysArray(?Type $filterValueType = null, bool $strict = false): Type;
138138

139139
public function getValuesArray(): Type;
140140

Diff for: src/Type/UnionType.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -730,9 +730,9 @@ public function unsetOffset(Type $offsetType): Type
730730
return $this->unionTypes(static fn (Type $type): Type => $type->unsetOffset($offsetType));
731731
}
732732

733-
public function getKeysArray(): Type
733+
public function getKeysArray(?Type $filterValueType = null, bool $strict = false): Type
734734
{
735-
return $this->unionTypes(static fn (Type $type): Type => $type->getKeysArray());
735+
return $this->unionTypes(static fn (Type $type): Type => $type->getKeysArray($filterValueType, $strict));
736736
}
737737

738738
public function getValuesArray(): Type

0 commit comments

Comments
 (0)