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

[PHPStanStaticTypeMapper] Clean up tweak false and bool check on UnionTypeMapper #5209

Merged
merged 1 commit into from
Oct 28, 2023
Merged
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
33 changes: 4 additions & 29 deletions packages/PHPStanStaticTypeMapper/TypeMapper/UnionTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,14 @@ public function resolveTypeWithNullablePHPParserUnionType(
Assert::isAnyOf($firstType, [Name::class, Identifier::class]);
Assert::isAnyOf($secondType, [Name::class, Identifier::class]);
} catch (InvalidArgumentException) {
return $this->resolveUnionTypes($phpParserUnionType, $totalTypes);
return $this->resolveUnionTypes($phpParserUnionType);
}

$firstTypeValue = $firstType->toString();
$secondTypeValue = $secondType->toString();

if ($firstTypeValue === $secondTypeValue) {
return $this->resolveUnionTypes($phpParserUnionType, $totalTypes);
return $this->resolveUnionTypes($phpParserUnionType);
}

if ($firstTypeValue === 'null') {
Expand All @@ -133,7 +133,7 @@ public function resolveTypeWithNullablePHPParserUnionType(
}
}

return $this->resolveUnionTypes($phpParserUnionType, $totalTypes);
return $this->resolveUnionTypes($phpParserUnionType);
}

private function resolveNullableType(NullableType $nullableType): null|NullableType|PhpParserUnionType
Expand Down Expand Up @@ -199,37 +199,12 @@ private function shouldSkipIterable(UnionType $unionType): bool
return $unionTypeAnalysis->hasArray();
}

private function resolveUnionTypes(PhpParserUnionType $phpParserUnionType, int $totalTypes): ?PhpParserUnionType
private function resolveUnionTypes(PhpParserUnionType $phpParserUnionType): ?PhpParserUnionType
{
if (! $this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::UNION_TYPES)) {
return null;
}

if ($totalTypes === 2) {
return $phpParserUnionType;
}

$identifierNames = [];
foreach ($phpParserUnionType->types as $type) {
if ($type instanceof Identifier) {
$identifierNames[] = $type->toString();
}
}

if (! in_array('bool', $identifierNames, true)) {
return $phpParserUnionType;
}

if (! in_array('false', $identifierNames, true)) {
return $phpParserUnionType;
}

$phpParserUnionType->types = array_filter(
$phpParserUnionType->types,
static fn (Node $node): bool => ! $node instanceof Identifier || $node->toString() !== 'false'
);
$phpParserUnionType->types = array_values($phpParserUnionType->types);

return $phpParserUnionType;
}

Expand Down