Skip to content

Commit 931e6e2

Browse files
samsonasikactions-user
andauthoredApr 26, 2023
[PHPStanStaticTypeMapper] Improve UnionTypeMapper performance take 2 (#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>
1 parent bd478cc commit 931e6e2

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed
 

‎packages/PHPStanStaticTypeMapper/TypeMapper/UnionTypeMapper.php

+11-10
Original file line numberDiff line numberDiff line change
@@ -246,29 +246,30 @@ private function resolveUnionTypes(PhpParserUnionType $phpParserUnionType): ?Php
246246
return null;
247247
}
248248

249+
if (count($phpParserUnionType->types) === 2) {
250+
return $phpParserUnionType;
251+
}
252+
249253
$identifierNames = [];
250254
foreach ($phpParserUnionType->types as $type) {
251255
if ($type instanceof Identifier) {
252256
$identifierNames[] = $type->toString();
253257
}
254258
}
255-
259+
256260
if (! in_array('bool', $identifierNames, true)) {
257261
return $phpParserUnionType;
258262
}
259-
263+
260264
if (! in_array('false', $identifierNames, true)) {
261265
return $phpParserUnionType;
262266
}
263267

264-
foreach ($phpParserUnionType->types as $key => $type) {
265-
if ($type instanceof Identifier && $type->toString() === 'false') {
266-
unset($phpParserUnionType->types[$key]);
267-
$phpParserUnionType->types = array_values($phpParserUnionType->types);
268-
269-
return $phpParserUnionType;
270-
}
271-
}
268+
$phpParserUnionType->types = array_filter(
269+
$phpParserUnionType->types,
270+
static fn(Node $node): bool => ! $node instanceof Identifier || $node->toString() !== 'false'
271+
);
272+
$phpParserUnionType->types = array_values($phpParserUnionType->types);
272273

273274
return $phpParserUnionType;
274275
}

0 commit comments

Comments
 (0)