ArrayNode   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 82
Duplicated Lines 30.49 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 93.33%

Importance

Changes 0
Metric Value
wmc 11
lcom 2
cbo 2
dl 25
loc 82
ccs 28
cts 30
cp 0.9333
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 7 1
A __clone() 12 12 2
A add() 0 8 1
A serializeEmpty() 0 4 1
A serialize() 13 13 3
A serializeChild() 0 8 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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