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

Commit bc82b67

Browse filesBrowse files
committed
bug #59844 [TypeInfo] Fix isSatisfiedBy not traversing type tree (mtarld)
This PR was merged into the 7.2 branch. Discussion ---------- [TypeInfo] Fix `isSatisfiedBy` not traversing type tree | Q | A | ------------- | --- | Branch? | 7.2 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | | License | MIT Previously, `Type::isSatisfiedBy` was not traversing the type tree, which means that: ```php $specification = static fn (Type $type): bool => $type instanceof ObjectType; return Type::collection(Type::object(Foo::class))->isSatisfiedBy($specification); ``` was unexpectedly returning `false`. This PR fixes it. Commits ------- 8df764a [TypeInfo] Fix `isSatisfiedBy` not traversing type tree
2 parents 4471969 + 8df764a commit bc82b67
Copy full SHA for bc82b67

File tree

2 files changed

+26
-10
lines changed
Filter options

2 files changed

+26
-10
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/TypeInfo/Tests/TypeTest.php
+10
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

Copy file name to clipboardExpand all lines: src/Symfony/Component/TypeInfo/Type.php
+16-10
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.