Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Type->getIterableKeyType() over ArrayType->getKeyType() #6480

Merged
merged 1 commit into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ private function shouldSkipArrayForInvalidTypeOrKeys(Expr $expr): bool

private function isArrayKeyTypeAllowed(ArrayType|ConstantArrayType $arrayType): bool
{
if ($arrayType->getKeyType()->isInteger()->yes()) {
if ($arrayType->getIterableKeyType()->isInteger()->yes()) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ private function shouldAddReturnArrayDocType(ArrayType|ConstantArrayType $arrayT
}

// handle only simple arrays
if (! $arrayType->getKeyType()->isInteger()->yes()) {
if (! $arrayType->getIterableKeyType()->isInteger()->yes()) {
return false;
}
}
Expand Down
4 changes: 2 additions & 2 deletions rules/TypeDeclaration/TypeNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function normalizeArrayOfUnionToUnionArray(Type $type, int $arrayNesting
$this->collectedNestedArrayTypes[] = new NestedArrayType(
$type->getIterableValueType(),
$arrayNesting,
$type->getKeyType()
$type->getIterableKeyType()
);
}

Expand Down Expand Up @@ -95,7 +95,7 @@ public function normalizeArrayTypeAndArrayNever(Type $type): Type

private function isConstantArrayNever(Type $type): bool
{
return $type instanceof ConstantArrayType && $type->getKeyType() instanceof NeverType && $type->getIterableValueType() instanceof NeverType;
return $type instanceof ConstantArrayType && $type->getIterableKeyType() instanceof NeverType && $type->getIterableValueType() instanceof NeverType;
}

private function collectNestedArrayTypeFromUnionType(UnionType $unionType, int $arrayNesting): void
Expand Down
2 changes: 1 addition & 1 deletion src/NodeTypeResolver/PHPStan/Type/TypeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ private function unwrapConstantArrayTypes(ConstantArrayType $constantArrayType):
{
$unwrappedTypes = [];

$flattenKeyTypes = TypeUtils::flattenTypes($constantArrayType->getKeyType());
$flattenKeyTypes = TypeUtils::flattenTypes($constantArrayType->getIterableKeyType());
$flattenItemTypes = TypeUtils::flattenTypes($constantArrayType->getIterableValueType());

foreach ($flattenItemTypes as $position => $nestedFlattenItemType) {
Expand Down
2 changes: 1 addition & 1 deletion src/NodeTypeResolver/PHPStan/TypeHasher.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function createTypeHash(Type $type): string
}

if ($type instanceof ArrayType) {
return $this->createTypeHash($type->getIterableValueType()) . $this->createTypeHash($type->getKeyType()) . '[]';
return $this->createTypeHash($type->getIterableValueType()) . $this->createTypeHash($type->getIterableKeyType()) . '[]';
}

if ($type instanceof GenericObjectType) {
Expand Down
4 changes: 2 additions & 2 deletions src/NodeTypeResolver/TypeComparator/ArrayTypeComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public function isSubtype(ArrayType|ConstantArrayType $checkedType, ArrayType|Co
->yes();
}

$checkedKeyType = $checkedType->getKeyType();
$mainKeyType = $mainType->getKeyType();
$checkedKeyType = $checkedType->getIterableKeyType();
$mainKeyType = $mainType->getIterableKeyType();

if (! $mainKeyType instanceof MixedType && $mainKeyType->isSuperTypeOf($checkedKeyType)->yes()) {
return true;
Expand Down