These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace League\JsonGuard\Constraint\DraftFour; |
||
4 | |||
5 | use League\JsonGuard\Assert; |
||
6 | use League\JsonGuard\ConstraintInterface; |
||
7 | use League\JsonGuard\Validator; |
||
8 | use function League\JsonGuard\pointer_push; |
||
9 | |||
10 | final class Items implements ConstraintInterface |
||
11 | { |
||
12 | const KEYWORD = 'items'; |
||
13 | |||
14 | /** |
||
15 | * {@inheritdoc} |
||
16 | */ |
||
17 | 14 | public function validate($value, $parameter, Validator $validator) |
|
18 | { |
||
19 | 14 | Assert::type($parameter, ['array', 'object'], self::KEYWORD, $validator->getSchemaPath()); |
|
20 | |||
21 | 12 | if (!is_array($value)) { |
|
22 | 4 | return null; |
|
23 | } |
||
24 | |||
25 | 10 | $errors = []; |
|
26 | 10 | View Code Duplication | foreach ($value as $key => $itemValue) { |
0 ignored issues
–
show
|
|||
27 | 10 | $schema = self::getSchema($parameter, $key); |
|
28 | |||
29 | // Additional items are allowed by default, |
||
30 | // so there might not be a schema for this. |
||
31 | 10 | if (is_null($schema)) { |
|
32 | 2 | continue; |
|
33 | } |
||
34 | |||
35 | 10 | $subValidator = $validator->makeSubSchemaValidator( |
|
36 | 5 | $itemValue, |
|
37 | 5 | $schema, |
|
38 | 10 | pointer_push($validator->getDataPath(), $key), |
|
39 | 10 | pointer_push($validator->getSchemaPath(), $key) |
|
40 | 5 | ); |
|
41 | 10 | $errors = array_merge($errors, $subValidator->errors()); |
|
42 | 5 | } |
|
43 | |||
44 | 10 | return $errors ?: null; |
|
45 | } |
||
46 | |||
47 | /** |
||
48 | * @param $parameter |
||
49 | * @param $key |
||
50 | * |
||
51 | * @return mixed |
||
52 | */ |
||
53 | 10 | private static function getSchema($parameter, $key) |
|
54 | { |
||
55 | 10 | if (is_object($parameter)) { |
|
56 | // list validation |
||
57 | 8 | return $parameter; |
|
58 | 6 | } elseif (is_array($parameter) && array_key_exists($key, $parameter)) { |
|
59 | // tuple validation |
||
60 | 6 | return $parameter[$key]; |
|
61 | } |
||
62 | |||
63 | 2 | return null; |
|
64 | } |
||
65 | } |
||
66 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.