AbstractParent   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 61
Duplicated Lines 16.39 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getNodes() 0 4 1
A getNode() 0 4 2
addNode() 0 1 ?
removeNode() 0 1 ?
A checkNode() 10 10 4
A removeNodeFromParent() 0 6 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
abstract class AbstractParent extends AbstractElement
6
{
7
    /**
8
     * @var AbstractElement[]
9
     */
10
    protected $nodes = array();
11
12
    /**
13
     * @return AbstractElement[]
14
     */
15
    public function getNodes()
16
    {
17
        return $this->nodes;
18
    }
19
20
    /**
21
     * @param  int|string           $index node name or index, depends on object or array
22
     * @return AbstractElement|null
23
     */
24
    public function getNode($index)
25
    {
26
        return isset($this->nodes[$index]) ? $this->nodes[$index] : null;
27
    }
28
29
    /**
30
     * @param  AbstractElement $node
31
     * @return void
32
     */
33
    abstract public function addNode(AbstractElement $node);
34
35
    /**
36
     * @param  AbstractElement $node
37
     * @return void
38
     */
39
    abstract public function removeNode(AbstractElement $node);
40
41
    /**
42
     * @param  AbstractElement $node
43
     * @throws \Exception
44
     */
45 View Code Duplication
    protected function checkNode(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...
46
    {
47
        if (null === $node->getName()) {
48
            throw new \Exception("Use the create<nodetype>Node on document!");
49
        }
50
51
        if (null === $node->getDocument() || $this->getDocument() !== $node->getDocument()) {
52
            throw new \Exception("Use the create<nodetype>Node on document!");
53
        }
54
    }
55
56
    /**
57
     * @param AbstractElement $node
58
     */
59
    protected function removeNodeFromParent(AbstractElement $node)
60
    {
61
        if (null !== $parent = $node->getParent()) {
62
            $parent->removeNode($node);
63
        }
64
    }
65
}
66