Completed
Push — master ( 3c122c...a9556d )
by Dominik
02:23
created

ArrayNode::__clone()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 2

Importance

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