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

ChildTrait::getChild()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
ccs 11
cts 11
cp 1
rs 9.2
cc 4
eloc 8
nc 5
nop 1
crap 4
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
}