Skip to content

Commit 9ab7458

Browse files
DanielEScherzerkocsismate
authored andcommitted
gen_stub: refactor Type::tryToSimpleType(), eliminate ::getWithoutNull()
`Type::tryToSimpleType()` tries to convert a type holding multiple simple types into a single simple type, with the following logic - if all of the inner types represent `null`, return the first of those - if all but one of the inner types represent `null`, return the non-null type - otherwise, return `null` Previously, it did this with a helper method `::getWithoutNull()`, that constructed a new `Type` containing only the inner types that did not represent `null`. However, the only thing the newly created object was used for was extracting the types it contains, so the actual object creation just adds overhead. Merge `Type::getWithoutNull()` into `Type::tryToSimpleType()` and clean up to avoid creating an unneeded object.
1 parent a4e0626 commit 9ab7458

File tree

1 file changed

+9
-18
lines changed

1 file changed

+9
-18
lines changed

build/gen_stub.php

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -626,28 +626,19 @@ public function isNullable(): bool {
626626
return false;
627627
}
628628

629-
public function getWithoutNull(): Type {
630-
return new Type(
631-
array_values(
632-
array_filter(
633-
$this->types,
634-
function(SimpleType $type) {
635-
return !$type->isNull();
636-
}
637-
)
638-
),
639-
false
640-
);
641-
}
642-
643629
public function tryToSimpleType(): ?SimpleType {
644-
$withoutNull = $this->getWithoutNull();
630+
$nonNullTypes = array_filter(
631+
$this->types,
632+
function(SimpleType $type) {
633+
return !$type->isNull();
634+
}
635+
);
645636
/* type has only null */
646-
if (count($withoutNull->types) === 0) {
637+
if (count($nonNullTypes) === 0) {
647638
return $this->types[0];
648639
}
649-
if (count($withoutNull->types) === 1) {
650-
return $withoutNull->types[0];
640+
if (count($nonNullTypes) === 1) {
641+
return reset($nonNullTypes);
651642
}
652643
return null;
653644
}

0 commit comments

Comments
 (0)