Total Complexity | 70 |
Total Lines | 431 |
Duplicated Lines | 0 % |
Changes | 5 | ||
Bugs | 0 | Features | 0 |
Complex classes like Body often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Body, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | abstract class Body |
||
17 | { |
||
18 | const SWAGGER_PROPERTIES="properties"; |
||
19 | const SWAGGER_ADDITIONAL_PROPERTIES="additionalProperties"; |
||
20 | const SWAGGER_REQUIRED="required"; |
||
21 | |||
22 | /** |
||
23 | * @var Schema |
||
24 | */ |
||
25 | protected $schema; |
||
26 | |||
27 | /** |
||
28 | * @var array |
||
29 | */ |
||
30 | protected $structure; |
||
31 | |||
32 | /** |
||
33 | * @var string |
||
34 | */ |
||
35 | protected $name; |
||
36 | |||
37 | /** |
||
38 | * OpenApi 2.0 does not describe null values, so this flag defines, |
||
39 | * if match is ok when one of property, which has type, is null |
||
40 | * |
||
41 | * @var bool |
||
42 | */ |
||
43 | protected $allowNullValues; |
||
44 | |||
45 | /** |
||
46 | * Body constructor. |
||
47 | * |
||
48 | * @param Schema $schema |
||
49 | * @param string $name |
||
50 | * @param array $structure |
||
51 | * @param bool $allowNullValues |
||
52 | */ |
||
53 | public function __construct(Schema $schema, $name, $structure, $allowNullValues = false) |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * @param Schema $schema |
||
66 | * @param string $name |
||
67 | * @param array $structure |
||
68 | * @param bool $allowNullValues |
||
69 | * @return OpenApiResponseBody|SwaggerResponseBody |
||
70 | * @throws GenericSwaggerException |
||
71 | */ |
||
72 | public static function getInstance(Schema $schema, $name, $structure, $allowNullValues = false) |
||
73 | { |
||
74 | if ($schema instanceof SwaggerSchema) { |
||
75 | return new SwaggerResponseBody($schema, $name, $structure, $allowNullValues); |
||
76 | } |
||
77 | |||
78 | if ($schema instanceof OpenApiSchema) { |
||
79 | return new OpenApiResponseBody($schema, $name, $structure, $allowNullValues); |
||
80 | } |
||
81 | |||
82 | throw new GenericSwaggerException("Cannot get instance SwaggerBody or SchemaBody from " . get_class($schema)); |
||
83 | } |
||
84 | |||
85 | abstract public function match($body); |
||
86 | |||
87 | /** |
||
88 | * @param string $name |
||
89 | * @param array $schemaArray |
||
90 | * @param string $body |
||
91 | * @param string $type |
||
92 | * @return bool |
||
93 | * @throws NotMatchedException |
||
94 | */ |
||
95 | protected function matchString($name, $schemaArray, $body, $type) |
||
110 | } |
||
111 | |||
112 | private function checkPattern($name, $body, $pattern) |
||
119 | } |
||
120 | } |
||
121 | |||
122 | private function preparePattern($pattern) |
||
123 | { |
||
124 | if ($pattern[0] !== '/' && substr($pattern, -1) !== '/') { |
||
125 | $pattern = '/' . $pattern . '/'; |
||
126 | } |
||
127 | return $pattern; |
||
128 | } |
||
129 | |||
130 | |||
131 | /** |
||
132 | * @param string $name |
||
133 | * @param array $schemaArray |
||
134 | * @param string $body |
||
135 | * @param string $type |
||
136 | * @return bool |
||
137 | */ |
||
138 | protected function matchFile($name, $schemaArray, $body, $type) |
||
139 | { |
||
140 | if ($type !== 'file') { |
||
141 | return null; |
||
142 | } |
||
143 | |||
144 | return true; |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * @param string $name |
||
149 | * @param string $body |
||
150 | * @param string $type |
||
151 | * @return bool |
||
152 | * @throws NotMatchedException |
||
153 | */ |
||
154 | protected function matchNumber($name, $body, $type) |
||
155 | { |
||
156 | if ($type !== 'integer' && $type !== 'float' && $type !== 'number') { |
||
157 | return null; |
||
158 | } |
||
159 | |||
160 | if (!is_numeric($body)) { |
||
161 | throw new NotMatchedException("Expected '$name' to be numeric, but found '$body'. ", $this->structure); |
||
162 | } |
||
163 | |||
164 | if (isset($schemaArray['pattern'])) { |
||
165 | $this->checkPattern($name, $body, $schemaArray['pattern']); |
||
166 | } |
||
167 | |||
168 | return true; |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * @param string $name |
||
173 | * @param string $body |
||
174 | * @param string $type |
||
175 | * @return bool |
||
176 | * @throws NotMatchedException |
||
177 | */ |
||
178 | protected function matchBool($name, $body, $type) |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * @param string $name |
||
193 | * @param array $schemaArray |
||
194 | * @param string $body |
||
195 | * @param string $type |
||
196 | * @return bool |
||
197 | * @throws DefinitionNotFoundException |
||
198 | * @throws GenericSwaggerException |
||
199 | * @throws InvalidDefinitionException |
||
200 | * @throws InvalidRequestException |
||
201 | * @throws NotMatchedException |
||
202 | */ |
||
203 | protected function matchArray($name, $schemaArray, $body, $type) |
||
204 | { |
||
205 | if ($type !== 'array') { |
||
206 | return null; |
||
207 | } |
||
208 | |||
209 | foreach ((array)$body as $item) { |
||
210 | if (!isset($schemaArray['items'])) { // If there is no type , there is no test. |
||
211 | continue; |
||
212 | } |
||
213 | $this->matchSchema($name, $schemaArray['items'], $item); |
||
214 | } |
||
215 | return true; |
||
216 | } |
||
217 | |||
218 | /** |
||
219 | * @param string $name |
||
220 | * @param array $schemaArray |
||
221 | * @param string $body |
||
222 | * @return mixed|null |
||
223 | */ |
||
224 | protected function matchTypes($name, $schemaArray, $body) |
||
225 | { |
||
226 | if (!isset($schemaArray['type'])) { |
||
227 | return null; |
||
228 | } |
||
229 | |||
230 | $type = $schemaArray['type']; |
||
231 | $nullable = isset($schemaArray['nullable']) ? (bool)$schemaArray['nullable'] : $this->schema->isAllowNullValues(); |
||
232 | |||
233 | $validators = [ |
||
234 | function () use ($name, $body, $type, $nullable) |
||
235 | { |
||
236 | return $this->matchNull($name, $body, $type, $nullable); |
||
237 | }, |
||
238 | |||
239 | function () use ($name, $schemaArray, $body, $type) |
||
240 | { |
||
241 | return $this->matchString($name, $schemaArray, $body, $type); |
||
242 | }, |
||
243 | |||
244 | function () use ($name, $body, $type) |
||
245 | { |
||
246 | return $this->matchNumber($name, $body, $type); |
||
247 | }, |
||
248 | |||
249 | function () use ($name, $body, $type) |
||
250 | { |
||
251 | return $this->matchBool($name, $body, $type); |
||
252 | }, |
||
253 | |||
254 | function () use ($name, $schemaArray, $body, $type) |
||
255 | { |
||
256 | return $this->matchArray($name, $schemaArray, $body, $type); |
||
257 | }, |
||
258 | |||
259 | function () use ($name, $schemaArray, $body, $type) |
||
260 | { |
||
261 | return $this->matchFile($name, $schemaArray, $body, $type); |
||
262 | }, |
||
263 | ]; |
||
264 | |||
265 | foreach ($validators as $validator) { |
||
266 | $result = $validator(); |
||
267 | if (!is_null($result)) { |
||
268 | return $result; |
||
269 | } |
||
270 | } |
||
271 | |||
272 | return null; |
||
273 | } |
||
274 | |||
275 | /** |
||
276 | * @param string $name |
||
277 | * @param array $schemaArray |
||
278 | * @param string $body |
||
279 | * @return bool|null |
||
280 | * @throws DefinitionNotFoundException |
||
281 | * @throws GenericSwaggerException |
||
282 | * @throws InvalidDefinitionException |
||
283 | * @throws InvalidRequestException |
||
284 | * @throws NotMatchedException |
||
285 | */ |
||
286 | public function matchObjectProperties($name, $schemaArray, $body) |
||
348 | } |
||
349 | |||
350 | /** |
||
351 | * @param string $name |
||
352 | * @param array $schemaArray |
||
353 | * @param array $body |
||
354 | * @return bool |
||
355 | * @throws DefinitionNotFoundException |
||
356 | * @throws InvalidDefinitionException |
||
357 | * @throws GenericSwaggerException |
||
358 | * @throws InvalidRequestException |
||
359 | * @throws NotMatchedException |
||
360 | */ |
||
361 | protected function matchSchema($name, $schemaArray, $body) |
||
423 | } |
||
424 | |||
425 | /** |
||
426 | * @param string $name |
||
427 | * @param string $body |
||
428 | * @param string $type |
||
429 | * @param bool $nullable |
||
430 | * @return bool |
||
431 | * @throws NotMatchedException |
||
432 | */ |
||
433 | protected function matchNull($name, $body, $type, $nullable) |
||
447 | } |
||
448 | } |
||
449 |