File tree 2 files changed +26
-10
lines changed
Filter options
src/Symfony/Component/TypeInfo
2 files changed +26
-10
lines changed
Original file line number Diff line number Diff line change 13
13
14
14
use PHPUnit \Framework \TestCase ;
15
15
use Symfony \Component \TypeInfo \Type ;
16
+ use Symfony \Component \TypeInfo \Type \CollectionType ;
17
+ use Symfony \Component \TypeInfo \Type \UnionType ;
16
18
use Symfony \Component \TypeInfo \TypeIdentifier ;
17
19
18
20
class TypeTest extends TestCase
@@ -34,4 +36,12 @@ public function testIsNullable()
34
36
35
37
$ this ->assertFalse (Type::int ()->isNullable ());
36
38
}
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
+ }
37
47
}
Original file line number Diff line number Diff line change @@ -29,6 +29,14 @@ abstract class Type implements \Stringable
29
29
*/
30
30
public function isSatisfiedBy (callable $ specification ): bool
31
31
{
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
+
32
40
return $ specification ($ this );
33
41
}
34
42
@@ -37,19 +45,17 @@ public function isSatisfiedBy(callable $specification): bool
37
45
*/
38
46
public function isIdentifiedBy (TypeIdentifier |string ...$ identifiers ): bool
39
47
{
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 );
44
49
45
- if ($ type instanceof CompositeTypeInterface ) {
46
- return $ type -> composedTypesAreSatisfiedBy ( $ specification ) ;
47
- }
50
+ if ($ this instanceof WrappingTypeInterface && $ this -> wrappedTypeIsSatisfiedBy ( $ specification ) ) {
51
+ return true ;
52
+ }
48
53
49
- return $ type ->isIdentifiedBy (...$ identifiers );
50
- };
54
+ if ($ this instanceof CompositeTypeInterface && $ this ->composedTypesAreSatisfiedBy ($ specification )) {
55
+ return true ;
56
+ }
51
57
52
- return $ this -> isSatisfiedBy ( $ specification ) ;
58
+ return false ;
53
59
}
54
60
55
61
public function isNullable (): bool
You can’t perform that action at this time.
0 commit comments