ObjectNode   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 115
Duplicated Lines 45.22 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 0
Metric Value
wmc 17
lcom 2
cbo 4
dl 52
loc 115
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A addAttribute() 12 12 2
A removeAttribute() 9 9 2
A checkAttribute() 10 10 4
A removeAttributeFromParent() 0 7 2
A getAttributes() 0 4 1
A getAttribute() 0 4 2
A addNode() 12 12 2
A removeNode() 9 9 2

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
namespace Saxulum\JsonDocument;
4
5
class ObjectNode extends AbstractParent
6
{
7
    /**
8
     * @var AttributeNode[]
9
     */
10
    protected $attributes = array();
11
12
    /**
13
     * @param  AttributeNode $attribute
14
     * @return void
15
     */
16 View Code Duplication
    public function addAttribute(AttributeNode $attribute)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
17
    {
18
        $name = $attribute->getName();
19
        if (isset($this->attributes[$name])) {
20
            throw new \InvalidArgumentException(sprintf("There is allready a attribute with this name '%s'!", $name));
21
        }
22
23
        $this->checkAttribute($attribute);
24
        $this->removeAttributeFromParent($attribute);
25
        Document::setProperty($attribute, 'parent', $this);
26
        $this->attributes[$attribute->getName()] = $attribute;
27
    }
28
29
    /**
30
     * @param  AttributeNode $attribute
31
     * @return void
32
     * @throw \InvalidArgumentException
33
     */
34 View Code Duplication
    public function removeAttribute(AttributeNode $attribute)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
35
    {
36
        $name = $attribute->getName();
37
        if (!isset($this->attributes[$name])) {
38
            throw new \InvalidArgumentException("Unknown node!");
39
        }
40
41
        Document::setProperty($attribute, 'parent', null);
42
    }
43
44
    /**
45
     * @param  AttributeNode $node
46
     * @return void
47
     * @throws \Exception
48
     */
49 View Code Duplication
    protected function checkAttribute(AttributeNode $node)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
50
    {
51
        if (null === $node->getName()) {
52
            throw new \Exception("Use the createAttributeNode on document!");
53
        }
54
55
        if (null === $node->getDocument() || $this->getDocument() !== $node->getDocument()) {
56
            throw new \Exception("Use the createAttributeNode on document!");
57
        }
58
    }
59
60
    /**
61
     * @param  AttributeNode $node
62
     * @return void
63
     */
64
    protected function removeAttributeFromParent(AttributeNode $node)
65
    {
66
        if (null !== $parent = $node->getParent()) {
67
            /** @var ObjectNode $parent */
68
            $parent->removeAttribute($node);
69
        }
70
    }
71
72
    /**
73
     * @return AttributeNode[]
74
     */
75
    public function getAttributes()
76
    {
77
        return $this->attributes;
78
    }
79
80
    /**
81
     * @param  string             $name
82
     * @return AttributeNode|null
83
     */
84
    public function getAttribute($name)
85
    {
86
        return isset($this->attributes[$name]) ? $this->attributes[$name] : null;
87
    }
88
89
    /**
90
     * @param  AbstractElement $node
91
     * @return void
92
     */
93 View Code Duplication
    public function addNode(AbstractElement $node)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
94
    {
95
        $name = $node->getName();
96
        if (isset($this->nodes[$name])) {
97
            throw new \InvalidArgumentException("There is allready a node with this name!");
98
        }
99
100
        $this->checkNode($node);
101
        $this->removeNodeFromParent($node);
102
        Document::setProperty($node, 'parent', $this);
103
        $this->nodes[$node->getName()] = $node;
104
    }
105
106
    /**
107
     * @param  AbstractElement $node
108
     * @return void
109
     */
110 View Code Duplication
    public function removeNode(AbstractElement $node)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
111
    {
112
        $name = $node->getName();
113
        if (!isset($this->nodes[$name])) {
114
            throw new \InvalidArgumentException("Unknown node!");
115
        }
116
117
        Document::setProperty($node, 'parent', null);
118
    }
119
}
120