Completed
Branch master (a93247)
by Dominik
04:14 queued 01:56
created

ObjectNode   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 43
Duplicated Lines 11.63 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 6
Bugs 1 Features 2
Metric Value
wmc 8
c 6
b 1
f 2
lcom 1
cbo 1
dl 5
loc 43
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 16 3
B serialize() 5 15 5

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
namespace Saxulum\ElasticSearchQueryBuilder\Node;
4
5
class ObjectNode extends AbstractParentNode
6
{
7
    /**
8
     * @param string $name
9
     * @param AbstractNode $node
10
     * @return $this
11
     */
12
    public function add($name, AbstractNode $node)
13
    {
14
        if (null !== $this->reflectionProperty->getValue($node)) {
15
            throw new \InvalidArgumentException('Node already got a parent!');
16
        }
17
18
        if (array_key_exists($name, $this->children)) {
19
            throw new \InvalidArgumentException(sprintf('There is allready a child for name %s', $name));
20
        }
21
22
        $this->reflectionProperty->setValue($node, $this);
23
        
24
        $this->children[$name] = $node;
25
26
        return $this;
27
    }
28
29
    /**
30
     * @return \stdClass|null
31
     */
32
    public function serialize()
33
    {
34
        $serialized = new \stdClass();
35 View Code Duplication
        foreach ($this->children as $name => $child) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
36
            if (null !== $serializedChild = $child->serialize()) {
37
                $serialized->$name = $serializedChild;
38
            }
39
        }
40
41
        if (!$this->allowEmpty && [] === (array) $serialized) {
42
            return null;
43
        }
44
45
        return $serialized;
46
    }
47
}
48