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

ObjectNode::__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 ObjectNode extends AbstractParentNode implements ObjectNodeSerializeInterface
8
{
9
    /**
10
     * @param bool $allowSerializeEmpty
11
     * @return ObjectNode
12
     */
13 18
    public static function create(bool $allowSerializeEmpty = false): ObjectNode
14
    {
15 18
        $node = new self;
16 18
        $node->allowSerializeEmpty = $allowSerializeEmpty;
17
18 18
        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 $key => $node) {
30 1
            $this->add($key, clone $node);
31
        }
32 1
    }
33
34
    /**
35
     * @param string       $key
36
     * @param AbstractNode $node
37
     *
38
     * @return $this
39
     *
40
     * @throws \InvalidArgumentException
41
     */
42 18
    public function add(string $key, AbstractNode $node)
43
    {
44 18
        if (isset($this->children[$key])) {
45 1
            throw new \InvalidArgumentException(sprintf('There is already a node with key %s!', $key));
46
        }
47
48 18
        $node->setParent($this);
49
50 18
        $this->children[$key] = $node;
51
52 18
        return $this;
53
    }
54
55
    /**
56
     * @return \stdClass
57
     */
58 1
    public function serializeEmpty(): \stdClass
59
    {
60 1
        return new \stdClass();
61
    }
62
63
    /**
64
     * @return \stdClass|null
65
     */
66 16 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...
67
    {
68 16
        $serialized = new \stdClass();
69 16
        foreach ($this->children as $key => $child) {
70 16
            $this->serializeChild($serialized, $key, $child);
71
        }
72
73 16
        if ([] === (array) $serialized) {
74 3
            return;
75
        }
76
77 14
        return $serialized;
78
    }
79
80
    /**
81
     * @param \stdClass    $serialized
82
     * @param string       $key
83
     * @param AbstractNode $child
84
     */
85 16
    private function serializeChild(\stdClass $serialized, $key, AbstractNode $child)
86
    {
87 16
        if (null !== $serializedChild = $child->serialize()) {
88 14
            $serialized->$key = $serializedChild;
89 4
        } elseif ($child->isAllowSerializeEmpty()) {
90 2
            $serialized->$key = $child->serializeEmpty();
91
        }
92 16
    }
93
94
    /**
95
     * @param boolean $beautify
96
     * @return string
97
     */
98 16 View Code Duplication
    public function json(bool $beautify = false): string
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...
99
    {
100 16
        if (null === $serialized = $this->serialize()) {
101 2
            return '';
102
        }
103
104 14
        if ($beautify) {
105 1
            return json_encode($serialized, JSON_PRETTY_PRINT);
106
        }
107
108 13
        return json_encode($serialized);
109
    }
110
}
111