Skip to content

Commit c16ce86

Browse files
committed
refactor
1 parent 00184f0 commit c16ce86

17 files changed

+40
-39
lines changed

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
}

src/Type/Accessory/HasOffsetType.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
338338
return new BooleanType();
339339
}
340340

341-
public function getKeysArray(): Type
341+
public function getKeysArray(?Type $filterValueType = null, bool $strict = false): Type
342342
{
343343
return new NonEmptyArrayType();
344344
}

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
}

src/Type/Accessory/NonEmptyArrayType.php

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

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

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
}

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
}

src/Type/Constant/ConstantArrayType.php

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

1248-
public function getKeysArray(): self
1248+
public function getKeysArray(?Type $filterValueType = null, bool $strict = false): Type
12491249
{
1250-
return $this->getKeysOrValuesArray($this->keyTypes);
1250+
$keysArray = $this->getKeysOrValuesArray($this->keyTypes);
1251+
1252+
if ($filterValueType !== null) {
1253+
return TypeCombinator::intersect(
1254+
new ArrayType(
1255+
$keysArray->getIterableKeyType()->generalize(GeneralizePrecision::lessSpecific()),
1256+
$keysArray->getIterableValueType()->generalize(GeneralizePrecision::lessSpecific()),
1257+
),
1258+
new AccessoryArrayListType(),
1259+
);
1260+
}
1261+
1262+
return $keysArray;
12511263
}
12521264

12531265
public function getValuesArray(): self

src/Type/IntersectionType.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -781,9 +781,9 @@ public function unsetOffset(Type $offsetType): Type
781781
return $this->intersectTypes(static fn (Type $type): Type => $type->unsetOffset($offsetType));
782782
}
783783

784-
public function getKeysArray(): Type
784+
public function getKeysArray(?Type $filterValueType = null, bool $strict = false): Type
785785
{
786-
return $this->intersectTypes(static fn (Type $type): Type => $type->getKeysArray());
786+
return $this->intersectTypes(static fn (Type $type): Type => $type->getKeysArray($filterValueType, $strict));
787787
}
788788

789789
public function getValuesArray(): Type

src/Type/MixedType.php

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

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

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
}

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
}

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

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

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
}

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
}

src/Type/Type.php

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

142142
public function unsetOffset(Type $offsetType): Type;
143143

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

146146
public function getValuesArray(): Type;
147147

src/Type/UnionType.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -706,9 +706,9 @@ public function unsetOffset(Type $offsetType): Type
706706
return $this->unionTypes(static fn (Type $type): Type => $type->unsetOffset($offsetType));
707707
}
708708

709-
public function getKeysArray(): Type
709+
public function getKeysArray(?Type $filterValueType = null, bool $strict = false): Type
710710
{
711-
return $this->unionTypes(static fn (Type $type): Type => $type->getKeysArray());
711+
return $this->unionTypes(static fn (Type $type): Type => $type->getKeysArray($filterValueType, $strict));
712712
}
713713

714714
public function getValuesArray(): Type

0 commit comments

Comments
 (0)