Passed
Push — master ( 42feb8...1cd426 )
by Dominik
02:11
created

ArrayNode::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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