Passed
Push — master ( 819f30...8ceff2 )
by Thorsten
02:22
created

NestedEntityList   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 75
Duplicated Lines 100 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 91.89%

Importance

Changes 0
Metric Value
wmc 16
lcom 2
cbo 3
dl 75
loc 75
ccs 34
cts 37
cp 0.9189
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A makeEmpty() 4 4 1
A wrap() 4 4 1
A fromNative() 4 4 1
A toNative() 6 6 1
B equals() 15 15 5
A __toString() 8 8 2
A diff() 18 18 4
A __construct() 4 4 1

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 Daikon\Entity\Entity;
4
5
use Daikon\DataStructure\TypedListTrait;
6
use Daikon\Entity\ValueObject\ValueObjectInterface;
7
use Daikon\Entity\ValueObject\ValueObjectListInterface;
8
9 View Code Duplication
final class NestedEntityList implements ValueObjectListInterface
0 ignored issues
show
Duplication introduced by
This class 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...
10
{
11
    use TypedListTrait;
12
13 4
    public static function makeEmpty(): self
14
    {
15 4
        return new self;
16
    }
17
18 26
    public static function wrap($entities): self
19
    {
20 26
        return new self($entities);
21
    }
22
23
    public static function fromNative($nativeValue): self
24
    {
25
        // @todo implement
26
    }
27
28
    public function toNative(): array
29
    {
30 5
        return $this->compositeVector->map(static function (ValueObjectInterface $entity): array {
31 5
            return $entity->toNative();
32 5
        })->toArray();
33
    }
34
35 2
    public function equals(ValueObjectInterface $otherList): bool
36
    {
37 2
        if (!$otherList instanceof self) {
38 1
            return false;
39
        }
40 1
        if (count($this) !== count($otherList)) {
41 1
            return false;
42
        }
43 1
        foreach ($this as $pos => $value) {
44 1
            if (!$value->equals($otherList->get($pos))) {
45 1
                return false;
46
            }
47
        }
48 1
        return true;
49
    }
50
51 1
    public function __toString(): string
52
    {
53 1
        $parts = [];
54 1
        foreach ($this as $nestedEntity) {
55 1
            $parts[] = (string)$nestedEntity;
56
        }
57 1
        return implode(",\n", $parts);
58
    }
59
60 1
    public function diff(ValueObjectListInterface $otherList): ValueObjectListInterface
61
    {
62 1
        $differentEntities = [];
63
        /* @var TypedEntityInterface $entity */
64 1
        foreach ($this as $pos => $entity) {
65 1
            if (!$otherList->has($pos)) {
66 1
                $differentEntities[] = $entity;
67 1
                continue;
68
            }
69
            /* @var TypedEntityInterface $otherEntity */
70 1
            $otherEntity = $otherList->get($pos);
71 1
            $diff = $entity->getValueObjectMap()->diff($otherEntity->getValueObjectMap());
72 1
            if (!$diff->isEmpty()) {
73
                $differentEntities[] = $entity;
74
            }
75
        }
76 1
        return new static($differentEntities);
77
    }
78
79 26
    private function __construct(array $entities = [])
80
    {
81 26
        $this->init($entities, TypedEntityInterface::class);
82 26
    }
83
}
84