Completed
Push — master ( fad66a...6b73b6 )
by Edgar
03:36
created

ChildTrait   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 17
c 4
b 0
f 0
lcom 1
cbo 2
dl 0
loc 100
ccs 42
cts 42
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getChild() 0 15 4
B getChildById() 0 19 6
A hasChild() 0 4 1
A getChildren() 0 4 1
A getChildAtIndex() 0 4 1
A removeChild() 0 11 2
A getFirstChild() 0 8 2
1
<?php
2
namespace nstdio\svg\traits;
3
4
5
use nstdio\svg\container\ContainerInterface;
6
use nstdio\svg\ElementInterface;
7
use nstdio\svg\ElementStorage;
8
use nstdio\svg\SVGElement;
9
use nstdio\svg\XMLDocumentInterface;
10
11
/**
12
 * Class ChildTrait
13
 *
14
 * @package nstdio\svg\traits
15
 * @author  Edgar Asatryan <[email protected]>
16
 */
17
trait ChildTrait
18
{
19
    /**
20
     * @var ElementStorage
21
     */
22
    protected $child;
23
24
    /**
25
     * @inheritdoc
26
     */
27 7
    public function getChild($name)
28
    {
29 7
        $find = [];
30
        /** @var ContainerInterface $item */
31 7
        foreach ($this->child as $item) {
32 7
            if (strtolower($item->getName()) === strtolower($name)) {
33 7
                $find[] = $item;
34 7
            }
35 7
            if ($item->hasChild()) {
36 3
                $find = array_merge($find, $item->getChild($name));
37 3
            }
38 7
        }
39
40 7
        return $find;
41
    }
42
43
    /**
44
     * @inheritdoc
45
     */
46 4
    public function getChildById($id)
47
    {
48 4
        $find = null;
49
        /** @var ContainerInterface|SVGElement $item */
50 4
        foreach ($this->child as $item) {
51 3
            if ($item->id === $id) {
52 3
                $find = $item;
53 3
            }
54 4
        }
55 4
        if ($find === null) {
56 4
            foreach ($this->child as $item) {
57 3
                if ($item->hasChild()) {
58 3
                    $find = $item->getChildById($id);
59 3
                }
60 4
            }
61 4
        }
62
63 4
        return $find;
64
    }
65
66
    /**
67
     * @inheritdoc
68
     */
69 53
    public function getFirstChild()
70
    {
71 53
        if (!$this->hasChild()) {
72 1
            return null;
73
        }
74
75 53
        return $this->child[0];
76
    }
77
78
    /**
79
     * @inheritdoc
80
     */
81 53
    public function hasChild()
82
    {
83 53
        return count($this->child) > 0;
84
    }
85
86
    /**
87
     * @inheritdoc
88
     */
89 2
    public function getChildren()
90
    {
91 2
        return $this->child;
92
    }
93
94
    /**
95
     * @inheritdoc
96
     */
97 8
    public function getChildAtIndex($index)
98
    {
99 8
        return $this->child[$index];
100
    }
101
102
    /**
103
     * @inheritdoc
104
     */
105 9
    public function removeChild(ElementInterface $child)
106
    {
107 9
        $this->child->remove($child);
108
        /** @var XMLDocumentInterface $element */
109 9
        $element = $this->getElement();
0 ignored issues
show
Bug introduced by
It seems like getElement() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
110 9
        if ($element instanceof \DOMNode) {
111 1
            return $element->removeChild($child->getElement()->getElement());
0 ignored issues
show
Bug introduced by
The method getElement() does not exist on DOMElement. Did you maybe mean getElementsByTagName()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
112
        } else {
113 9
            return $element->removeNode($child->getElement());
0 ignored issues
show
Documentation introduced by
$child->getElement() is of type object<DOMElement>, but the function expects a object<nstdio\svg\XMLDocumentInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
114
        }
115
    }
116
}