Total Complexity | 67 |
Total Lines | 421 |
Duplicated Lines | 0 % |
Changes | 6 | ||
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 |
||
12 | abstract class Body |
||
13 | { |
||
14 | const SWAGGER_PROPERTIES="properties"; |
||
15 | const SWAGGER_ADDITIONAL_PROPERTIES="additionalProperties"; |
||
16 | const SWAGGER_REQUIRED="required"; |
||
17 | |||
18 | /** |
||
19 | * @var Schema |
||
20 | */ |
||
21 | protected Schema $schema; |
||
22 | |||
23 | /** |
||
24 | * @var array |
||
25 | */ |
||
26 | protected array $structure; |
||
27 | |||
28 | /** |
||
29 | * @var string |
||
30 | */ |
||
31 | protected string $name; |
||
32 | |||
33 | /** |
||
34 | * OpenApi 2.0 does not describe null values, so this flag defines, |
||
35 | * if match is ok when one of property, which has type, is null |
||
36 | * |
||
37 | * @var bool |
||
38 | */ |
||
39 | protected bool $allowNullValues; |
||
40 | |||
41 | /** |
||
42 | * Body constructor. |
||
43 | * |
||
44 | * @param Schema $schema |
||
45 | * @param string $name |
||
46 | * @param array $structure |
||
47 | * @param bool $allowNullValues |
||
48 | */ |
||
49 | public function __construct(Schema $schema, string $name, array $structure, bool $allowNullValues = false) |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * @param mixed $body |
||
59 | * @throws DefinitionNotFoundException |
||
60 | * @throws GenericSwaggerException |
||
61 | * @throws InvalidDefinitionException |
||
62 | * @throws InvalidRequestException |
||
63 | * @throws NotMatchedException |
||
64 | * @throws RequiredArgumentNotFound |
||
65 | * @return bool |
||
66 | */ |
||
67 | abstract public function match(mixed $body): bool; |
||
68 | |||
69 | /** |
||
70 | * @param string $name |
||
71 | * @param array $schemaArray |
||
72 | * @param mixed $body |
||
73 | * @param mixed $type |
||
74 | * @return ?bool |
||
75 | * @throws NotMatchedException |
||
76 | */ |
||
77 | protected function matchString(string $name, array $schemaArray, mixed $body, mixed $type): ?bool |
||
78 | { |
||
79 | if ($type !== 'string') { |
||
80 | return null; |
||
81 | } |
||
82 | |||
83 | if (isset($schemaArray['enum']) && !in_array($body, $schemaArray['enum'])) { |
||
84 | throw new NotMatchedException("Value '$body' in '$name' not matched in ENUM. ", $this->structure); |
||
85 | } |
||
86 | |||
87 | if (isset($schemaArray['pattern'])) { |
||
88 | $this->checkPattern($name, $body, $schemaArray['pattern']); |
||
89 | } |
||
90 | |||
91 | if (!is_string($body)) { |
||
92 | throw new NotMatchedException("Value '" . var_export($body, true) . "' in '$name' is not string. ", $this->structure); |
||
93 | } |
||
94 | |||
95 | return true; |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * @throws NotMatchedException |
||
100 | */ |
||
101 | private function checkPattern(string $name, mixed $body, string $pattern): void |
||
102 | { |
||
103 | $pattern = '/' . rtrim(ltrim($pattern, '/'), '/') . '/'; |
||
104 | $isSuccess = (bool) preg_match($pattern, $body); |
||
105 | |||
106 | if (!$isSuccess) { |
||
107 | throw new NotMatchedException("Value '$body' in '$name' not matched in pattern. ", $this->structure); |
||
108 | } |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * @param string $name |
||
113 | * @param array $schemaArray |
||
114 | * @param mixed $body |
||
115 | * @param mixed $type |
||
116 | * @return bool|null |
||
117 | */ |
||
118 | protected function matchFile(string $name, array $schemaArray, mixed $body, mixed $type): ?bool |
||
119 | { |
||
120 | if ($type !== 'file') { |
||
121 | return null; |
||
122 | } |
||
123 | |||
124 | return true; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * @param string $name |
||
129 | * @param array $schemaArray |
||
130 | * @param mixed $body |
||
131 | * @param mixed $type |
||
132 | * @return ?bool |
||
133 | * @throws NotMatchedException |
||
134 | */ |
||
135 | protected function matchNumber(string $name, array $schemaArray, mixed $body, mixed $type): ?bool |
||
136 | { |
||
137 | if ($type !== 'integer' && $type !== 'float' && $type !== 'number') { |
||
138 | return null; |
||
139 | } |
||
140 | |||
141 | if (!is_numeric($body)) { |
||
142 | throw new NotMatchedException("Expected '$name' to be numeric, but found '$body'. ", $this->structure); |
||
143 | } |
||
144 | |||
145 | if (isset($schemaArray['pattern'])) { |
||
146 | $this->checkPattern($name, $body, $schemaArray['pattern']); |
||
147 | } |
||
148 | |||
149 | return true; |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * @param string $name |
||
154 | * @param mixed $body |
||
155 | * @param mixed $type |
||
156 | * @return ?bool |
||
157 | * @throws NotMatchedException |
||
158 | */ |
||
159 | protected function matchBool(string $name, mixed $body, mixed $type): ?bool |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * @param string $name |
||
174 | * @param array $schemaArray |
||
175 | * @param mixed $body |
||
176 | * @param mixed $type |
||
177 | * @return ?bool |
||
178 | * @throws DefinitionNotFoundException |
||
179 | * @throws GenericSwaggerException |
||
180 | * @throws InvalidDefinitionException |
||
181 | * @throws InvalidRequestException |
||
182 | * @throws NotMatchedException |
||
183 | */ |
||
184 | protected function matchArray(string $name, array $schemaArray, mixed $body, mixed $type): ?bool |
||
185 | { |
||
186 | if ($type !== 'array') { |
||
187 | return null; |
||
188 | } |
||
189 | |||
190 | foreach ((array)$body as $item) { |
||
191 | if (!isset($schemaArray['items'])) { // If there is no type , there is no test. |
||
192 | continue; |
||
193 | } |
||
194 | $this->matchSchema($name, $schemaArray['items'], $item); |
||
195 | } |
||
196 | return true; |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * @param string $name |
||
201 | * @param mixed $schemaArray |
||
202 | * @param mixed $body |
||
203 | * @return ?bool |
||
204 | */ |
||
205 | protected function matchTypes(string $name, mixed $schemaArray, mixed $body): ?bool |
||
206 | { |
||
207 | if (!isset($schemaArray['type'])) { |
||
208 | return null; |
||
209 | } |
||
210 | |||
211 | $type = $schemaArray['type']; |
||
212 | $nullable = isset($schemaArray['nullable']) ? (bool)$schemaArray['nullable'] : $this->schema->isAllowNullValues(); |
||
213 | |||
214 | $validators = [ |
||
215 | function () use ($name, $body, $type, $nullable) |
||
216 | { |
||
217 | return $this->matchNull($name, $body, $type, $nullable); |
||
218 | }, |
||
219 | |||
220 | function () use ($name, $schemaArray, $body, $type) |
||
221 | { |
||
222 | return $this->matchString($name, $schemaArray, $body, $type); |
||
223 | }, |
||
224 | |||
225 | function () use ($name, $schemaArray, $body, $type) |
||
226 | { |
||
227 | return $this->matchNumber($name, $schemaArray, $body, $type); |
||
228 | }, |
||
229 | |||
230 | function () use ($name, $body, $type) |
||
231 | { |
||
232 | return $this->matchBool($name, $body, $type); |
||
233 | }, |
||
234 | |||
235 | function () use ($name, $schemaArray, $body, $type) |
||
236 | { |
||
237 | return $this->matchArray($name, $schemaArray, $body, $type); |
||
238 | }, |
||
239 | |||
240 | function () use ($name, $schemaArray, $body, $type) |
||
241 | { |
||
242 | return $this->matchFile($name, $schemaArray, $body, $type); |
||
243 | }, |
||
244 | ]; |
||
245 | |||
246 | foreach ($validators as $validator) { |
||
247 | $result = $validator(); |
||
248 | if (!is_null($result)) { |
||
249 | return $result; |
||
250 | } |
||
251 | } |
||
252 | |||
253 | return null; |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * @param string $name |
||
258 | * @param array $schemaArray |
||
259 | * @param mixed $body |
||
260 | * @return bool|null |
||
261 | * @throws DefinitionNotFoundException |
||
262 | * @throws GenericSwaggerException |
||
263 | * @throws InvalidDefinitionException |
||
264 | * @throws InvalidRequestException |
||
265 | * @throws NotMatchedException |
||
266 | */ |
||
267 | public function matchObjectProperties(string $name, mixed $schemaArray, mixed $body): ?bool |
||
329 | } |
||
330 | |||
331 | /** |
||
332 | * @param string $name |
||
333 | * @param array $schemaArray |
||
334 | * @param array $body |
||
335 | * @return ?bool |
||
336 | * @throws DefinitionNotFoundException |
||
337 | * @throws InvalidDefinitionException |
||
338 | * @throws GenericSwaggerException |
||
339 | * @throws InvalidRequestException |
||
340 | * @throws NotMatchedException |
||
341 | */ |
||
342 | protected function matchSchema(string $name, mixed $schemaArray, mixed $body): ?bool |
||
409 | } |
||
410 | |||
411 | /** |
||
412 | * @param string $name |
||
413 | * @param mixed $body |
||
414 | * @param mixed $type |
||
415 | * @param bool $nullable |
||
416 | * @return ?bool |
||
417 | * @throws NotMatchedException |
||
418 | */ |
||
419 | protected function matchNull(string $name, mixed $body, mixed $type, bool $nullable): ?bool |
||
433 | } |
||
434 | } |
||
435 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.