Skip to content

Commit

Permalink
[PHPStanStaticTypeMapper] Improve UnionTypeMapper performance take 2 (#…
Browse files Browse the repository at this point in the history
…3688)

* [PHPStanStaticTypeMapper] Improve UnionTypeMapper take 2

No need to verify bool and false exists when count types only 2 as already verified previously for all bool

* clean up

* [ci-review] Rector Rectify

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user committed Apr 26, 2023
1 parent bd478cc commit 931e6e2
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions packages/PHPStanStaticTypeMapper/TypeMapper/UnionTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,29 +246,30 @@ private function resolveUnionTypes(PhpParserUnionType $phpParserUnionType): ?Php
return null;
}

if (count($phpParserUnionType->types) === 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;
}

foreach ($phpParserUnionType->types as $key => $type) {
if ($type instanceof Identifier && $type->toString() === 'false') {
unset($phpParserUnionType->types[$key]);
$phpParserUnionType->types = array_values($phpParserUnionType->types);

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 931e6e2

Please sign in to comment.