ArrayValueObject   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
dl 0
loc 52
rs 10
c 1
b 0
f 0
wmc 11

2 Methods

Rating   Name   Duplication   Size   Complexity  
B getValueByArray() 0 45 10
A getValue() 0 3 1
1
<?php
2
3
namespace Dtc\GridBundle\Mapper;
4
5
use ArrayObject;
6
use Exception;
7
8
class ArrayValueObject extends ArrayObject
9
{
10
    public function getValueByArray(array $params)
11
    {
12
        $data = &$this;
13
        $total = count($params);
14
        $key = current($params);
15
16
        if (0 === $total) {
17
            throw new Exception('requires at least 1 arg');
18
        }
19
20
        if (null === $key) {
21
            throw new Exception('requires non NULL args');
22
        }
23
        if (!is_scalar($key)) {
24
            throw new Exception('requires scalar args');
25
        }
26
        if (!isset($data[$key])) {
27
            return null;
28
        }
29
30
        if (1 === $total) {
31
            return $data[$key];
32
        }
33
34
        $data = &$data[$key];
35
        $args = array_slice($params, 1);
36
37
        $count = 0;
38
        foreach ($args as $key) {
39
            if ($count++ > 100) {
40
                exit();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
41
            }
42
43
            if (is_array($data)) {
44
                if (!isset($data[$key])) {
45
                    return null;
46
                } else {
47
                    $data = &$data[$key];
48
                }
49
            } else {
50
                return null;
51
            }
52
        }
53
54
        return $data;
55
    }
56
57
    public function getValue()
58
    {
59
        return $this->getValueByArray(func_get_args());
60
    }
61
}
62