ConfigValueHolder   A
last analyzed

Complexity

Total Complexity 35

Size/Duplication

Total Lines 391
Duplicated Lines 2.56 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 10
loc 391
rs 9
c 0
b 0
f 0
wmc 35
lcom 1
cbo 1

21 Methods

Rating   Name   Duplication   Size   Complexity  
A addChildren() 0 9 2
A appendChildren() 0 4 1
A hasChildren() 5 21 4
A getChildren() 0 16 4
A setAttribute() 0 4 1
A hasAttribute() 0 4 1
A getAttribute() 0 4 2
A getAttributes() 0 4 1
A setValue() 0 4 1
A getValue() 0 4 1
A getNode() 0 9 1
A offsetExists() 0 4 1
A setName() 0 4 1
A getName() 0 4 1
A __isset() 0 4 1
B __get() 5 34 6
A offsetGet() 0 7 2
A offsetSet() 0 4 1
A offsetUnset() 0 4 1
A getIterator() 0 4 1
A __toString() 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
// +---------------------------------------------------------------------------+
4
// | This file is part of the Agavi package.                                   |
5
// | Copyright (c) 2005-2011 the Agavi Project.                                |
6
// |                                                                           |
7
// | For the full copyright and license information, please view the LICENSE   |
8
// | file that was distributed with this source code. You can also view the    |
9
// | LICENSE file online at http://www.agavi.org/LICENSE.txt                   |
10
// |   vi: set noexpandtab:                                                    |
11
// |   Local Variables:                                                        |
12
// |   indent-tabs-mode: t                                                     |
13
// |   End:                                                                    |
14
// +---------------------------------------------------------------------------+
15
16
namespace Agavi\Config;
17
18
use Agavi\Config\Util\Dom\XmlConfigDomElement;
19
use Agavi\Util\Inflector;
20
21
/**
22
 * ConfigValueHolder is the storage class for the XmlConfigHandler
23
 *
24
 * @package    agavi
25
 * @subpackage config
26
 *
27
 * @author     Dominik del Bondio <[email protected]>
28
 * @copyright  Authors
29
 * @copyright  The Agavi Project
30
 *
31
 * @since      0.11.0
32
 *
33
 * @deprecated Not used anymore by XML config handlers, to be removed in Agavi 1.1
34
 *
35
 * @version    $Id$
36
 */
37
class ConfigValueHolder implements \ArrayAccess, \IteratorAggregate
38
{
39
    /**
40
     * @var        string The name of this value.
41
     */
42
    protected $_name = '';
43
    /**
44
     * @var        array The attributes of this value.
45
     */
46
    protected $_attributes = array();
47
    /**
48
     * @var        array The child nodes of this value.
49
     */
50
    protected $_childs = array();
51
    /**
52
     * @var        string The value.
53
     */
54
    protected $_value = null;
55
56
    /**
57
     * Sets the name of this value.
58
     *
59
     * @param      string $name The name.
60
     *
61
     * @author     Dominik del Bondio <[email protected]>
62
     * @since      0.11.0
63
     */
64
    public function setName($name)
65
    {
66
        $this->_name = $name;
67
    }
68
69
    /**
70
     * Returns the name of this value.
71
     *
72
     * @return     string The name.
73
     *
74
     * @author     Dominik del Bondio <[email protected]>
75
     * @since      0.11.0
76
     */
77
    public function getName()
78
    {
79
        return $this->_name;
80
    }
81
82
    /**
83
     * isset() overload.
84
     *
85
     * @param      string $name Name of the child.
86
     *
87
     * @return     bool Whether or not that child exists.
88
     *
89
     * @author     Dominik del Bondio <[email protected]>
90
     * @since      0.11.0
91
     */
92
    public function __isset($name)
93
    {
94
        return $this->hasChildren($name);
95
    }
96
97
    /**
98
     * Magic getter overload.
99
     *
100
     * @param      string $name Name of the child .
101
     *
102
     * @return     ConfigValueHolder The child, if it exists.
103
     *
104
     * @author     Dominik del Bondio <[email protected]>
105
     * @since      0.11.0
106
     */
107
    public function __get($name)
108
    {
109
        if (isset($this->_childs[$name])) {
110
            return $this->_childs[$name];
111
        } else {
112
            $tagName = $name;
113
            $tagNameStart = '';
114 View Code Duplication
            if (($lastUScore = strrpos($tagName, '_')) !== false) {
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...
115
                $lastUScore++;
116
                $tagNameStart = substr($tagName, 0, $lastUScore);
117
                $tagName = substr($tagName, $lastUScore);
118
            }
119
120
            // check if the requested node was specified using the plural version
121
            // and create a "virtual" node which reflects the non existent plural node
122
            $singularName = $tagNameStart . Inflector::singularize($tagName);
123
            if ($this->hasChildren($singularName)) {
124
                $vh = new ConfigValueHolder();
0 ignored issues
show
Deprecated Code introduced by
The class Agavi\Config\ConfigValueHolder has been deprecated with message: Not used anymore by XML config handlers, to be removed in Agavi 1.1

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
125
                $vh->setName($name);
126
127
                /** @var ConfigValueHolder $child */
128
                foreach ($this->_childs as $child) {
129
                    if ($child->getName() == $singularName) {
130
                        $vh->addChildren($singularName, $child);
131
                    }
132
                }
133
134
                return $vh;
135
            } else {
136
                //throw new AgaviException('Node with the name ' . $name . ' does not exist ('.$this->getName().', '.implode(', ', array_keys($this->_childs)).')');
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
137
                return null;
138
            }
139
        }
140
    }
141
142
    /**
143
     * Adds a named children to this value. If a children with the same name
144
     * already exists the given value will be appended to the children.
145
     *
146
     * @param      string            $name     The name of the child.
147
     * @param      ConfigValueHolder $children The child value.
148
     *
149
     * @author     Dominik del Bondio <[email protected]>
150
     * @since      0.11.0
151
     */
152
    public function addChildren($name, $children)
153
    {
154
        if (!$this->hasChildren($name)) {
155
            $this->$name = $children;
156
            $this->_childs[$name] = $children;
157
        } else {
158
            $this->appendChildren($children);
159
        }
160
    }
161
162
    /**
163
     * Adds a unnamed children to this value.
164
     *
165
     * @param      ConfigValueHolder $children The child value.
166
     *
167
     * @author     Dominik del Bondio <[email protected]>
168
     * @since      0.11.0
169
     */
170
    public function appendChildren($children)
171
    {
172
        $this->_childs[] = $children;
173
    }
174
175
    /**
176
     * Checks whether the value has children at all (no params) or whether a
177
     * child with the given name exists.
178
     *
179
     * @param      string $child The name of the child.
180
     *
181
     * @return     bool True if children exist, false if not.
182
     *
183
     * @author     Dominik del Bondio <[email protected]>
184
     * @since      0.11.0
185
     */
186
    public function hasChildren($child = null)
187
    {
188
        if ($child === null) {
189
            return count($this->_childs) > 0;
190
        }
191
192
        if (isset($this->_childs[$child])) {
193
            return true;
194
        } else {
195
            $tagName = $child;
196
            $tagNameStart = '';
197 View Code Duplication
            if (($lastUScore = strrpos($tagName, '_')) !== false) {
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...
198
                $lastUScore++;
199
                $tagNameStart = substr($tagName, 0, $lastUScore);
200
                $tagName = substr($tagName, $lastUScore);
201
            }
202
203
            $singularName = $tagNameStart . Inflector::singularize($tagName);
204
            return isset($this->_childs[$singularName]);
205
        }
206
    }
207
208
    /**
209
     * Returns the children of this value.
210
     *
211
     * @param      string $nodename Return only the childs matching this node (tag) name.
212
     *
213
     * @return     ConfigValueHolder[] An array with the childs of this value.
214
     *
215
     * @author     Dominik del Bondio <[email protected]>
216
     * @since      0.11.0
217
     */
218
    public function getChildren($nodename = null)
219
    {
220
        if ($nodename === null) {
221
            return $this->_childs;
222
        } else {
223
            $childs = array();
224
            /** @var ConfigValueHolder $child */
225
            foreach ($this->_childs as $child) {
226
                if ($child->getName() == $nodename) {
227
                    $childs[] = $child;
228
                }
229
            }
230
231
            return $childs;
232
        }
233
    }
234
235
    /**
236
     * Set an attribute.
237
     *
238
     * If an attribute with the name already exists the value will be
239
     * overridden.
240
     *
241
     * @param      string $name  An attribute name.
242
     * @param      mixed  $value An attribute value.
243
     *
244
     * @author     Dominik del Bondio <[email protected]>
245
     * @since      0.11.0
246
     */
247
    public function setAttribute($name, $value)
248
    {
249
        $this->_attributes[$name] = $value;
250
    }
251
252
    /**
253
     * Indicates whether or not an attribute exists.
254
     *
255
     * @param      string $name An attribute name.
256
     *
257
     * @return     bool true, if the attribute exists, otherwise false.
258
     *
259
     * @author     Dominik del Bondio <[email protected]>
260
     * @since      0.11.0
261
     */
262
    public function hasAttribute($name)
263
    {
264
        return isset($this->_attributes[$name]);
265
    }
266
267
    /**
268
     * Retrieve an attribute.
269
     *
270
     * @param      string $name    An attribute name.
271
     * @param      mixed  $default A default attribute value.
272
     *
273
     * @return     mixed An attribute value, if the attribute exists, otherwise
274
     *                   null or the given default.
275
     *
276
     * @author     Dominik del Bondio <[email protected]>
277
     * @since      0.11.0
278
     */
279
    public function getAttribute($name, $default = null)
280
    {
281
        return isset($this->_attributes[$name]) ? $this->_attributes[$name] : $default;
282
    }
283
284
    /**
285
     * Retrieve all attributes.
286
     *
287
     * @return     array An associative array of attributes.
288
     *
289
     * @author     Dominik del Bondio <[email protected]>
290
     * @since      0.11.0
291
     */
292
    public function getAttributes()
293
    {
294
        return $this->_attributes;
295
    }
296
297
    /**
298
     * Set the value of this value node.
299
     *
300
     * @param      string $value A value.
301
     *
302
     * @author     Dominik del Bondio <[email protected]>
303
     * @since      0.11.0
304
     */
305
    public function setValue($value)
306
    {
307
        $this->_value = $value;
308
    }
309
310
    /**
311
     * Retrieves the value of this value node.
312
     *
313
     * @return     string The value of this node.
314
     *
315
     * @author     Dominik del Bondio <[email protected]>
316
     * @since      0.11.0
317
     */
318
    public function getValue()
319
    {
320
        return $this->_value;
321
    }
322
323
    /**
324
     * Retrieves the info of this value node.
325
     *
326
     * @return     array An array containing the info for this node.
327
     *
328
     * @author     Dominik del Bondio <[email protected]>
329
     * @since      0.11.0
330
     */
331
    public function getNode()
332
    {
333
        return array(
334
            'name' => $this->_name,
335
            'attributes' => $this->_attributes,
336
            'children' => $this->_childs,
337
            'value' => $this->_value,
338
        );
339
    }
340
341
    /**
342
     * Determines if a named child exists. From ArrayAccess.
343
     *
344
     * @param      string $offset Offset to check
345
     *
346
     * @return     bool Whether the offset exists.
347
     *
348
     * @author     Dominik del Bondio <[email protected]>
349
     * @since      0.11.0
350
     */
351
    public function offsetExists($offset)
352
    {
353
        return isset($this->_childs[$offset]);
354
    }
355
356
    /**
357
     * Retrieves a named child. From ArrayAccess.
358
     *
359
     * @param      string $offset Offset to retrieve
360
     *
361
     * @return     ConfigValueHolder The child value.
362
     *
363
     * @author     Dominik del Bondio <[email protected]>
364
     * @since      0.11.0
365
     */
366
    public function offsetGet($offset)
367
    {
368
        if (!isset($this->_childs[$offset])) {
369
            return null;
370
        }
371
        return $this->_childs[$offset];
372
    }
373
374
    /**
375
     * Inserts a named child. From ArrayAccess.
376
     *
377
     * @param      string            $offset Offset to modify
378
     * @param      ConfigValueHolder $value  The child value.
379
     *
380
     * @author     Dominik del Bondio <[email protected]>
381
     * @since      0.11.0
382
     */
383
    public function offsetSet($offset, $value)
384
    {
385
        $this->_childs[$offset] = $value;
386
    }
387
388
    /**
389
     * Deletes a named child. From ArrayAccess.
390
     *
391
     * @return     string $offset Offset to delete.
392
     *
393
     * @author     Dominik del Bondio <[email protected]>
394
     * @since      0.11.0
395
     */
396
    public function offsetUnset($offset)
397
    {
398
        unset($this->_childs[$offset]);
399
    }
400
401
    /**
402
     * Returns an Iterator for the child nodes. From IteratorAggregate.
403
     *
404
     * @return     \Iterator The iterator.
405
     *
406
     * @author     Dominik del Bondio <[email protected]>
407
     * @since      0.11.0
408
     */
409
    public function getIterator()
410
    {
411
        return new \ArrayIterator($this->getChildren());
412
    }
413
414
    /**
415
     * Retrieves the string representation of this value node. This is
416
     * currently only the value of the node.
417
     *
418
     * @return     string The string representation.
419
     *
420
     * @author     Dominik del Bondio <[email protected]>
421
     * @since      0.11.0
422
     */
423
    public function __toString()
424
    {
425
        return $this->_value;
426
    }
427
}
428