ArrayNode::add()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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