ArrayTrait   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 94
Duplicated Lines 19.15 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 18
loc 94
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
C validateArray() 18 49 13
addError() 0 1 ?
numberToOrdinal() 0 1 ?
conjugationObject() 0 1 ?
conjugationToBe() 0 1 ?

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php namespace CMPayments\SchemaValidator\Validators;
2
3
use CMPayments\SchemaValidator\Exceptions\ValidateException;
4
5
/**
6
 * Class ArrayTrait
7
 *
8
 * @package CMPayments\SchemaValidator\Validators
9
 * @Author  Boy Wijnmaalen <[email protected]>
10
 */
11
trait ArrayTrait
12
{
13
    /**
14
     * Validate an array
15
     *
16
     * @param $data
17
     * @param $schema
18
     * @param $path
19
     */
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)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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);
0 ignored issues
show
Bug introduced by
The method validate() does not exist on CMPayments\SchemaValidator\Validators\ArrayTrait. Did you maybe mean validateArray()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
67
        }
68
    }
69
70
    /**
71
     * @param int   $code
72
     * @param array $args
73
     *
74
     * @return mixed
75
     */
76
    abstract public function addError($code, array $args = []);
77
78
    /**
79
     * @param int $number
80
     *
81
     * @return mixed
82
     */
83
    abstract public function numberToOrdinal($number);
84
85
    /**
86
     * Returns a valid representation of 'items' (or other value)
87
     *
88
     * @param int    $count
89
     * @param string $single
90
     * @param string $plural
91
     *
92
     * @return string
93
     */
94
    abstract public function conjugationObject($count, $single = 'item', $plural = 'items');
95
96
    /**
97
     * Returns a valid conjugation of the verb 'to be'
98
     *
99
     * @param int $count
100
     *
101
     * @return string
102
     */
103
    abstract public function conjugationToBe($count);
104
}
105