Completed
Push — master ( 19108c...549f37 )
by Auke
23:31
created

NamedNodeList   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 80
Duplicated Lines 15 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 72.34%

Importance

Changes 0
Metric Value
dl 12
loc 80
ccs 34
cts 47
cp 0.7234
rs 10
c 0
b 0
f 0
wmc 19
lcom 1
cbo 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __get() 0 8 2
A __set() 0 11 3
A offsetSet() 6 16 5
A offsetUnset() 6 10 3
A __clone() 0 9 2
A exchangeArray() 0 10 3

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 Ariadne Component Library.
5
 *
6
 * (c) Muze <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace arc\tree;
13
14
/**
15
 * Implements an ArrayObject with constraint that a value is always a NamedNode and its key is always the name
16
 * of that node
17
 * @property \arc\tree\NamedNode $parentNode
18
 */
19
class NamedNodeList extends \ArrayObject
20
{
21
    private $parentNode = null;
22
23 16
    public function __construct($list = null, $parentNode = null)
24
    {
25 16
        parent::__construct( $list );
26 16
        $this->parentNode = $parentNode;
27 16
    }
28
29 2
    public function __set($name, $value)
30
    {
31
        switch ($name) {
32 2
            case 'parentNode':
33 2
                $this->parentNode = $value;
34 2
                foreach ($this as $child) {
35 2
                    $child->parentNode = $this->parentNode;
36
                }
37 2
                break;
38
        }
39 2
    }
40
41
    public function __get($name)
42
    {
43
        switch ($name) {
44
            case 'parentNode':
45
                return $this->parentNode;
46
                break;
47
        }
48
    }
49
50 16
    public function offsetSet($name, $value)
51
    {
52 16
        if (!($value instanceof \arc\tree\NamedNode)) {
53 6
            $value = new \arc\tree\NamedNode( $name, $this->parentNode, null, $value );
54
        }
55 16
        if ($value->parentNode != $this->parentNode) {
56
            $value->parentNode = $this->parentNode;
57
        }
58 16 View Code Duplication
        if ($this->offsetExists( $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...
59 2
            $old = $this->offsetGet( $name );
60 2
            if ($old !== $value) {
61 2
                $old->parentNode = null;
62
            }
63
        }
64 16
        parent::offsetSet($name, $value);
65 16
    }
66
67 4
    public function offsetUnset($name)
68
    {
69 4 View Code Duplication
        if ($this->offsetExists( $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...
70 4
            $node = $this->offsetGet( $name );
71 4
            if ($node->parentNode) {
72
                $node->parentNode = null;
73
            }
74
        }
75 4
        parent::offsetUnset( $name );
76 4
    }
77
78 2
    public function __clone()
79
    {
80 2
        $this->parentNode = null;
81 2
        foreach ((array) $this as $name => $child) {
82 2
            $clone = clone $child;
83 2
            $clone->parentNode = $this->parentNode;
84 2
            parent::offsetSet( $name, $clone );
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (offsetSet() instead of __clone()). Are you sure this is correct? If so, you might want to change this to $this->offsetSet().

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...
85
        }
86 2
    }
87
88
    public function exchangeArray($input)
89
    {
90
        $oldArray = $this->getArrayCopy();
91
        foreach ($oldArray as $node) {
92
            $node->parentNode = null; // removes them from the childNodes list as well
93
        }
94
        foreach ($input as $name => $node) {
95
            $this->offsetSet( $name, $node );
96
        }
97
    }
98
}
99