Conditions | 91 |
Paths | > 20000 |
Total Lines | 296 |
Code Lines | 176 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
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 |
||
110 | private function process($data, $import = true, $path = '#') |
||
111 | { |
||
112 | if (!$import && $data instanceof ObjectItem) { |
||
113 | $data = $data->jsonSerialize(); |
||
114 | } |
||
115 | $result = $data; |
||
116 | if ($this->ref !== null) { |
||
117 | // https://github.com/json-schema-org/JSON-Schema-Test-Suite/pull/129 |
||
118 | return $this->ref->getSchema()->process($data, $import, $path . '->' . $this->ref->ref); |
||
119 | } |
||
120 | |||
121 | if ($this->type !== null) { |
||
122 | if (!$this->type->isValid($data)) { |
||
123 | $this->fail(new TypeException(ucfirst(implode(', ', $this->type->types) . ' expected, ' . json_encode($data) . ' received')), $path); |
||
124 | } |
||
125 | } |
||
126 | |||
127 | if ($this->enum !== null) { |
||
128 | $enumOk = false; |
||
129 | foreach ($this->enum as $item) { |
||
130 | if ($item === $data) { // todo support complex structures here |
||
131 | $enumOk = true; |
||
132 | break; |
||
133 | } |
||
134 | } |
||
135 | if (!$enumOk) { |
||
136 | $this->fail(new EnumException('Enum failed'), $path); |
||
137 | } |
||
138 | } |
||
139 | |||
140 | if ($this->not !== null) { |
||
141 | $exception = false; |
||
142 | try { |
||
143 | $this->not->process($data, $import, $path . '->not'); |
||
144 | } catch (InvalidValue $exception) { |
||
145 | // Expected exception |
||
146 | } |
||
147 | if ($exception === false) { |
||
148 | $this->fail(new LogicException('Failed due to logical constraint: not'), $path); |
||
149 | } |
||
150 | } |
||
151 | |||
152 | if ($this->oneOf !== null) { |
||
153 | $successes = 0; |
||
154 | foreach ($this->oneOf as $index => $item) { |
||
155 | try { |
||
156 | $result = $item->process($data, $import, $path . '->oneOf:' . $index); |
||
157 | $successes++; |
||
158 | if ($successes > 1) { |
||
159 | break; |
||
160 | } |
||
161 | } catch (InvalidValue $exception) { |
||
162 | // Expected exception |
||
163 | } |
||
164 | } |
||
165 | if ($successes !== 1) { |
||
166 | $this->fail(new LogicException('Failed due to logical constraint: oneOf'), $path); |
||
167 | } |
||
168 | } |
||
169 | |||
170 | if ($this->anyOf !== null) { |
||
171 | $successes = 0; |
||
172 | foreach ($this->anyOf as $index => $item) { |
||
173 | try { |
||
174 | $result = $item->process($data, $import, $path . '->anyOf:' . $index); |
||
175 | $successes++; |
||
176 | if ($successes) { |
||
177 | break; |
||
178 | } |
||
179 | } catch (InvalidValue $exception) { |
||
180 | // Expected exception |
||
181 | } |
||
182 | } |
||
183 | if (!$successes) { |
||
184 | $this->fail(new LogicException('Failed due to logical constraint: anyOf'), $path); |
||
185 | } |
||
186 | } |
||
187 | |||
188 | if ($this->allOf !== null) { |
||
189 | foreach ($this->allOf as $index => $item) { |
||
190 | $result = $item->process($data, $import, $path . '->allOf' . $index); |
||
191 | } |
||
192 | } |
||
193 | |||
194 | |||
195 | if (is_string($data)) { |
||
196 | if ($this->minLength !== null) { |
||
197 | if (mb_strlen($data, 'UTF-8') < $this->minLength) { |
||
198 | $this->fail(new StringException('String is too short', StringException::TOO_SHORT), $path); |
||
199 | } |
||
200 | } |
||
201 | if ($this->maxLength !== null) { |
||
202 | if (mb_strlen($data, 'UTF-8') > $this->maxLength) { |
||
203 | $this->fail(new StringException('String is too long', StringException::TOO_LONG), $path); |
||
204 | } |
||
205 | } |
||
206 | if ($this->pattern !== null) { |
||
207 | if (0 === preg_match($this->pattern, $data)) { |
||
208 | $this->fail(new StringException('Does not match to ' |
||
209 | . $this->pattern, StringException::PATTERN_MISMATCH), $path); |
||
210 | } |
||
211 | } |
||
212 | } |
||
213 | |||
214 | if (is_int($data) || is_float($data)) { |
||
215 | if ($this->multipleOf !== null) { |
||
216 | $div = $data / $this->multipleOf; |
||
217 | if ($div != (int)$div) { |
||
218 | $this->fail(new NumericException($data . ' is not multiple of ' . $this->multipleOf, NumericException::MULTIPLE_OF), $path); |
||
219 | } |
||
220 | } |
||
221 | |||
222 | if ($this->maximum !== null) { |
||
223 | if ($this->exclusiveMaximum === true) { |
||
224 | if ($data >= $this->maximum) { |
||
225 | $this->fail(new NumericException( |
||
226 | 'Value less or equal than ' . $this->minimum . ' expected, ' . $data . ' received', |
||
227 | NumericException::MAXIMUM), $path); |
||
228 | } |
||
229 | } else { |
||
230 | if ($data > $this->maximum) { |
||
231 | $this->fail(new NumericException( |
||
232 | 'Value less than ' . $this->minimum . ' expected, ' . $data . ' received', |
||
233 | NumericException::MAXIMUM), $path); |
||
234 | } |
||
235 | } |
||
236 | } |
||
237 | |||
238 | if ($this->minimum !== null) { |
||
239 | if ($this->exclusiveMinimum === true) { |
||
240 | if ($data <= $this->minimum) { |
||
241 | $this->fail(new NumericException( |
||
242 | 'Value more or equal than ' . $this->minimum . ' expected, ' . $data . ' received', |
||
243 | NumericException::MINIMUM), $path); |
||
244 | } |
||
245 | } else { |
||
246 | if ($data < $this->minimum) { |
||
247 | $this->fail(new NumericException( |
||
248 | 'Value more than ' . $this->minimum . ' expected, ' . $data . ' received', |
||
249 | NumericException::MINIMUM), $path); |
||
250 | } |
||
251 | } |
||
252 | } |
||
253 | |||
254 | |||
255 | } |
||
256 | |||
257 | if ($data instanceof \stdClass) { |
||
258 | if ($this->required !== null) { |
||
259 | foreach ($this->required as $item) { |
||
260 | if (!property_exists($data, $item)) { |
||
261 | $this->fail(new ObjectException('Required property missing: ' . $item, ObjectException::REQUIRED), $path); |
||
262 | } |
||
263 | } |
||
264 | } |
||
265 | |||
266 | if ($import && !$result instanceof ObjectItem) { |
||
267 | $result = $this->makeObjectItem(); |
||
268 | |||
269 | if ($result instanceof ClassStructure) { |
||
270 | if ($result->__validateOnSet) { |
||
|
|||
271 | $result->__validateOnSet = false; |
||
272 | /** @noinspection PhpUnusedLocalVariableInspection */ |
||
273 | $validateOnSetHandler = new ScopeExit(function () use ($result) { |
||
1 ignored issue
–
show
|
|||
274 | $result->__validateOnSet = true; |
||
275 | }); |
||
276 | } |
||
277 | } |
||
278 | } |
||
279 | |||
280 | if ($this->properties !== null) { |
||
281 | /** @var Schema[] $properties */ |
||
282 | $properties = &$this->properties->toArray(); // TODO check performance of pointer |
||
283 | $nestedProperties = $this->properties->getNestedProperties(); |
||
284 | } |
||
285 | |||
286 | $array = (array)$data; |
||
287 | if ($this->minProperties !== null && count($array) < $this->minProperties) { |
||
288 | $this->fail(new ObjectException("Not enough properties", ObjectException::TOO_FEW), $path); |
||
289 | } |
||
290 | if ($this->maxProperties !== null && count($array) > $this->maxProperties) { |
||
291 | $this->fail(new ObjectException("Too many properties", ObjectException::TOO_MANY), $path); |
||
292 | } |
||
293 | foreach ($array as $key => $value) { |
||
294 | $found = false; |
||
295 | if (isset($this->dependencies[$key])) { |
||
296 | $dependencies = $this->dependencies[$key]; |
||
297 | if ($dependencies instanceof Schema) { |
||
298 | $dependencies->process($data, $import, $path . '->dependencies:' . $key); |
||
299 | } else { |
||
300 | foreach ($dependencies as $item) { |
||
301 | if (!property_exists($data, $item)) { |
||
302 | $this->fail(new ObjectException('Dependency property missing: ' . $item, |
||
303 | ObjectException::DEPENDENCY_MISSING), $path); |
||
304 | } |
||
305 | } |
||
306 | } |
||
307 | } |
||
308 | |||
309 | if (isset($properties[$key])) { |
||
310 | $found = true; |
||
311 | $value = $properties[$key]->process($value, $import, $path . '->properties:' . $key); |
||
312 | } |
||
313 | |||
314 | /** @var Egg $nestedEgg */ |
||
315 | $nestedEgg = null; |
||
316 | if (!$found && isset($nestedProperties[$key])) { |
||
317 | $found = true; |
||
318 | $nestedEgg = $nestedProperties[$key]; |
||
319 | $value = $nestedEgg->propertySchema->process($value, $import, $path . '->nestedProperties:' . $key); |
||
320 | } |
||
321 | |||
322 | if ($this->patternProperties !== null) { |
||
323 | foreach ($this->patternProperties as $pattern => $propertySchema) { |
||
324 | if (preg_match($pattern, $key)) { |
||
325 | $found = true; |
||
326 | $value = $propertySchema->process($value, $import, $path . '->patternProperties:' . $pattern); |
||
327 | if ($import) { |
||
328 | $result->addPatternPropertyName($pattern, $key); |
||
329 | } |
||
330 | //break; // todo manage multiple import data properly (pattern accessor) |
||
331 | } |
||
332 | } |
||
333 | } |
||
334 | if (!$found && $this->additionalProperties !== null) { |
||
335 | if ($this->additionalProperties === false) { |
||
336 | $this->fail(new ObjectException('Additional properties not allowed'), $path); |
||
337 | } |
||
338 | |||
339 | $value = $this->additionalProperties->process($value, $import, $path . '->additionalProperties'); |
||
340 | if ($import) { |
||
341 | $result->addAdditionalPropertyName($key); |
||
342 | } |
||
343 | } |
||
344 | |||
345 | if ($nestedEgg && $import) { |
||
346 | $result->setNestedProperty($key, $value, $nestedEgg); |
||
347 | } else { |
||
348 | $result->$key = $value; |
||
349 | } |
||
350 | |||
351 | } |
||
352 | |||
353 | } |
||
354 | |||
355 | if (is_array($data)) { |
||
356 | |||
357 | if ($this->minItems !== null && count($data) < $this->minItems) { |
||
358 | $this->fail(new ArrayException("Not enough items in array"), $path); |
||
359 | } |
||
360 | |||
361 | if ($this->maxItems !== null && count($data) > $this->maxItems) { |
||
362 | $this->fail(new ArrayException("Too many items in array"), $path); |
||
363 | } |
||
364 | |||
365 | $pathItems = 'items'; |
||
366 | if ($this->items instanceof Schema) { |
||
367 | $items = array(); |
||
368 | $additionalItems = $this->items; |
||
369 | } elseif ($this->items === null) { // items defaults to empty schema so everything is valid |
||
370 | $items = array(); |
||
371 | $additionalItems = true; |
||
372 | } else { // listed items |
||
373 | $items = $this->items; |
||
374 | $additionalItems = $this->additionalItems; |
||
375 | $pathItems = 'additionalItems'; |
||
376 | } |
||
377 | |||
378 | if ($items !== null || $additionalItems !== null) { |
||
379 | $itemsLen = is_array($items) ? count($items) : 0; |
||
380 | $index = 0; |
||
381 | foreach ($data as &$value) { |
||
382 | if ($index < $itemsLen) { |
||
383 | $value = $items[$index]->process($value, $import, $path . '->items:' . $index); |
||
384 | } else { |
||
385 | if ($additionalItems instanceof Schema) { |
||
386 | $value = $additionalItems->process($value, $import, $path . '->' . $pathItems |
||
387 | . '[' . $index . ']'); |
||
388 | } elseif ($additionalItems === false) { |
||
389 | $this->fail(new ArrayException('Unexpected array item'), $path); |
||
390 | } |
||
391 | } |
||
392 | ++$index; |
||
393 | } |
||
394 | } |
||
395 | |||
396 | if ($this->uniqueItems) { |
||
397 | if (!UniqueItems::isValid($data)) { |
||
398 | $this->fail(new ArrayException('Array is not unique'), $path); |
||
399 | } |
||
400 | } |
||
401 | } |
||
402 | |||
403 | |||
404 | return $result; |
||
405 | } |
||
406 | |||
505 |
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.