Completed
Push — master ( 9dfc82...fbb6e1 )
by Dominik
02:13
created

ArrayNode   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 64
Duplicated Lines 20.31 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A add() 0 8 1
A getDefault() 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 $allowDefault
11
     */
12 7
    public function __construct(bool $allowDefault = false)
13
    {
14 7
        $this->allowDefault = $allowDefault;
15 7
    }
16
17
    /**
18
     * @param AbstractNode $node
19
     *
20
     * @return $this
21
     *
22
     * @throws \InvalidArgumentException
23
     */
24 4
    public function add(AbstractNode $node)
25
    {
26 4
        $node->setParent($this);
27
28 4
        $this->children[] = $node;
29
30 4
        return $this;
31
    }
32
33
    /**
34
     * @return array
35
     */
36 1
    public function getDefault(): array
37
    {
38 1
        return [];
39
    }
40
41
    /**
42
     * @return array|null
43
     */
44 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...
45
    {
46 4
        $serialized = [];
47 4
        foreach ($this->children as $child) {
48 3
            $this->serializeChild($serialized, $child);
49
        }
50
51 4
        if ([] === $serialized) {
52 2
            return;
53
        }
54
55 2
        return $serialized;
56
    }
57
58
    /**
59
     * @param array             $serialized
60
     * @param AbstractNode $child
61
     */
62 3
    private function serializeChild(array &$serialized, AbstractNode $child)
63
    {
64 3
        if (null !== $serializedChild = $child->serialize()) {
65 1
            $serialized[] = $serializedChild;
66 2
        } elseif ($child->isAllowDefault()) {
67 1
            $serialized[] = $child->getDefault();
68
        }
69 3
    }
70
}
71