diff --git a/src/Symfony/Component/TypeInfo/Tests/TypeTest.php b/src/Symfony/Component/TypeInfo/Tests/TypeTest.php index 6d60b5dc21eca..8b2bc4d900c48 100644 --- a/src/Symfony/Component/TypeInfo/Tests/TypeTest.php +++ b/src/Symfony/Component/TypeInfo/Tests/TypeTest.php @@ -13,6 +13,8 @@ use PHPUnit\Framework\TestCase; use Symfony\Component\TypeInfo\Type; +use Symfony\Component\TypeInfo\Type\CollectionType; +use Symfony\Component\TypeInfo\Type\UnionType; use Symfony\Component\TypeInfo\TypeIdentifier; class TypeTest extends TestCase @@ -34,4 +36,12 @@ public function testIsNullable() $this->assertFalse(Type::int()->isNullable()); } + + public function testIsSatisfiedBy() + { + $this->assertTrue(Type::union(Type::int(), Type::string())->isSatisfiedBy(fn (Type $t): bool => 'int' === (string) $t)); + $this->assertTrue(Type::union(Type::int(), Type::string())->isSatisfiedBy(fn (Type $t): bool => $t instanceof UnionType)); + $this->assertTrue(Type::list(Type::int())->isSatisfiedBy(fn (Type $t): bool => $t instanceof CollectionType && 'int' === (string) $t->getCollectionValueType())); + $this->assertFalse(Type::list(Type::int())->isSatisfiedBy(fn (Type $t): bool => 'int' === (string) $t)); + } } diff --git a/src/Symfony/Component/TypeInfo/Type.php b/src/Symfony/Component/TypeInfo/Type.php index 2a39f14e7b5bf..c14b86263a784 100644 --- a/src/Symfony/Component/TypeInfo/Type.php +++ b/src/Symfony/Component/TypeInfo/Type.php @@ -29,6 +29,14 @@ abstract class Type implements \Stringable */ public function isSatisfiedBy(callable $specification): bool { + if ($this instanceof WrappingTypeInterface && $this->wrappedTypeIsSatisfiedBy($specification)) { + return true; + } + + if ($this instanceof CompositeTypeInterface && $this->composedTypesAreSatisfiedBy($specification)) { + return true; + } + return $specification($this); } @@ -37,19 +45,17 @@ public function isSatisfiedBy(callable $specification): bool */ public function isIdentifiedBy(TypeIdentifier|string ...$identifiers): bool { - $specification = static function (Type $type) use (&$specification, $identifiers): bool { - if ($type instanceof WrappingTypeInterface) { - return $type->wrappedTypeIsSatisfiedBy($specification); - } + $specification = static fn (Type $type): bool => $type->isIdentifiedBy(...$identifiers); - if ($type instanceof CompositeTypeInterface) { - return $type->composedTypesAreSatisfiedBy($specification); - } + if ($this instanceof WrappingTypeInterface && $this->wrappedTypeIsSatisfiedBy($specification)) { + return true; + } - return $type->isIdentifiedBy(...$identifiers); - }; + if ($this instanceof CompositeTypeInterface && $this->composedTypesAreSatisfiedBy($specification)) { + return true; + } - return $this->isSatisfiedBy($specification); + return false; } public function isNullable(): bool