Completed
Push — master ( bb13e5...2727f3 )
by Dominik
02:26
created

ArrayNode   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 68
Duplicated Lines 19.12 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 2
cbo 2
dl 13
loc 68
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 7 1
A add() 0 8 1
A serialize() 13 13 3
A serializeEmpty() 0 4 1
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
     * @return ArrayNode
12
     */
13 6
    public static function create(bool $allowSerializeEmpty = false): ArrayNode
14
    {
15 6
        $node = new self;
16 6
        $node->allowSerializeEmpty = $allowSerializeEmpty;
17
18 6
        return $node;
19
    }
20
21
    /**
22
     * @param AbstractNode $node
23
     *
24
     * @return $this
25
     *
26
     * @throws \InvalidArgumentException
27
     */
28 6
    public function add(AbstractNode $node)
29
    {
30 6
        $node->setParent($this);
31
32 6
        $this->children[] = $node;
33
34 6
        return $this;
35
    }
36
37
    /**
38
     * @return array
39
     */
40
    public function serializeEmpty(): array
41
    {
42
        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 4
            $this->serializeChild($serialized, $child);
53
        }
54
55 4
        if ([] === $serialized) {
56 1
            return;
57
        }
58
59 3
        return $serialized;
60
    }
61
62
    /**
63
     * @param array             $serialized
64
     * @param AbstractNode $child
65
     */
66 4
    private function serializeChild(array &$serialized, AbstractNode $child)
67
    {
68 4
        if (null !== $serializedChild = $child->serialize()) {
69 3
            $serialized[] = $serializedChild;
70 1
        } elseif ($child->isAllowSerializeEmpty()) {
71
            $serialized[] = $child->serializeEmpty();
72
        }
73 4
    }
74
}
75