Conditions | 50 |
Paths | > 20000 |
Total Lines | 194 |
Code Lines | 116 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
32 | public function getTypeString($schema, $path = '') |
||
33 | { |
||
34 | $schema = Schema::unboolSchema($schema); |
||
35 | |||
36 | $isOptional = false; |
||
37 | $isObject = false; |
||
38 | $isArray = false; |
||
39 | $isBoolean = false; |
||
40 | $isString = false; |
||
41 | $isNumber = false; |
||
42 | |||
43 | if ($schema->const !== null) { |
||
44 | return '(' . var_export($schema->const, true) . ')'; |
||
45 | } |
||
46 | |||
47 | if (!empty($schema->enum)) { |
||
48 | $res = ''; |
||
49 | foreach ($schema->enum as $value) { |
||
50 | $res .= var_export($value, true) . '|'; |
||
51 | } |
||
52 | return '(' . substr($res, 0, -1) . ')'; |
||
53 | } |
||
54 | |||
55 | if (!empty($schema->getFromRefs())) { |
||
56 | $refs = $schema->getFromRefs(); |
||
57 | $path = $refs[0]; |
||
58 | } |
||
59 | |||
60 | $type = $schema->type; |
||
61 | if ($type === null) { |
||
|
|||
62 | $type = []; |
||
63 | |||
64 | if (!empty($schema->properties) || !empty($schema->additionalProperties) || !empty($schema->patternProperties)) { |
||
65 | $type[] = Schema::OBJECT; |
||
66 | } |
||
67 | |||
68 | if (!empty($schema->items) || !empty($schema->additionalItems)) { |
||
69 | $type[] = Schema::_ARRAY; |
||
70 | } |
||
71 | } |
||
72 | |||
73 | if (!is_array($type)) { |
||
74 | $type = [$type]; |
||
75 | } |
||
76 | |||
77 | $or = []; |
||
78 | |||
79 | if ($schema->oneOf !== null) { |
||
80 | foreach ($schema->oneOf as $i => $item) { |
||
81 | $or[] = $this->getTypeString($item, $path . '/oneOf/' . $i); |
||
82 | } |
||
83 | } |
||
84 | |||
85 | if ($schema->anyOf !== null) { |
||
86 | foreach ($schema->anyOf as $i => $item) { |
||
87 | $or[] = $this->getTypeString($item, $path . '/anyOf/' . $i); |
||
88 | } |
||
89 | } |
||
90 | |||
91 | if ($schema->allOf !== null) { |
||
92 | foreach ($schema->allOf as $i => $item) { |
||
93 | $or[] = $this->getTypeString($item, $path . '/allOf/' . $i); |
||
94 | } |
||
95 | } |
||
96 | |||
97 | if ($schema->then !== null) { |
||
98 | $or[] = $this->getTypeString($schema->then, $path . '/then'); |
||
99 | } |
||
100 | |||
101 | if ($schema->else !== null) { |
||
102 | $or[] = $this->getTypeString($schema->else, $path . '/else'); |
||
103 | } |
||
104 | |||
105 | foreach ($type as $i => $t) { |
||
106 | switch ($t) { |
||
107 | case Schema::NULL: |
||
108 | $isOptional = true; |
||
109 | break; |
||
110 | |||
111 | case Schema::OBJECT: |
||
112 | $isObject = true; |
||
113 | break; |
||
114 | |||
115 | case Schema::_ARRAY: |
||
116 | $isArray = true; |
||
117 | break; |
||
118 | |||
119 | case Schema::NUMBER: |
||
120 | case Schema::INTEGER: |
||
121 | $isNumber = true; |
||
122 | break; |
||
123 | |||
124 | case Schema::STRING: |
||
125 | $isString = true; |
||
126 | break; |
||
127 | |||
128 | case Schema::BOOLEAN: |
||
129 | $isBoolean = true; |
||
130 | break; |
||
131 | |||
132 | } |
||
133 | } |
||
134 | |||
135 | if ($isObject) { |
||
136 | $typeAdded = false; |
||
137 | |||
138 | if (!empty($schema->properties)) { |
||
139 | if ($this->processed->contains($schema)) { |
||
140 | $or [] = $this->processed->offsetGet($schema); |
||
141 | $typeAdded = true; |
||
142 | } else { |
||
143 | if ($schema instanceof Schema) { |
||
144 | $typeName = $this->typeName($schema, $path); |
||
145 | $this->makeObjectTypeDef($schema, $path); |
||
146 | |||
147 | $or [] = $typeName; |
||
148 | $typeAdded = true; |
||
149 | } |
||
150 | } |
||
151 | |||
152 | } |
||
153 | |||
154 | if ($schema->additionalProperties instanceof Schema) { |
||
155 | $typeName = $this->getTypeString($schema->additionalProperties, $path . '/additionalProperties'); |
||
156 | $or [] = "Object.<String,$typeName>"; |
||
157 | $typeAdded = true; |
||
158 | } |
||
159 | |||
160 | if (!empty($schema->patternProperties)) { |
||
161 | foreach ($schema->patternProperties as $pattern => $propertySchema) { |
||
162 | if ($propertySchema instanceof Schema) { |
||
163 | $typeName = $this->getTypeString($propertySchema, $path . '/patternProperties/' . $pattern); |
||
164 | $or [] = $typeName; |
||
165 | $typeAdded = true; |
||
166 | } |
||
167 | } |
||
168 | } |
||
169 | |||
170 | if (!$typeAdded) { |
||
171 | $or [] = 'Object'; |
||
172 | } |
||
173 | } |
||
174 | |||
175 | if ($isArray) { |
||
176 | $typeAdded = false; |
||
177 | |||
178 | if ($schema->items instanceof Schema) { |
||
179 | $typeName = $this->getTypeString($schema->items, $path . '/items'); |
||
180 | $or [] = "Array<$typeName>"; |
||
181 | $typeAdded = true; |
||
182 | } |
||
183 | |||
184 | if ($schema->additionalItems instanceof Schema) { |
||
185 | $typeName = $this->getTypeString($schema->additionalItems, $path . '/additionalItems'); |
||
186 | $or [] = "Array<$typeName>"; |
||
187 | $typeAdded = true; |
||
188 | } |
||
189 | |||
190 | if (!$typeAdded) { |
||
191 | $or [] = 'Array'; |
||
192 | } |
||
193 | } |
||
194 | |||
195 | if ($isString) { |
||
196 | if ($schema->format === 'binary') { |
||
197 | $or[] = 'File'; |
||
198 | $or[] = 'Blob'; |
||
199 | } else { |
||
200 | $or[] = 'String'; |
||
201 | } |
||
202 | } |
||
203 | |||
204 | if ($isNumber) { |
||
205 | $or [] = 'Number'; |
||
206 | } |
||
207 | |||
208 | if ($isBoolean) { |
||
209 | $or [] = 'Boolean'; |
||
210 | } |
||
211 | |||
212 | $res = ''; |
||
213 | foreach ($or as $item) { |
||
214 | if (!empty($item) && $item !== '*') { |
||
215 | $res .= '|' . ($isOptional ? '?' : '') . $item; |
||
216 | } |
||
217 | } |
||
218 | |||
219 | if ($res !== '') { |
||
220 | $res = substr($res, 1); |
||
221 | } else { |
||
222 | $res = '*'; |
||
223 | } |
||
224 | |||
225 | return $res; |
||
226 | } |
||
305 | } |