Completed
Pull Request — master (#23)
by Auke
02:10
created

NamedNodelist::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 4
Bugs 1 Features 1
Metric Value
c 4
b 1
f 1
dl 0
loc 5
ccs 0
cts 4
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 2
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
    public function __construct($list = null, $parentNode = null)
24
    {
25
        parent::__construct( $list );
26
        $this->parentNode = $parentNode;
27
    }
28
29
    public function __set($name, $value)
30
    {
31
        switch ($name) {
32
            case 'parentNode':
33
                $this->parentNode = $value;
34
                foreach ($this as $child) {
35
                    $child->parentNode = $this->parentNode;
36
                }
37
                break;
38
        }
39
    }
40
41
    public function __get($name)
42
    {
43
        switch ($name) {
44
            case 'parentNode':
45
                return $this->parentNode;
46
                break;
47
        }
48
    }
49
50
    public function offsetSet($name, $value)
51
    {
52
        if (!($value instanceof \arc\tree\NamedNode)) {
53
            $value = new \arc\tree\NamedNode( $name, $this->parentNode, null, $value );
54
        }
55
        $value->parentNode = $this->parentNode;
56
        if ($this->offsetExists( $name )) {
57
            $old = $this->offsetGet( $name );
58
            if ($old !== $value) {
59
                $old->parentNode = null;
60
            }
61
        }
62
        parent::offsetSet($name, $value);
63
    }
64
65
    public function offsetUnset($name)
66
    {
67
        if ($this->offsetExists( $name )) {
68
            $node = $this->offsetGet( $name );
69
            $node->parentNode = null;
70
        }
71
        parent::offsetUnset( $name );
72
    }
73
74
    public function __clone()
75
    {
76
        $this->parentNode = null;
77
        foreach ((array) $this as $name => $child) {
78
            $clone = clone $child;
79
            $clone->parentNode = $this->parentNode;
80
            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...
81
        }
82
    }
83
84
    public function exchangeArray($input)
85
    {
86
        $oldArray = $this->getArrayCopy();
87
        foreach ($oldArray as $node) {
88
            $node->parentNode = null; // removes them from the childNodes list as well
89
        }
90
        foreach ($input as $name => $node) {
91
            $this->offsetSet( $name, $node );
92
        }
93
    }
94
}
95