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

NamedNodeList::offsetSet()   A

Complexity

Conditions 5
Paths 12

Size

Total Lines 16

Duplication

Lines 6
Ratio 37.5 %

Code Coverage

Tests 10
CRAP Score 5.0187

Importance

Changes 0
Metric Value
cc 5
nc 12
nop 2
dl 6
loc 16
ccs 10
cts 11
cp 0.9091
crap 5.0187
rs 9.4222
c 0
b 0
f 0
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