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