Data   A
last analyzed

Complexity

Total Complexity 40

Size/Duplication

Total Lines 240
Duplicated Lines 6.67 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 40
lcom 1
cbo 1
dl 16
loc 240
rs 9.2
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A getData() 0 4 1
A clear() 0 12 4
A setData() 0 4 1
A mergeData() 0 9 3
A assign() 0 11 3
A __set() 0 4 1
A assignByRef() 0 4 1
C append() 13 28 13
A appendByRef() 3 14 6
A isAssigned() 0 4 1
A __isset() 0 4 1
A unassign() 0 4 1
A __unset() 0 4 1
A get() 0 4 1
A __get() 0 8 2

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Data often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Data, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * Copyright (c) 2013-2017
4
 *
5
 * @category  Library
6
 * @package   Dwoo
7
 * @author    Jordi Boggiano <[email protected]>
8
 * @author    David Sanchez <[email protected]>
9
 * @copyright 2008-2013 Jordi Boggiano
10
 * @copyright 2013-2017 David Sanchez
11
 * @license   http://dwoo.org/LICENSE Modified BSD License
12
 * @version   1.3.4
13
 * @date      2017-03-01
14
 * @link      http://dwoo.org/
15
 */
16
17
namespace Dwoo;
18
19
/**
20
 * Dwoo data object, use it for complex data assignments or if you want to easily pass it
21
 * around multiple functions to avoid passing an array by reference.
22
 * This software is provided 'as-is', without any express or implied warranty.
23
 * In no event will the authors be held liable for any damages arising from the use of this software.
24
 */
25
class Data implements IDataProvider
26
{
27
    /**
28
     * Data array.
29
     *
30
     * @var array
31
     */
32
    protected $data = array();
33
34
    /**
35
     * Returns the data array.
36
     *
37
     * @return array
38
     */
39
    public function getData()
40
    {
41
        return $this->data;
42
    }
43
44
    /**
45
     * Clears a the entire data or only the given key.
46
     *
47
     * @param array|string $name clears only one value if you give a name, multiple values if
48
     *                           you give an array of names, or the entire data if left null
49
     */
50
    public function clear($name = null)
51
    {
52
        if ($name === null) {
53
            $this->data = array();
54
        } elseif (is_array($name)) {
55
            foreach ($name as $index) {
56
                unset($this->data[$index]);
57
            }
58
        } else {
59
            unset($this->data[$name]);
60
        }
61
    }
62
63
    /**
64
     * Overwrites the entire data with the given array.
65
     *
66
     * @param array $data the new data array to use
67
     */
68
    public function setData(array $data)
69
    {
70
        $this->data = $data;
71
    }
72
73
    /**
74
     * merges the given array(s) with the current data with array_merge.
75
     *
76
     * @param array $data  the array to merge
77
     */
78
    public function mergeData(array $data)
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
79
    {
80
        $args = func_get_args();
81
        foreach ($args as $key => $v) {
82
            if (is_array($v)) {
83
                $this->data = array_merge($this->data, $v);
84
            }
85
        }
86
    }
87
88
    /**
89
     * Assigns a value or an array of values to the data object.
90
     *
91
     * @param array|string $name an associative array of multiple (index=>value) or a string
92
     *                           that is the index to use, i.e. a value assigned to "foo" will be
93
     *                           accessible in the template through {$foo}
94
     * @param mixed        $val  the value to assign, or null if $name was an array
95
     */
96
    public function assign($name, $val = null)
97
    {
98
        if (is_array($name)) {
99
            reset($name);
100
            foreach ($name as $k => $v){
101
                $this->data[$k] = $v;
102
            }
103
        } else {
104
            $this->data[$name] = $val;
105
        }
106
    }
107
108
    /**
109
     * Allows to assign variables using the object syntax.
110
     *
111
     * @param string $name  the variable name
112
     * @param string $value the value to assign to it
113
     */
114
    public function __set($name, $value)
115
    {
116
        $this->assign($name, $value);
117
    }
118
119
    /**
120
     * Assigns a value by reference to the data object.
121
     *
122
     * @param string $name the index to use, i.e. a value assigned to "foo" will be
123
     *                     accessible in the template through {$foo}
124
     * @param mixed  $val  the value to assign by reference
125
     */
126
    public function assignByRef($name, &$val)
127
    {
128
        $this->data[$name] = &$val;
129
    }
130
131
    /**
132
     * Appends values or an array of values to the data object.
133
     *
134
     * @param array|string $name  an associative array of multiple (index=>value) or a string
135
     *                            that is the index to use, i.e. a value assigned to "foo" will be
136
     *                            accessible in the template through {$foo}
137
     * @param mixed        $val   the value to assign, or null if $name was an array
138
     * @param bool         $merge true to merge data or false to append, defaults to false
139
     */
140
    public function append($name, $val = null, $merge = false)
141
    {
142
        if (is_array($name)) {
143
            foreach ($name as $key => $val) {
144 View Code Duplication
                if (isset($this->data[$key]) && !is_array($this->data[$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...
145
                    settype($this->data[$key], 'array');
146
                }
147
148 View Code Duplication
                if ($merge === true && is_array($val)) {
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...
149
                    $this->data[$key] = $val + $this->data[$key];
150
                } else {
151
                    $this->data[$key][] = $val;
152
                }
153
            }
154
        } elseif ($val !== null) {
155
            if (isset($this->data[$name]) && !is_array($this->data[$name])) {
156
                settype($this->data[$name], 'array');
157
            } elseif (!isset($this->data[$name])) {
158
                $this->data[$name] = array();
159
            }
160
161 View Code Duplication
            if ($merge === true && is_array($val)) {
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...
162
                $this->data[$name] = $val + $this->data[$name];
163
            } else {
164
                $this->data[$name][] = $val;
165
            }
166
        }
167
    }
168
169
    /**
170
     * Appends a value by reference to the data object.
171
     *
172
     * @param string $name  the index to use, i.e. a value assigned to "foo" will be
173
     *                      accessible in the template through {$foo}
174
     * @param mixed  $val   the value to append by reference
175
     * @param bool   $merge true to merge data or false to append, defaults to false
176
     */
177
    public function appendByRef($name, &$val, $merge = false)
178
    {
179 View Code Duplication
        if (isset($this->data[$name]) && !is_array($this->data[$name])) {
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...
180
            settype($this->data[$name], 'array');
181
        }
182
183
        if ($merge === true && is_array($val)) {
184
            foreach ($val as $key => &$value) {
185
                $this->data[$name][$key] = &$value;
186
            }
187
        } else {
188
            $this->data[$name][] = &$val;
189
        }
190
    }
191
192
    /**
193
     * Returns true if the variable has been assigned already, false otherwise.
194
     *
195
     * @param string $name the variable name
196
     *
197
     * @return bool
198
     */
199
    public function isAssigned($name)
200
    {
201
        return isset($this->data[$name]);
202
    }
203
204
    /**
205
     * Supports calls to isset($dwoo->var).
206
     *
207
     * @param string $name the variable name
208
     *
209
     * @return bool
210
     */
211
    public function __isset($name)
212
    {
213
        return isset($this->data[$name]);
214
    }
215
216
    /**
217
     * Unassigns/removes a variable.
218
     *
219
     * @param string $name the variable name
220
     */
221
    public function unassign($name)
222
    {
223
        unset($this->data[$name]);
224
    }
225
226
    /**
227
     * Supports unsetting variables using the object syntax.
228
     *
229
     * @param string $name the variable name
230
     */
231
    public function __unset($name)
232
    {
233
        unset($this->data[$name]);
234
    }
235
236
    /**
237
     * Returns a variable if it was assigned.
238
     *
239
     * @param string $name the variable name
240
     *
241
     * @return mixed
242
     */
243
    public function get($name)
244
    {
245
        return $this->__get($name);
246
    }
247
248
    /**
249
     * Allows to read variables using the object syntax.
250
     *
251
     * @param string $name the variable name
252
     *
253
     * @return mixed
254
     * @throws Exception
255
     */
256
    public function __get($name)
257
    {
258
        if (isset($this->data[$name])) {
259
            return $this->data[$name];
260
        } else {
261
            throw new Exception('Tried to read a value that was not assigned yet : "' . $name . '"');
262
        }
263
    }
264
}
265