Skip to content

Commit 162a1fa

Browse files
committed
minor symfony#54669 [TypeInfo] rework isA to handle class names and improve isNullable (mtarld)
This PR was merged into the 7.1 branch. Discussion ---------- [TypeInfo] rework isA to handle class names and improve isNullable | Q | A | ------------- | --- | Branch? | 7.1 | Bug fix? | no | New feature? | no | Deprecations? | no | Issues | | License | MIT symfony#54659 introduces a better design that allows us to easily handle class names in `isA` and to simplify `isNullable` Commits ------- 5624ee0 [TypeInfo] rework isA to handle class names and improve isNullable
2 parents 96d664e + 5624ee0 commit 162a1fa

14 files changed

+69
-26
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,9 @@ public function testIsA()
4545
{
4646
$this->assertFalse((new BackedEnumType(DummyBackedEnum::class, Type::int()))->isA(TypeIdentifier::ARRAY));
4747
$this->assertTrue((new BackedEnumType(DummyBackedEnum::class, Type::int()))->isA(TypeIdentifier::OBJECT));
48+
$this->assertFalse((new BackedEnumType(DummyBackedEnum::class, Type::int()))->isA(self::class));
49+
$this->assertTrue((new BackedEnumType(DummyBackedEnum::class, Type::int()))->isA(DummyBackedEnum::class));
50+
$this->assertTrue((new BackedEnumType(DummyBackedEnum::class, Type::int()))->isA(\BackedEnum::class));
51+
$this->assertTrue((new BackedEnumType(DummyBackedEnum::class, Type::int()))->isA(\UnitEnum::class));
4852
}
4953
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,8 @@ public function testIsA()
6666
{
6767
$this->assertFalse((new BuiltinType(TypeIdentifier::INT))->isA(TypeIdentifier::ARRAY));
6868
$this->assertTrue((new BuiltinType(TypeIdentifier::INT))->isA(TypeIdentifier::INT));
69+
$this->assertFalse((new BuiltinType(TypeIdentifier::INT))->isA('array'));
70+
$this->assertTrue((new BuiltinType(TypeIdentifier::INT))->isA('int'));
71+
$this->assertFalse((new BuiltinType(TypeIdentifier::INT))->isA(self::class));
6972
}
7073
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,5 +100,12 @@ public function testIsA()
100100
$this->assertTrue($type->isA(TypeIdentifier::ARRAY));
101101
$this->assertFalse($type->isA(TypeIdentifier::STRING));
102102
$this->assertFalse($type->isA(TypeIdentifier::INT));
103+
$this->assertFalse($type->isA(self::class));
104+
105+
$type = new CollectionType(new GenericType(Type::object(self::class), Type::string(), Type::bool()));
106+
107+
$this->assertFalse($type->isA(TypeIdentifier::ARRAY));
108+
$this->assertTrue($type->isA(TypeIdentifier::OBJECT));
109+
$this->assertTrue($type->isA(self::class));
103110
}
104111
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,8 @@ public function testIsA()
4444
{
4545
$this->assertFalse((new EnumType(DummyEnum::class))->isA(TypeIdentifier::ARRAY));
4646
$this->assertTrue((new EnumType(DummyEnum::class))->isA(TypeIdentifier::OBJECT));
47+
$this->assertTrue((new EnumType(DummyEnum::class))->isA(DummyEnum::class));
48+
$this->assertTrue((new EnumType(DummyEnum::class))->isA(\UnitEnum::class));
49+
$this->assertFalse((new EnumType(DummyEnum::class))->isA(\BackedEnum::class));
4750
}
4851
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,12 @@ public function testIsA()
5454
$type = new GenericType(Type::builtin(TypeIdentifier::ARRAY), Type::string(), Type::bool());
5555
$this->assertTrue($type->isA(TypeIdentifier::ARRAY));
5656
$this->assertFalse($type->isA(TypeIdentifier::STRING));
57+
$this->assertFalse($type->isA(self::class));
5758

5859
$type = new GenericType(Type::object(self::class), Type::union(Type::bool(), Type::string()), Type::int(), Type::float());
5960
$this->assertTrue($type->isA(TypeIdentifier::OBJECT));
6061
$this->assertFalse($type->isA(TypeIdentifier::INT));
6162
$this->assertFalse($type->isA(TypeIdentifier::STRING));
63+
$this->assertTrue($type->isA(self::class));
6264
}
6365
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,7 @@ public function testIsA()
3636
{
3737
$this->assertFalse((new ObjectType(self::class))->isA(TypeIdentifier::ARRAY));
3838
$this->assertTrue((new ObjectType(self::class))->isA(TypeIdentifier::OBJECT));
39+
$this->assertTrue((new ObjectType(self::class))->isA(self::class));
40+
$this->assertFalse((new ObjectType(self::class))->isA(\stdClass::class));
3941
}
4042
}

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

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,6 @@ public function testIs()
3131
$this->assertFalse(Type::generic(Type::string(), Type::int())->is($isInt));
3232
}
3333

34-
public function testIsA()
35-
{
36-
$this->assertTrue(Type::int()->isA(TypeIdentifier::INT));
37-
$this->assertTrue(Type::union(Type::string(), Type::int())->isA(TypeIdentifier::INT));
38-
$this->assertTrue(Type::generic(Type::int(), Type::string())->isA(TypeIdentifier::INT));
39-
40-
$this->assertFalse(Type::string()->isA(TypeIdentifier::INT));
41-
$this->assertFalse(Type::union(Type::string(), Type::float())->isA(TypeIdentifier::INT));
42-
$this->assertFalse(Type::generic(Type::string(), Type::int())->isA(TypeIdentifier::INT));
43-
}
44-
4534
public function testIsNullable()
4635
{
4736
$this->assertTrue(Type::null()->isNullable());

src/Symfony/Component/TypeInfo/Type.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ abstract class Type implements \Stringable
2626

2727
abstract public function getBaseType(): BuiltinType|ObjectType;
2828

29+
/**
30+
* @param TypeIdentifier|class-string $subject
31+
*/
32+
abstract public function isA(TypeIdentifier|string $subject): bool;
33+
34+
abstract public function asNonNullable(): self;
35+
2936
/**
3037
* @param callable(Type): bool $callable
3138
*/
@@ -34,15 +41,8 @@ public function is(callable $callable): bool
3441
return $callable($this);
3542
}
3643

37-
public function isA(TypeIdentifier $typeIdentifier): bool
38-
{
39-
return $this->getBaseType()->getTypeIdentifier() === $typeIdentifier;
40-
}
41-
4244
public function isNullable(): bool
4345
{
44-
return \in_array($this->getBaseType()->getTypeIdentifier(), [TypeIdentifier::NULL, TypeIdentifier::MIXED], true);
46+
return $this->is(fn (Type $t): bool => $t->isA(TypeIdentifier::NULL) || $t->isA(TypeIdentifier::MIXED));
4547
}
46-
47-
abstract public function asNonNullable(): self;
4848
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,19 @@ public function getTypeIdentifier(): TypeIdentifier
4646
return $this->typeIdentifier;
4747
}
4848

49+
public function isA(TypeIdentifier|string $subject): bool
50+
{
51+
if ($subject instanceof TypeIdentifier) {
52+
return $this->getTypeIdentifier() === $subject;
53+
}
54+
55+
try {
56+
return TypeIdentifier::from($subject) === $this->getTypeIdentifier();
57+
} catch (\ValueError) {
58+
return false;
59+
}
60+
}
61+
4962
/**
5063
* @return self|UnionType<BuiltinType<TypeIdentifier::OBJECT>|BuiltinType<TypeIdentifier::RESOURCE>|BuiltinType<TypeIdentifier::ARRAY>|BuiltinType<TypeIdentifier::STRING>|BuiltinType<TypeIdentifier::FLOAT>|BuiltinType<TypeIdentifier::INT>|BuiltinType<TypeIdentifier::BOOL>>
5164
*/

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ public function getType(): BuiltinType|ObjectType|GenericType
5858
return $this->type;
5959
}
6060

61+
public function isA(TypeIdentifier|string $subject): bool
62+
{
63+
return $this->getType()->isA($subject);
64+
}
65+
6166
public function isList(): bool
6267
{
6368
return $this->isList;

0 commit comments

Comments
 (0)