Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8df764a

Browse files
committed
[TypeInfo] Fix isSatisfiedBy not traversing type tree
1 parent cc8ea7f commit 8df764a

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

src/Symfony/Component/TypeInfo/Tests/TypeTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\TypeInfo\Type;
16+
use Symfony\Component\TypeInfo\Type\CollectionType;
17+
use Symfony\Component\TypeInfo\Type\UnionType;
1618
use Symfony\Component\TypeInfo\TypeIdentifier;
1719

1820
class TypeTest extends TestCase
@@ -34,4 +36,12 @@ public function testIsNullable()
3436

3537
$this->assertFalse(Type::int()->isNullable());
3638
}
39+
40+
public function testIsSatisfiedBy()
41+
{
42+
$this->assertTrue(Type::union(Type::int(), Type::string())->isSatisfiedBy(fn (Type $t): bool => 'int' === (string) $t));
43+
$this->assertTrue(Type::union(Type::int(), Type::string())->isSatisfiedBy(fn (Type $t): bool => $t instanceof UnionType));
44+
$this->assertTrue(Type::list(Type::int())->isSatisfiedBy(fn (Type $t): bool => $t instanceof CollectionType && 'int' === (string) $t->getCollectionValueType()));
45+
$this->assertFalse(Type::list(Type::int())->isSatisfiedBy(fn (Type $t): bool => 'int' === (string) $t));
46+
}
3747
}

src/Symfony/Component/TypeInfo/Type.php

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ abstract class Type implements \Stringable
2929
*/
3030
public function isSatisfiedBy(callable $specification): bool
3131
{
32+
if ($this instanceof WrappingTypeInterface && $this->wrappedTypeIsSatisfiedBy($specification)) {
33+
return true;
34+
}
35+
36+
if ($this instanceof CompositeTypeInterface && $this->composedTypesAreSatisfiedBy($specification)) {
37+
return true;
38+
}
39+
3240
return $specification($this);
3341
}
3442

@@ -37,19 +45,17 @@ public function isSatisfiedBy(callable $specification): bool
3745
*/
3846
public function isIdentifiedBy(TypeIdentifier|string ...$identifiers): bool
3947
{
40-
$specification = static function (Type $type) use (&$specification, $identifiers): bool {
41-
if ($type instanceof WrappingTypeInterface) {
42-
return $type->wrappedTypeIsSatisfiedBy($specification);
43-
}
48+
$specification = static fn (Type $type): bool => $type->isIdentifiedBy(...$identifiers);
4449

45-
if ($type instanceof CompositeTypeInterface) {
46-
return $type->composedTypesAreSatisfiedBy($specification);
47-
}
50+
if ($this instanceof WrappingTypeInterface && $this->wrappedTypeIsSatisfiedBy($specification)) {
51+
return true;
52+
}
4853

49-
return $type->isIdentifiedBy(...$identifiers);
50-
};
54+
if ($this instanceof CompositeTypeInterface && $this->composedTypesAreSatisfiedBy($specification)) {
55+
return true;
56+
}
5157

52-
return $this->isSatisfiedBy($specification);
58+
return false;
5359
}
5460

5561
public function isNullable(): bool

0 commit comments

Comments
 (0)