Skip to content

Commit

Permalink
UnionType::pickTypes overriden in BenevolentUnionType for a more bene…
Browse files Browse the repository at this point in the history
…volent behaviour
  • Loading branch information
ondrejmirtes committed Jan 4, 2023
1 parent 6debffd commit 480626e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 41 deletions.
13 changes: 13 additions & 0 deletions src/Type/BenevolentUnionType.php
Expand Up @@ -43,6 +43,19 @@ protected function unionTypes(callable $getType): Type
return TypeUtils::toBenevolentUnion(TypeCombinator::union(...$resultTypes));
}

protected function pickTypes(callable $getTypes): array
{
$types = [];
foreach ($this->getTypes() as $type) {
$innerTypes = $getTypes($type);
foreach ($innerTypes as $innerType) {
$types[] = $innerType;
}
}

return $types;
}

public function getOffsetValueType(Type $offsetType): Type
{
$types = [];
Expand Down
66 changes: 25 additions & 41 deletions src/Type/UnionType.php
Expand Up @@ -114,55 +114,17 @@ public function getReferencedClasses(): array

public function getArrays(): array
{
$arrays = [];
foreach ($this->types as $type) {
$innerTypeArrays = $type->getArrays();
if ($innerTypeArrays === []) {
return [];
}

foreach ($innerTypeArrays as $array) {
$arrays[] = $array;
}
}

return $arrays;
return $this->pickTypes(static fn (Type $type) => $type->getArrays());
}

public function getConstantArrays(): array
{
$constantArrays = [];
foreach ($this->types as $type) {
$typeAsConstantArrays = $type->getConstantArrays();

if ($typeAsConstantArrays === []) {
return [];
}

foreach ($typeAsConstantArrays as $constantArray) {
$constantArrays[] = $constantArray;
}
}

return $constantArrays;
return $this->pickTypes(static fn (Type $type) => $type->getConstantArrays());
}

public function getConstantStrings(): array
{
$strings = [];
foreach ($this->types as $type) {
$constantStrings = $type->getConstantStrings();

if ($constantStrings === []) {
return [];
}

foreach ($constantStrings as $string) {
$strings[] = $string;
}
}

return $strings;
return $this->pickTypes(static fn (Type $type) => $type->getConstantStrings());
}

public function accepts(Type $type, bool $strictTypes): TrinaryLogic
Expand Down Expand Up @@ -929,4 +891,26 @@ protected function unionTypes(callable $getType): Type
return TypeCombinator::union(...array_map($getType, $this->types));
}

/**
* @template T of Type
* @param callable(Type $type): list<T> $getTypes
* @return list<T>
*/
protected function pickTypes(callable $getTypes): array
{
$types = [];
foreach ($this->types as $type) {
$innerTypes = $getTypes($type);
if ($innerTypes === []) {
return [];
}

foreach ($innerTypes as $innerType) {
$types[] = $innerType;
}
}

return $types;
}

}
9 changes: 9 additions & 0 deletions tests/PHPStan/Type/UnionTypeTest.php
Expand Up @@ -1407,6 +1407,15 @@ public function dataGetConstantStrings(): iterable
"'bar'",
],
],
[
new BenevolentUnionType([
new ConstantStringType('foo'),
new NullType(),
]),
[
"'foo'",
],
],
];
}

Expand Down

0 comments on commit 480626e

Please sign in to comment.