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

Utilize PHPStan Type interface in TypeNormalizer #6478

Closed
wants to merge 1 commit into from
Closed
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
17 changes: 9 additions & 8 deletions rules/TypeDeclaration/TypeNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ public function __construct(
*/
public function normalizeArrayOfUnionToUnionArray(Type $type, int $arrayNesting = 1): Type
{
if (! $type instanceof ArrayType && ! $type instanceof ConstantArrayType) {
if (! $type->isArray()->yes()) {
return $type;
}

if ($type instanceof ConstantArrayType && $arrayNesting === 1) {
if ($type->isConstantArray()->yes() && $arrayNesting === 1) {
return $type;
}

Expand All @@ -52,16 +52,17 @@ public function normalizeArrayOfUnionToUnionArray(Type $type, int $arrayNesting
$this->collectedNestedArrayTypes = [];
}

if ($type->getItemType() instanceof ArrayType) {
$itemType = $type->getIterableValueType();
if ($itemType instanceof ArrayType) {
++$arrayNesting;
$this->normalizeArrayOfUnionToUnionArray($type->getItemType(), $arrayNesting);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the root cause is that we call getItemType which is not defined in Type, therefore will crash depending on the used type

} elseif ($type->getItemType() instanceof UnionType) {
$this->collectNestedArrayTypeFromUnionType($type->getItemType(), $arrayNesting);
$this->normalizeArrayOfUnionToUnionArray($itemType, $arrayNesting);
} elseif ($itemType instanceof UnionType) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can be IntersectionType, see PR patch that happen on downgrade:

due to downgrade error:

until we have more fixture tests, I think we can keep using strict ArrayType and ConstantArrayType to avoid regression.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, lets do it less aggressive: #6479

$this->collectNestedArrayTypeFromUnionType($itemType, $arrayNesting);
} else {
$this->collectedNestedArrayTypes[] = new NestedArrayType(
$type->getItemType(),
$itemType,
$arrayNesting,
$type->getKeyType()
$type->getIterableKeyType()
);
}

Expand Down