ArrayConfig   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 7
Bugs 0 Features 2
Metric Value
wmc 12
c 7
b 0
f 2
lcom 1
cbo 1
dl 0
loc 77
ccs 31
cts 31
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A restrict() 0 4 1
A offsetSet() 0 5 1
A buildConfig() 0 4 1
A formatKeys() 0 10 3
A replace() 0 11 4
1
<?php
2
namespace Puppy\Config;
3
4
/**
5
 * Class SimpleConfig
6
 * @package Puppy\Config
7
 * @author Raphaël Lefebvre <[email protected]>
8
 */
9
class ArrayConfig extends \ArrayObject
10
{
11
12
    /**
13
     * @param array|\ArrayObject $data
14
     */
15 23
    public function __construct($data)
16
    {
17 23
        if(!($data instanceof self)){
18 23
            $data = $this->buildConfig($data);
19 23
        }
20 23
        parent::__construct($data);
21 23
    }
22
23
    /**
24
     * restricts the config to a specific visibility.
25
     *
26
     * @param string $restriction
27
     * @param string $separator
28
     * @return RestrictedConfig
29
     */
30 2
    public function restrict($restriction, $separator = '.')
31
    {
32 2
        return new RestrictedConfig($restriction, $separator, $this);
33
    }
34
35
    /**
36
     * @param mixed $key
37
     * @param mixed $value
38
     */
39 6
    public function offsetSet($key, $value)
40
    {
41 6
        parent::offsetSet($key, $value);
42 6
        $this->exchangeArray($this->buildConfig(parent::getArrayCopy()));
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getArrayCopy() instead of offsetSet()). Are you sure this is correct? If so, you might want to change this to $this->getArrayCopy().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
43 6
    }
44
45
    /**
46
     * @param array|\ArrayObject $data
47
     * @return array
48
     */
49 23
    private function buildConfig($data)
50
    {
51 23
        return $this->replace($data, $this->formatKeys($data));
52
    }
53
54
    /**
55
     * @param array|\ArrayObject $data
56
     * @return array
57
     */
58 23
    private function formatKeys($data)
59
    {
60 23
        $result = [];
61 23
        foreach ($data as $key => $value) {
62 21
            if (is_string($value)) {
63 21
                $result['%' . $key . '%'] = $value;
64 21
            }
65 23
        }
66 23
        return $result;
67
    }
68
69
    /**
70
     * @param $data
71
     * @param array $replacements
72
     * @return mixed
73
     */
74 23
    private function replace($data, array $replacements)
75
    {
76 23
        foreach ($data as $key => $var) {
77 21
            if (is_string($var)) {
78 21
                $data[$key] = strtr($var, $replacements);
79 21
            }elseif(is_array($var)){
80 2
                $data[$key] = $this->replace($var, $replacements);
81 2
            }
82 23
        }
83 23
        return $data;
84
    }
85
}
86