Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Nov 14, 2024
1 parent 2334804 commit 21fc500
Show file tree
Hide file tree
Showing 17 changed files with 40 additions and 39 deletions.
2 changes: 1 addition & 1 deletion src/Type/Accessory/AccessoryArrayListType.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public function unsetOffset(Type $offsetType): Type
return new ErrorType();
}

public function getKeysArray(): Type
public function getKeysArray(?Type $filterValueType = null, bool $strict = false): Type
{
return $this;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Type/Accessory/HasOffsetType.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
return new BooleanType();
}

public function getKeysArray(): Type
public function getKeysArray(?Type $filterValueType = null, bool $strict = false): Type
{
return new NonEmptyArrayType();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Type/Accessory/HasOffsetValueType.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public function unsetOffset(Type $offsetType): Type
return $this;
}

public function getKeysArray(): Type
public function getKeysArray(?Type $filterValueType = null, bool $strict = false): Type
{
return new NonEmptyArrayType();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Type/Accessory/NonEmptyArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public function unsetOffset(Type $offsetType): Type
return new ErrorType();
}

public function getKeysArray(): Type
public function getKeysArray(?Type $filterValueType = null, bool $strict = false): Type
{
return $this;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Type/Accessory/OversizedArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public function unsetOffset(Type $offsetType): Type
return new ErrorType();
}

public function getKeysArray(): Type
public function getKeysArray(?Type $filterValueType = null, bool $strict = false): Type
{
return $this;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Type/ArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public function generalizeValues(): self
return new self($this->keyType, $this->itemType->generalize(GeneralizePrecision::lessSpecific()));
}

public function getKeysArray(): Type
public function getKeysArray(?Type $filterValueType = null, bool $strict = false): Type
{
return TypeCombinator::intersect(new self(new IntegerType(), $this->getIterableKeyType()), new AccessoryArrayListType());
}
Expand Down
16 changes: 14 additions & 2 deletions src/Type/Constant/ConstantArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -1245,9 +1245,21 @@ public function generalizeValues(): self
return new self($this->keyTypes, $valueTypes, $this->nextAutoIndexes, $this->optionalKeys, $this->isList);
}

public function getKeysArray(): self
public function getKeysArray(?Type $filterValueType = null, bool $strict = false): Type
{
return $this->getKeysOrValuesArray($this->keyTypes);
$keysArray = $this->getKeysOrValuesArray($this->keyTypes);

if ($filterValueType !== null) {
return TypeCombinator::intersect(
new ArrayType(
$keysArray->getIterableKeyType()->generalize(GeneralizePrecision::lessSpecific()),
$keysArray->getIterableValueType()->generalize(GeneralizePrecision::lessSpecific()),
),
new AccessoryArrayListType(),
);
}

return $keysArray;
}

public function getValuesArray(): self
Expand Down
4 changes: 2 additions & 2 deletions src/Type/IntersectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -781,9 +781,9 @@ public function unsetOffset(Type $offsetType): Type
return $this->intersectTypes(static fn (Type $type): Type => $type->unsetOffset($offsetType));
}

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

public function getValuesArray(): Type
Expand Down
2 changes: 1 addition & 1 deletion src/Type/MixedType.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public function unsetOffset(Type $offsetType): Type
return $this;
}

public function getKeysArray(): Type
public function getKeysArray(?Type $filterValueType = null, bool $strict = false): Type
{
if ($this->isArray()->no()) {
return new ErrorType();
Expand Down
2 changes: 1 addition & 1 deletion src/Type/NeverType.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ public function unsetOffset(Type $offsetType): Type
return new NeverType();
}

public function getKeysArray(): Type
public function getKeysArray(?Type $filterValueType = null, bool $strict = false): Type
{
return new NeverType();
}
Expand Down
25 changes: 7 additions & 18 deletions src/Type/Php/ArrayKeysFunctionDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,10 @@
use PHPStan\Analyser\Scope;
use PHPStan\Php\PhpVersion;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\Accessory\AccessoryArrayListType;
use PHPStan\Type\ArrayType;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\GeneralizePrecision;
use PHPStan\Type\NeverType;
use PHPStan\Type\NullType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use function count;
use function strtolower;

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

$keysArray = $arrayType->getKeysArray();
if (count($functionCall->getArgs()) === 1) {
return $keysArray;
$strict = false;
$filterType = null;
if (count($functionCall->getArgs()) >= 2) {
$filterType = $scope->getType($functionCall->getArgs()[1]->value);
}

$newArrayType = $keysArray;
if (!$keysArray->isConstantArray()->no()) {
$newArrayType = new ArrayType(
$keysArray->getIterableKeyType()->generalize(GeneralizePrecision::lessSpecific()),
$keysArray->getIterableValueType()->generalize(GeneralizePrecision::lessSpecific()),
);
}
if ($keysArray->isList()->yes()) {
$newArrayType = TypeCombinator::intersect($newArrayType, new AccessoryArrayListType());
if (count($functionCall->getArgs()) >= 3) {
$strict = $scope->getType($functionCall->getArgs()[2]->value)->isTrue()->yes();
}
return $newArrayType;
return $arrayType->getKeysArray($filterType, $strict);
}

}
4 changes: 2 additions & 2 deletions src/Type/StaticType.php
Original file line number Diff line number Diff line change
Expand Up @@ -396,9 +396,9 @@ public function unsetOffset(Type $offsetType): Type
return $this->getStaticObjectType()->unsetOffset($offsetType);
}

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

public function getValuesArray(): Type
Expand Down
4 changes: 2 additions & 2 deletions src/Type/Traits/LateResolvableTypeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,9 @@ public function unsetOffset(Type $offsetType): Type
return $this->resolve()->unsetOffset($offsetType);
}

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

public function getValuesArray(): Type
Expand Down
2 changes: 1 addition & 1 deletion src/Type/Traits/MaybeArrayTypeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function isList(): TrinaryLogic
return TrinaryLogic::createMaybe();
}

public function getKeysArray(): Type
public function getKeysArray(?Type $filterValueType = null, bool $strict = false): Type
{
return new ErrorType();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Type/Traits/NonArrayTypeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function isList(): TrinaryLogic
return TrinaryLogic::createNo();
}

public function getKeysArray(): Type
public function getKeysArray(?Type $filterValueType = null, bool $strict = false): Type
{
return new ErrorType();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Type/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public function setExistingOffsetValueType(Type $offsetType, Type $valueType): T

public function unsetOffset(Type $offsetType): Type;

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

public function getValuesArray(): Type;

Expand Down
4 changes: 2 additions & 2 deletions src/Type/UnionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -706,9 +706,9 @@ public function unsetOffset(Type $offsetType): Type
return $this->unionTypes(static fn (Type $type): Type => $type->unsetOffset($offsetType));
}

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

public function getValuesArray(): Type
Expand Down

0 comments on commit 21fc500

Please sign in to comment.