UniqueItems::isValid()   B
last analyzed

Complexity

Conditions 10
Paths 15

Size

Total Lines 24
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 10

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 24
ccs 20
cts 20
cp 1
rs 7.6666
c 0
b 0
f 0
cc 10
nc 15
nop 1
crap 10

How to fix   Complexity   

Long Method

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:

1
<?php
2
3
namespace Swaggest\JsonSchema\Constraint;
4
5
use Swaggest\JsonSchema\Structure\ObjectItemContract;
6
7
class UniqueItems
8
{
9
    /**
10
     * @param array $data
11
     * @return bool
12
     * @todo optimize a lot
13
     */
14 525
    public static function isValid(array $data)
15
    {
16 525
        $index = array();
17 525
        foreach ($data as $value) {
18 507
            if (is_array($value) || $value instanceof \stdClass) {
19 54
                $value = json_encode($value);
20 489
            } elseif (is_bool($value)) {
21 41
                $value = '_B' . $value;
22 489
            } elseif (is_string($value)) {
23 456
                $value = '_S' . $value;
24 64
            } elseif (is_int($value)) {
25 56
                $value = '_I' . $value;
26 12
            } elseif (is_float($value)) {
27 3
                $value = '_F' . $value;
28 9
            } elseif ($value instanceof ObjectItemContract) {
29 2
                $value = json_encode($value);
30
            }
31 507
            $tmp = &$index[$value];
32 507
            if ($tmp !== null) {
33 22
                return false;
34
            }
35 507
            $tmp = true;
36
        }
37 506
        return true;
38
    }
39
40
}