Ajde_Object_Magic   B
last analyzed

Complexity

Total Complexity 37

Size/Duplication

Total Lines 202
Duplicated Lines 6.93 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 14
loc 202
rs 8.6
c 0
b 0
f 0
wmc 37
lcom 1
cbo 4

17 Methods

Rating   Name   Duplication   Size   Complexity  
C __call() 8 28 7
A set() 0 4 1
A _set() 0 8 2
A remove() 0 4 1
A get() 6 9 2
A _get() 0 8 2
A has() 0 10 2
A isEmpty() 0 8 2
A hasEmpty() 0 4 2
A hasNotEmpty() 0 8 2
A reset() 0 4 1
A values() 0 4 1
A merge() 0 8 2
B valuesAsSingleDimensionArray() 0 23 6
A fromCamelCase() 0 7 1
A toCamelCase() 0 9 2
A classnameToUppercase() 0 4 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
2
3
abstract class Ajde_Object_Magic extends Ajde_Object
4
{
5
    protected $_data = [];
6
7
    final public function __call($method, $arguments)
8
    {
9
        $prefix = strtolower(substr($method, 0, 3));
10
        $key = substr($method, 3);
11
        $key = strtolower(substr($key, 0, 1)).substr($key, 1);
12
        switch ($prefix) {
13
            case 'get':
14 View Code Duplication
                if ($this->has($key)) {
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...
15
                    return $this->get($key);
16
                } else {
17
                    if (!method_exists($this, '__fallback')) {
18
                        throw new Ajde_Exception("Property '$key' not set in class ".get_class($this)." when calling get('$key')",
19
                            90007);
20
                    }
21
                }
22
                break;
23
            case 'set':
24
                return $this->set($key, $arguments[0]);
25
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
26
            case 'has':
27
                return $this->has($key);
28
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
29
        }
30
        if (method_exists($this, '__fallback')) {
31
            return call_user_func_array([$this, '__fallback'], [$method, $arguments]);
32
        }
33
        throw new Ajde_Exception('Call to undefined method '.get_class($this)."::$method()", 90006);
34
    }
35
36
    /**
37
     * TODO.
38
     *
39
     * @param string $key
40
     * @param mixed  $value
41
     */
42
    public function set($key, $value)
43
    {
44
        $this->_set($key, $value);
45
    }
46
47
    /**
48
     * TODO.
49
     *
50
     * @param string $key
51
     * @param mixed  $value
52
     */
53
    protected function _set($key, $value)
54
    {
55
        if (substr_count($key, '.')) {
56
            Ajde_Core_Array::set($this->_data, $key, $value);
57
        } else {
58
            $this->_data[$key] = $value;
59
        }
60
    }
61
62
    public function remove($key)
63
    {
64
        unset($this->_data[$key]);
65
    }
66
67
    public function get($key)
68
    {
69 View Code Duplication
        if ($this->has($key)) {
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...
70
            return $this->_get($key);
71
        } else {
72
            throw new Ajde_Exception("Parameter '$key' not set in class ".get_class($this)." when calling get('$key')",
73
                90007);
74
        }
75
    }
76
77
    protected function _get($key)
78
    {
79
        if (substr_count($key, '.')) {
80
            return Ajde_Core_Array::get($this->_data, $key);
81
        }
82
83
        return $this->_data[$key];
84
    }
85
86
    public function has($key)
87
    {
88
        $exist = array_key_exists($key, $this->_data);
89
90
        if ($exist === false) {
91
            $exist = !is_null(Ajde_Core_Array::get($this->_data, $key));
92
        }
93
94
        return (bool) $exist;
95
    }
96
97
    public function isEmpty($key)
98
    {
99
        $value = $this->get($key);
100
101
        return $value instanceof Ajde_Model ?
102
            !$value->hasLoaded() :
103
            empty($value);
104
    }
105
106
    public function hasEmpty($key)
107
    {
108
        return $this->has($key) && $this->isEmpty($key);
109
    }
110
111
    public function hasNotEmpty($key)
112
    {
113
        if ($this->has($key)) {
114
            return !$this->isEmpty($key);
115
        }
116
117
        return false;
118
    }
119
120
    public function reset()
121
    {
122
        $this->_data = [];
123
    }
124
125
    final public function values()
126
    {
127
        return $this->_data;
128
    }
129
130
    final public function merge($key, array $value)
131
    {
132
        if (!$this->has($key)) {
133
            $this->set($key, []);
134
        }
135
136
        $this->set($key, Ajde_Core_Array::mergeRecursive($this->get($key), $value));
137
    }
138
139
    final public function valuesAsSingleDimensionArray()
140
    {
141
        $array = [];
142
        foreach ($this->_data as $k => $item) {
143
            if (is_string($item)) {
144
                $array[$k] = $item;
145
            } else {
146
                if (is_array($item)) {
147
                    $array[$k] = serialize($item);
148
                } else {
149
                    if ($item instanceof self) {
150
                        $array[$k] = serialize($item->valuesAsSingleDimensionArray());
151
                    } else {
152
                        if (is_object($item)) {
153
                            $array[$k] = serialize($item);
154
                        }
155
                    }
156
                }
157
            }
158
        }
159
160
        return $array;
161
    }
162
163
    /**
164
     * Translates a camel case string into a string with underscores (e.g. firstName -&gt; first_name).
165
     *
166
     * @see http://www.paulferrett.com/2009/php-camel-case-functions/
167
     *
168
     * @param string $str String in camel case format
169
     *
170
     * @return string $str Translated into underscore format
171
     */
172
    public static function fromCamelCase($str)
173
    {
174
        $str[0] = strtolower($str[0]);
175
        $func = create_function('$c', 'return "_" . strtolower($c[1]);');
176
177
        return preg_replace_callback('/([A-Z])/', $func, $str);
178
    }
179
180
    /**
181
     * Translates a string with underscores into camel case (e.g. first_name -&gt; firstName).
182
     *
183
     * @see http://www.paulferrett.com/2009/php-camel-case-functions/
184
     *
185
     * @param string $str                   String in underscore format
186
     * @param bool   $capitalise_first_char If true, capitalise the first char in $str
187
     *
188
     * @return string $str translated into camel caps
189
     */
190
    public static function toCamelCase($str, $capitalise_first_char = false)
191
    {
192
        if ($capitalise_first_char) {
193
            $str[0] = strtoupper($str[0]);
194
        }
195
        $func = create_function('$c', 'return strtoupper($c[1]);');
196
197
        return preg_replace_callback('/_([a-z])/', $func, $str);
198
    }
199
200
    public static function classnameToUppercase($classname)
201
    {
202
        return str_replace(' ', '_', ucwords(str_replace('_', ' ', $classname)));
203
    }
204
}
205