Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[TypeInfo] Fix isSatisfiedBy not traversing type tree #59844

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 24, 2025
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 10 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 26 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)) {
chalasr marked this conversation as resolved.
Show resolved Hide resolved
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
Morty Proxy This is a proxified and sanitized view of the page, visit original site.