12
12
use TypeLang \Parser \Node \Stmt \Template \TemplateArgumentNode ;
13
13
use TypeLang \Parser \Node \Stmt \Template \TemplateArgumentsListNode ;
14
14
use TypeLang \Parser \Node \Stmt \TypeStatement ;
15
- use TypeLang \Parser \Node \Stmt \UnionTypeNode ;
16
15
17
16
class ArrayType implements TypeInterface
18
17
{
@@ -33,33 +32,20 @@ public function __construct(
33
32
#[\Override]
34
33
public function getTypeStatement (LocalContext $ context ): TypeStatement
35
34
{
36
- if (!$ context ->isDetailedTypes ()) {
37
- return new NamedTypeNode ($ this ->name );
38
- }
35
+ $ child = $ context ->withDetailedTypes (false );
39
36
40
37
return new NamedTypeNode (
41
38
name: $ this ->name ,
42
39
arguments: new TemplateArgumentsListNode ([
43
- new TemplateArgumentNode ($ this ->key ->getTypeStatement ($ context )),
44
- new TemplateArgumentNode ($ this ->value ->getTypeStatement ($ context )),
40
+ new TemplateArgumentNode ($ this ->key ->getTypeStatement ($ child )),
41
+ new TemplateArgumentNode ($ this ->value ->getTypeStatement ($ child )),
45
42
]),
46
43
);
47
44
}
48
45
49
- /**
50
- * @return UnionTypeNode<TypeStatement>
51
- */
52
- protected function getSupportedKeyType (): UnionTypeNode
53
- {
54
- return new UnionTypeNode (
55
- new NamedTypeNode ('string ' ),
56
- new NamedTypeNode ('int ' ),
57
- );
58
- }
59
-
60
46
public function match (mixed $ value , LocalContext $ context ): bool
61
47
{
62
- if (!$ this -> matchRootType ($ value, $ context )) {
48
+ if (!\is_array ($ value )) {
63
49
return false ;
64
50
}
65
51
@@ -79,26 +65,14 @@ public function match(mixed $value, LocalContext $context): bool
79
65
return true ;
80
66
}
81
67
82
- /**
83
- * @return ($value is iterable ? true : false)
84
- */
85
- private function matchRootType (mixed $ value , LocalContext $ context ): bool
86
- {
87
- if (!$ context ->isStrictTypesEnabled ()) {
88
- return \is_iterable ($ value );
89
- }
90
-
91
- return \is_array ($ value );
92
- }
93
-
94
68
/**
95
69
* @return array<array-key, mixed>
96
70
* @throws InvalidValueException
97
71
* @throws TypeNotFoundException
98
72
*/
99
73
public function cast (mixed $ value , LocalContext $ context ): array
100
74
{
101
- if (!$ this -> matchRootType ($ value, $ context )) {
75
+ if (!\is_array ($ value )) {
102
76
throw InvalidValueException::becauseInvalidValueGiven (
103
77
value: $ value ,
104
78
expected: $ this ->getTypeStatement ($ context ),
@@ -109,14 +83,6 @@ public function cast(mixed $value, LocalContext $context): array
109
83
$ result = [];
110
84
111
85
foreach ($ value as $ index => $ item ) {
112
- if (!\is_string ($ index ) && !\is_int ($ index )) {
113
- throw InvalidValueException::becauseInvalidValueGiven (
114
- value: $ index ,
115
- expected: $ this ->getSupportedKeyType (),
116
- context: $ context ,
117
- );
118
- }
119
-
120
86
$ context ->enter (new ArrayIndexEntry ($ index ));
121
87
122
88
$ result [$ this ->key ->cast ($ index , $ context )]
0 commit comments