Skip to content

Commit 572209f

Browse files
committed
minor symfony#54659 [TypeInfo] rework the base Type class to not depend on subclasses (xabbuh)
This PR was merged into the 7.1 branch. Discussion ---------- [TypeInfo] rework the base Type class to not depend on subclasses | Q | A | ------------- | --- | Branch? | 7.1 | Bug fix? | no | New feature? | no | Deprecations? | no | Issues | | License | MIT Commits ------- 3738245 rework the base Type class to not depend on subclasses
2 parents a2d03c5 + 3738245 commit 572209f

File tree

9 files changed

+57
-47
lines changed

9 files changed

+57
-47
lines changed

src/Symfony/Component/TypeInfo/Type.php

Lines changed: 4 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,8 @@
1111

1212
namespace Symfony\Component\TypeInfo;
1313

14-
use Symfony\Component\TypeInfo\Exception\LogicException;
1514
use Symfony\Component\TypeInfo\Type\BuiltinType;
16-
use Symfony\Component\TypeInfo\Type\CollectionType;
17-
use Symfony\Component\TypeInfo\Type\GenericType;
18-
use Symfony\Component\TypeInfo\Type\IntersectionType;
1915
use Symfony\Component\TypeInfo\Type\ObjectType;
20-
use Symfony\Component\TypeInfo\Type\UnionType;
2116

2217
/**
2318
* @author Mathias Arlaud <[email protected]>
@@ -27,63 +22,25 @@ abstract class Type implements \Stringable
2722
{
2823
use TypeFactoryTrait;
2924

30-
public function getBaseType(): BuiltinType|ObjectType
31-
{
32-
if ($this instanceof UnionType || $this instanceof IntersectionType) {
33-
throw new LogicException(sprintf('Cannot get base type on "%s" compound type.', (string) $this));
34-
}
35-
36-
$baseType = $this;
37-
38-
if ($baseType instanceof CollectionType) {
39-
$baseType = $baseType->getType();
40-
}
41-
42-
if ($baseType instanceof GenericType) {
43-
$baseType = $baseType->getType();
44-
}
45-
46-
return $baseType;
47-
}
25+
abstract public function getBaseType(): BuiltinType|ObjectType;
4826

4927
/**
5028
* @param callable(Type): bool $callable
5129
*/
5230
public function is(callable $callable): bool
5331
{
54-
return match (true) {
55-
$this instanceof UnionType => $this->atLeastOneTypeIs($callable),
56-
$this instanceof IntersectionType => $this->everyTypeIs($callable),
57-
default => $callable($this),
58-
};
32+
return $callable($this);
5933
}
6034

6135
public function isA(TypeIdentifier $typeIdentifier): bool
6236
{
63-
return $this->testIdentifier(fn (TypeIdentifier $i): bool => $typeIdentifier === $i);
37+
return $this->getBaseType()->getTypeIdentifier() === $typeIdentifier;
6438
}
6539

6640
public function isNullable(): bool
6741
{
68-
return $this->testIdentifier(fn (TypeIdentifier $i): bool => TypeIdentifier::NULL === $i || TypeIdentifier::MIXED === $i);
42+
return \in_array($this->getBaseType()->getTypeIdentifier(), [TypeIdentifier::NULL, TypeIdentifier::MIXED], true);
6943
}
7044

7145
abstract public function asNonNullable(): self;
72-
73-
/**
74-
* @param callable(TypeIdentifier): bool $test
75-
*/
76-
private function testIdentifier(callable $test): bool
77-
{
78-
$callable = function (self $t) use ($test, &$callable): bool {
79-
// unwrap compound type to forward type identifier check
80-
if ($t instanceof UnionType || $t instanceof IntersectionType) {
81-
return $t->is($callable);
82-
}
83-
84-
return $test($t->getBaseType()->getTypeIdentifier());
85-
};
86-
87-
return $this->is($callable);
88-
}
8946
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ public function __construct(
3131
) {
3232
}
3333

34+
public function getBaseType(): self|ObjectType
35+
{
36+
return $this;
37+
}
38+
3439
/**
3540
* @return T
3641
*/

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ public function __construct(
4343
}
4444
}
4545

46+
public function getBaseType(): BuiltinType|ObjectType
47+
{
48+
return $this->getType()->getBaseType();
49+
}
50+
4651
/**
4752
* @return T
4853
*/

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

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

1414
use Symfony\Component\TypeInfo\Exception\InvalidArgumentException;
15+
use Symfony\Component\TypeInfo\Exception\LogicException;
1516
use Symfony\Component\TypeInfo\Type;
17+
use Symfony\Component\TypeInfo\TypeIdentifier;
1618

1719
/**
1820
* @author Mathias Arlaud <[email protected]>
@@ -48,6 +50,21 @@ public function __construct(Type ...$types)
4850
$this->types = array_values(array_unique($types));
4951
}
5052

53+
public function getBaseType(): BuiltinType|ObjectType
54+
{
55+
throw new LogicException(sprintf('Cannot get base type on "%s" compound type.', $this));
56+
}
57+
58+
public function isA(TypeIdentifier $typeIdentifier): bool
59+
{
60+
return $this->is(fn (Type $type) => $type->isA($typeIdentifier));
61+
}
62+
63+
public function isNullable(): bool
64+
{
65+
return $this->is(fn (Type $type) => $type->isNullable());
66+
}
67+
5168
/**
5269
* @return list<T>
5370
*/

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ public function __construct(
4141
$this->variableTypes = $variableTypes;
4242
}
4343

44+
public function getBaseType(): BuiltinType|ObjectType
45+
{
46+
return $this->getType();
47+
}
48+
4449
/**
4550
* @return T
4651
*/

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ final class IntersectionType extends Type
2727
*/
2828
use CompositeTypeTrait;
2929

30+
public function is(callable $callable): bool
31+
{
32+
return $this->everyTypeIs($callable);
33+
}
34+
3035
public function __toString(): string
3136
{
3237
$string = '';

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ public function __construct(
3030
) {
3131
}
3232

33+
public function getBaseType(): BuiltinType|self
34+
{
35+
return $this;
36+
}
37+
3338
public function getTypeIdentifier(): TypeIdentifier
3439
{
3540
return TypeIdentifier::OBJECT;

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\TypeInfo\Type;
1313

14+
use Symfony\Component\TypeInfo\Exception\LogicException;
1415
use Symfony\Component\TypeInfo\Type;
1516

1617
/**
@@ -27,6 +28,11 @@ public function __construct(
2728
) {
2829
}
2930

31+
public function getBaseType(): BuiltinType|ObjectType
32+
{
33+
throw new LogicException(sprintf('Cannot get base type on "%s" template type.', $this));
34+
}
35+
3036
public function getName(): string
3137
{
3238
return $this->name;

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ final class UnionType extends Type
2727
*/
2828
use CompositeTypeTrait;
2929

30+
public function is(callable $callable): bool
31+
{
32+
return $this->atLeastOneTypeIs($callable);
33+
}
34+
3035
public function asNonNullable(): Type
3136
{
3237
$nonNullableTypes = [];

0 commit comments

Comments
 (0)