Completed
Branch master (343030)
by Dominik
05:26 queued 02:33
created

ArrayNode::add()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
cc 1
eloc 5
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Saxulum\ElasticSearchQueryBuilder\Node;
4
5
class ArrayNode extends AbstractParentNode
6
{
7
    /**
8
     * @param AbstractNode $node
9
     * @param bool         $allowDefault
10
     *
11
     * @return $this
12
     */
13 4
    public function add(AbstractNode $node, $allowDefault = false)
14
    {
15 4
        $node->setParent($this);
16
17 4
        $this->children[] = $node;
18 4
        $this->allowDefault[] = $allowDefault;
19
20 4
        return $this;
21
    }
22
23
    /**
24
     * @return array
25
     */
26 1
    public function getDefault()
27
    {
28 1
        return [];
29
    }
30
31
    /**
32
     * @return \stdClass|null
33
     */
34 4
    public function serialize()
35
    {
36 4
        $serialized = [];
37 4
        foreach ($this->children as $i => $child) {
38 3
            $this->serializeChild($serialized, $i, $child);
39 4
        }
40
41 4
        if ([] === $serialized) {
42 2
            return;
43
        }
44
45 2
        return $serialized;
46
    }
47
48
    /**
49
     * @param array        $serialized
50
     * @param string       $name
0 ignored issues
show
Bug introduced by
There is no parameter named $name. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
51
     * @param AbstractNode $child
52
     */
53 3
    private function serializeChild(array &$serialized, $i, AbstractNode $child)
54
    {
55 3
        if (null !== $serializedChild = $child->serialize()) {
56 1
            $serialized[] = $serializedChild;
57 3
        } elseif ($this->allowDefault[$i]) {
58 1
            $serialized[] = $child->getDefault();
59 1
        }
60 3
    }
61
}
62