Skip to content

Commit

Permalink
[PHPStanStaticTypeMapper] Reduce bool check on UnionTypeMapper (#3778)
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed May 9, 2023
1 parent cb30fd9 commit 6655c2c
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Rector\PHPStanStaticTypeMapper\TypeAnalyzer;

use PHPStan\Type\NullType;
use PHPStan\Type\UnionType;

final class BoolUnionTypeAnalyzer
Expand All @@ -19,23 +18,4 @@ public function isBoolUnionType(UnionType $unionType): bool

return true;
}

public function isNullableBoolUnionType(UnionType $unionType): bool
{
$hasNullable = false;
foreach ($unionType->getTypes() as $unionedType) {
if ($unionedType instanceof NullType) {
$hasNullable = true;
continue;
}

if ($unionedType->isBoolean()->yes()) {
continue;
}

return false;
}

return $hasNullable;
}
}
30 changes: 0 additions & 30 deletions packages/PHPStanStaticTypeMapper/TypeMapper/UnionTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,19 +105,6 @@ public function mapToPhpParserNode(Type $type, string $typeKind): ?Node
return $arrayNode;
}

if ($this->boolUnionTypeAnalyzer->isNullableBoolUnionType($type)
&& ! $this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::UNION_TYPES)
) {
return $this->resolveNullableType(new NullableType(new Identifier('bool')));
}

if (! $this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::UNION_TYPES) && $this->isFalseBoolUnion(
$type
)) {
// return new Bool
return new Identifier('bool');
}

// special case for nullable
$nullabledType = $this->matchTypeForNullableUnionType($type);
if (! $nullabledType instanceof Type) {
Expand Down Expand Up @@ -481,23 +468,6 @@ private function correctObjectType(TypeWithClassName $typeWithClassName): TypeWi
return $typeWithClassName;
}

private function isFalseBoolUnion(UnionType $unionType): bool
{
if (count($unionType->getTypes()) !== 2) {
return false;
}

foreach ($unionType->getTypes() as $unionedType) {
if ($unionedType instanceof ConstantBooleanType) {
continue;
}

return false;
}

return true;
}

private function narrowIntegerType(UnionType $unionType): ?Identifier
{
foreach ($unionType->getTypes() as $unionedType) {
Expand Down

0 comments on commit 6655c2c

Please sign in to comment.