Skip to content

Commit e46905c

Browse files
committed
[TypeInfo] Fix isSatisfiedBy not traversing type tree
1 parent 0f6fd37 commit e46905c

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 testIsSatifiedBy()
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy