Passed
Push — master ( c7bb8d...42feb8 )
by Dominik
02:16
created

ArrayNode::serializeEmpty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Saxulum\ElasticSearchQueryBuilder\Node;
6
7
final class ArrayNode extends AbstractParentNode
8
{
9
    /**
10
     * @param bool $allowSerializeEmpty
11
     */
12 7
    public function __construct(bool $allowSerializeEmpty = false)
13
    {
14 7
        $this->allowSerializeEmpty = $allowSerializeEmpty;
15 7
    }
16
17
    /**
18
     * @param AbstractNode $node
19
     *
20
     * @return $this
21
     *
22
     * @throws \InvalidArgumentException
23
     */
24 4
    public function add(AbstractNode $node)
25
    {
26 4
        $node->setParent($this);
27
28 4
        $this->children[] = $node;
29
30 4
        return $this;
31
    }
32
33
    /**
34
     * @return array
35
     */
36 1
    public function serializeEmpty(): array
37
    {
38 1
        return [];
39
    }
40
41
    /**
42
     * @return array|null
43
     */
44 4 View Code Duplication
    public function serialize()
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...
45
    {
46 4
        $serialized = [];
47 4
        foreach ($this->children as $child) {
48 3
            $this->serializeChild($serialized, $child);
49
        }
50
51 4
        if ([] === $serialized) {
52 2
            return;
53
        }
54
55 2
        return $serialized;
56
    }
57
58
    /**
59
     * @param array             $serialized
60
     * @param AbstractNode $child
61
     */
62 3
    private function serializeChild(array &$serialized, AbstractNode $child)
63
    {
64 3
        if (null !== $serializedChild = $child->serialize()) {
65 1
            $serialized[] = $serializedChild;
66 2
        } elseif ($child->isAllowSerializeEmpty()) {
67 1
            $serialized[] = $child->serializeEmpty();
68
        }
69 3
    }
70
}
71