Skip to content

Commit e02306b

Browse files
committed
feature symfony#54670 [TypeInfo] Ease getting base type on nullable types (mtarld)
This PR was merged into the 7.1 branch. Discussion ---------- [TypeInfo] Ease getting base type on nullable types | Q | A | ------------- | --- | Branch? | 7.1 | Bug fix? | no | New feature? | no | Deprecations? | no | Issues | | License | MIT Allow retrieving base type on "nullable union types" (ie: `string|null`) without getting a `LogicException`. Commits ------- 2b73bc9 [TypeInfo] Ease getting base type on nullable types
2 parents 9a86584 + 2b73bc9 commit e02306b

12 files changed

+70
-15
lines changed

src/Symfony/Component/TypeInfo/Tests/Type/BackedEnumTypeTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ public function testIsNullable()
2929
$this->assertFalse((new BackedEnumType(DummyBackedEnum::class, Type::int()))->isNullable());
3030
}
3131

32+
public function testGetBaseType()
33+
{
34+
$this->assertEquals(new BackedEnumType(DummyBackedEnum::class, Type::int()), (new BackedEnumType(DummyBackedEnum::class, Type::int()))->getBaseType());
35+
}
36+
3237
public function testAsNonNullable()
3338
{
3439
$type = new BackedEnumType(DummyBackedEnum::class, Type::int());

src/Symfony/Component/TypeInfo/Tests/Type/BuiltinTypeTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ public function testToString()
2424
$this->assertSame('int', (string) new BuiltinType(TypeIdentifier::INT));
2525
}
2626

27+
public function testGetBaseType()
28+
{
29+
$this->assertEquals(new BuiltinType(TypeIdentifier::INT), (new BuiltinType(TypeIdentifier::INT))->getBaseType());
30+
}
31+
2732
public function testIsNullable()
2833
{
2934
$this->assertFalse((new BuiltinType(TypeIdentifier::INT))->isNullable());

src/Symfony/Component/TypeInfo/Tests/Type/CollectionTypeTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ public function testToString()
7474
$this->assertEquals('array<string,bool>', (string) $type);
7575
}
7676

77+
public function testGetBaseType()
78+
{
79+
$this->assertEquals(Type::int(), Type::collection(Type::generic(Type::int(), Type::string()))->getBaseType());
80+
}
81+
7782
public function testIsNullable()
7883
{
7984
$this->assertFalse((new CollectionType(Type::generic(Type::builtin(TypeIdentifier::ARRAY), Type::int())))->isNullable());

src/Symfony/Component/TypeInfo/Tests/Type/EnumTypeTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ public function testToString()
2323
$this->assertSame(DummyEnum::class, (string) new EnumType(DummyEnum::class));
2424
}
2525

26+
public function testGetBaseType()
27+
{
28+
$this->assertEquals(new EnumType(DummyEnum::class), (new EnumType(DummyEnum::class))->getBaseType());
29+
}
30+
2631
public function testIsNullable()
2732
{
2833
$this->assertFalse((new EnumType(DummyEnum::class))->isNullable());

src/Symfony/Component/TypeInfo/Tests/Type/GenericTypeTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ public function testToString()
3030
$this->assertEquals(sprintf('%s<bool|string,int,float>', self::class), (string) $type);
3131
}
3232

33+
public function testGetBaseType()
34+
{
35+
$this->assertEquals(Type::object(), Type::generic(Type::object(), Type::int())->getBaseType());
36+
}
37+
3338
public function testIsNullable()
3439
{
3540
$this->assertFalse((new GenericType(Type::builtin(TypeIdentifier::ARRAY), Type::int()))->isNullable());

src/Symfony/Component/TypeInfo/Tests/Type/IntersectionTypeTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ public function testEveryTypeIs()
5656
$this->assertFalse($type->everyTypeIs(fn (Type $t) => $t instanceof BuiltinType));
5757
}
5858

59+
public function testGetBaseType()
60+
{
61+
$this->expectException(LogicException::class);
62+
(new IntersectionType(Type::string(), Type::int()))->getBaseType();
63+
}
64+
5965
public function testToString()
6066
{
6167
$type = new IntersectionType(Type::int(), Type::string(), Type::float());

src/Symfony/Component/TypeInfo/Tests/Type/ObjectTypeTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ public function testIsNullable()
2727
$this->assertFalse((new ObjectType(self::class))->isNullable());
2828
}
2929

30+
public function testGetBaseType()
31+
{
32+
$this->assertEquals(new ObjectType(self::class), (new ObjectType(self::class))->getBaseType());
33+
}
34+
3035
public function testIsA()
3136
{
3237
$this->assertFalse((new ObjectType(self::class))->isA(TypeIdentifier::ARRAY));

src/Symfony/Component/TypeInfo/Tests/Type/UnionTypeTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\TypeInfo\Exception\InvalidArgumentException;
16+
use Symfony\Component\TypeInfo\Exception\LogicException;
1617
use Symfony\Component\TypeInfo\Type;
1718
use Symfony\Component\TypeInfo\Type\BuiltinType;
1819
use Symfony\Component\TypeInfo\Type\UnionType;
@@ -66,6 +67,14 @@ public function testAsNonNullable()
6667
], $type->asNonNullable()->getTypes());
6768
}
6869

70+
public function testGetBaseType()
71+
{
72+
$this->assertEquals(Type::string(), (new UnionType(Type::string(), Type::null()))->getBaseType());
73+
74+
$this->expectException(LogicException::class);
75+
(new UnionType(Type::string(), Type::int(), Type::null()))->getBaseType();
76+
}
77+
6978
public function testAtLeastOneTypeIs()
7079
{
7180
$type = new UnionType(Type::int(), Type::string(), Type::bool());

src/Symfony/Component/TypeInfo/Tests/TypeTest.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,6 @@ public function testIsNullable()
5757
$this->assertFalse(Type::generic(Type::int(), Type::mixed())->isNullable());
5858
}
5959

60-
public function testGetBaseType()
61-
{
62-
$this->assertEquals(Type::string(), Type::string()->getBaseType());
63-
$this->assertEquals(Type::object(self::class), Type::object(self::class)->getBaseType());
64-
$this->assertEquals(Type::object(), Type::generic(Type::object(), Type::int())->getBaseType());
65-
$this->assertEquals(Type::builtin(TypeIdentifier::ARRAY), Type::list()->getBaseType());
66-
$this->assertEquals(Type::int(), Type::collection(Type::generic(Type::int(), Type::string()))->getBaseType());
67-
}
68-
6960
public function testCannotGetBaseTypeOnCompoundType()
7061
{
7162
$this->expectException(LogicException::class);

src/Symfony/Component/TypeInfo/Type/CompositeTypeTrait.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace Symfony\Component\TypeInfo\Type;
1313

1414
use Symfony\Component\TypeInfo\Exception\InvalidArgumentException;
15-
use Symfony\Component\TypeInfo\Exception\LogicException;
1615
use Symfony\Component\TypeInfo\Type;
1716
use Symfony\Component\TypeInfo\TypeIdentifier;
1817

@@ -50,11 +49,6 @@ public function __construct(Type ...$types)
5049
$this->types = array_values(array_unique($types));
5150
}
5251

53-
public function getBaseType(): BuiltinType|ObjectType
54-
{
55-
throw new LogicException(sprintf('Cannot get base type on "%s" compound type.', $this));
56-
}
57-
5852
public function isA(TypeIdentifier $typeIdentifier): bool
5953
{
6054
return $this->is(fn (Type $type) => $type->isA($typeIdentifier));

0 commit comments

Comments
 (0)