Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Symfony/Component/TypeInfo/Tests/TypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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));
}
}
26 changes: 16 additions & 10 deletions src/Symfony/Component/TypeInfo/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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
Expand Down
Loading