Conditions | 13 |
Paths | 48 |
Total Lines | 49 |
Code Lines | 20 |
Lines | 18 |
Ratio | 36.73 % |
Changes | 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 namespace CMPayments\SchemaValidator\Validators; |
||
20 | public function validateArray($data, $schema, $path) |
||
21 | { |
||
22 | // check for minItems property |
||
23 | View Code Duplication | if (isset($schema->minItems) && (count($data) < $schema->minItems)) { |
|
|
|||
24 | |||
25 | $count = count($data); |
||
26 | |||
27 | $this->addError( |
||
28 | ValidateException::ERROR_USER_ARRAY_MINIMUM_CHECK, |
||
29 | [$path, $schema->minItems, $this->conjugationObject($schema->minItems), $this->conjugationToBe($count), $count, $this->conjugationObject($count)] |
||
30 | ); |
||
31 | } |
||
32 | |||
33 | // check for maxItems property |
||
34 | View Code Duplication | if (isset($schema->maxItems) && (count($data) > $schema->maxItems)) { |
|
35 | |||
36 | $count = count($data); |
||
37 | |||
38 | $this->addError( |
||
39 | ValidateException::ERROR_USER_ARRAY_MAXIMUM_CHECK, |
||
40 | [$path, $schema->maxItems, $this->conjugationToBe($count), $count, $this->conjugationObject($count)] |
||
41 | ); |
||
42 | } |
||
43 | |||
44 | // check for uniqueItems property |
||
45 | if (isset($schema->uniqueItems) && $schema->uniqueItems && ($count = count($data))) { |
||
46 | |||
47 | if (count(array_unique($data, SORT_REGULAR)) !== $count) { |
||
48 | |||
49 | $this->addError(ValidateException::ERROR_USER_ARRAY_NO_DUPLICATES_ALLOWED, [$path]); |
||
50 | } |
||
51 | } |
||
52 | |||
53 | // to prevent that this action is done for every item in $data we'll do it now instead of in validateEnum() |
||
54 | // in order to prevent this action being redone in validateEnum, we unset the $schema->items->caseSensitive parameter |
||
55 | if ((isset($schema->items->caseSensitive) && !$schema->items->caseSensitive) && (isset($schema->items->enum))) { |
||
56 | |||
57 | $data = array_map('strtolower', $data); |
||
58 | $schema->items->enum = array_map('strtolower', $schema->items->enum); |
||
59 | |||
60 | unset($schema->items->caseSensitive); |
||
61 | } |
||
62 | |||
63 | // Continue checking if every $row in $data matches $schema->items |
||
64 | foreach ($data as $property => $row) { |
||
65 | |||
66 | $this->validate($schema->items, $this->numberToOrdinal($property + 1) . ' child', $row, $path); |
||
67 | } |
||
68 | } |
||
69 | |||
105 |
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.