Skip to content

Commit

Permalink
[PHPStanStaticTypeMapper] Clean up tweak false and bool check on Unio…
Browse files Browse the repository at this point in the history
…nTypeMapper (#5209)
  • Loading branch information
samsonasik committed Oct 28, 2023
1 parent 5f86e6b commit 6f33010
Showing 1 changed file with 4 additions and 29 deletions.
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

0 comments on commit 6f33010

Please sign in to comment.