1
0
mirror of https://github.com/php/php-src.git synced 2026-04-27 18:23:26 +02:00

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.
This commit is contained in:
Daniel Scherzer
2024-10-04 18:27:43 -07:00
committed by Máté Kocsis
parent a4e062600f
commit 9ab74588d5
+9 -18
View File
@@ -626,28 +626,19 @@ class Type {
return false;
}
public function getWithoutNull(): Type {
return new Type(
array_values(
array_filter(
$this->types,
function(SimpleType $type) {
return !$type->isNull();
}
)
),
false
);
}
public function tryToSimpleType(): ?SimpleType {
$withoutNull = $this->getWithoutNull();
$nonNullTypes = array_filter(
$this->types,
function(SimpleType $type) {
return !$type->isNull();
}
);
/* type has only null */
if (count($withoutNull->types) === 0) {
if (count($nonNullTypes) === 0) {
return $this->types[0];
}
if (count($withoutNull->types) === 1) {
return $withoutNull->types[0];
if (count($nonNullTypes) === 1) {
return reset($nonNullTypes);
}
return null;
}