Skip to content

Commit

Permalink
minor #54659 [TypeInfo] rework the base Type class to not depend on s…
Browse files Browse the repository at this point in the history
…ubclasses (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
  • Loading branch information
xabbuh committed Apr 18, 2024
2 parents a2d03c5 + 3738245 commit 572209f
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 47 deletions.
51 changes: 4 additions & 47 deletions src/Symfony/Component/TypeInfo/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,8 @@

namespace Symfony\Component\TypeInfo;

use Symfony\Component\TypeInfo\Exception\LogicException;
use Symfony\Component\TypeInfo\Type\BuiltinType;
use Symfony\Component\TypeInfo\Type\CollectionType;
use Symfony\Component\TypeInfo\Type\GenericType;
use Symfony\Component\TypeInfo\Type\IntersectionType;
use Symfony\Component\TypeInfo\Type\ObjectType;
use Symfony\Component\TypeInfo\Type\UnionType;

/**
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
Expand All @@ -27,63 +22,25 @@ abstract class Type implements \Stringable
{
use TypeFactoryTrait;

public function getBaseType(): BuiltinType|ObjectType
{
if ($this instanceof UnionType || $this instanceof IntersectionType) {
throw new LogicException(sprintf('Cannot get base type on "%s" compound type.', (string) $this));
}

$baseType = $this;

if ($baseType instanceof CollectionType) {
$baseType = $baseType->getType();
}

if ($baseType instanceof GenericType) {
$baseType = $baseType->getType();
}

return $baseType;
}
abstract public function getBaseType(): BuiltinType|ObjectType;

/**
* @param callable(Type): bool $callable
*/
public function is(callable $callable): bool
{
return match (true) {
$this instanceof UnionType => $this->atLeastOneTypeIs($callable),
$this instanceof IntersectionType => $this->everyTypeIs($callable),
default => $callable($this),
};
return $callable($this);
}

public function isA(TypeIdentifier $typeIdentifier): bool
{
return $this->testIdentifier(fn (TypeIdentifier $i): bool => $typeIdentifier === $i);
return $this->getBaseType()->getTypeIdentifier() === $typeIdentifier;
}

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

abstract public function asNonNullable(): self;

/**
* @param callable(TypeIdentifier): bool $test
*/
private function testIdentifier(callable $test): bool
{
$callable = function (self $t) use ($test, &$callable): bool {
// unwrap compound type to forward type identifier check
if ($t instanceof UnionType || $t instanceof IntersectionType) {
return $t->is($callable);
}

return $test($t->getBaseType()->getTypeIdentifier());
};

return $this->is($callable);
}
}
5 changes: 5 additions & 0 deletions src/Symfony/Component/TypeInfo/Type/BuiltinType.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public function __construct(
) {
}

public function getBaseType(): self|ObjectType
{
return $this;
}

/**
* @return T
*/
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/TypeInfo/Type/CollectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public function __construct(
}
}

public function getBaseType(): BuiltinType|ObjectType
{
return $this->getType()->getBaseType();
}

/**
* @return T
*/
Expand Down
17 changes: 17 additions & 0 deletions src/Symfony/Component/TypeInfo/Type/CompositeTypeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
namespace Symfony\Component\TypeInfo\Type;

use Symfony\Component\TypeInfo\Exception\InvalidArgumentException;
use Symfony\Component\TypeInfo\Exception\LogicException;
use Symfony\Component\TypeInfo\Type;
use Symfony\Component\TypeInfo\TypeIdentifier;

/**
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
Expand Down Expand Up @@ -48,6 +50,21 @@ public function __construct(Type ...$types)
$this->types = array_values(array_unique($types));
}

public function getBaseType(): BuiltinType|ObjectType
{
throw new LogicException(sprintf('Cannot get base type on "%s" compound type.', $this));
}

public function isA(TypeIdentifier $typeIdentifier): bool
{
return $this->is(fn (Type $type) => $type->isA($typeIdentifier));
}

public function isNullable(): bool
{
return $this->is(fn (Type $type) => $type->isNullable());
}

/**
* @return list<T>
*/
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/TypeInfo/Type/GenericType.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ public function __construct(
$this->variableTypes = $variableTypes;
}

public function getBaseType(): BuiltinType|ObjectType
{
return $this->getType();
}

/**
* @return T
*/
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/TypeInfo/Type/IntersectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ final class IntersectionType extends Type
*/
use CompositeTypeTrait;

public function is(callable $callable): bool
{
return $this->everyTypeIs($callable);
}

public function __toString(): string
{
$string = '';
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/TypeInfo/Type/ObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ public function __construct(
) {
}

public function getBaseType(): BuiltinType|self
{
return $this;
}

public function getTypeIdentifier(): TypeIdentifier
{
return TypeIdentifier::OBJECT;
Expand Down
6 changes: 6 additions & 0 deletions src/Symfony/Component/TypeInfo/Type/TemplateType.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\TypeInfo\Type;

use Symfony\Component\TypeInfo\Exception\LogicException;
use Symfony\Component\TypeInfo\Type;

/**
Expand All @@ -27,6 +28,11 @@ public function __construct(
) {
}

public function getBaseType(): BuiltinType|ObjectType
{
throw new LogicException(sprintf('Cannot get base type on "%s" template type.', $this));
}

public function getName(): string
{
return $this->name;
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/TypeInfo/Type/UnionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ final class UnionType extends Type
*/
use CompositeTypeTrait;

public function is(callable $callable): bool
{
return $this->atLeastOneTypeIs($callable);
}

public function asNonNullable(): Type
{
$nonNullableTypes = [];
Expand Down

0 comments on commit 572209f

Please sign in to comment.