Passed
Push — master ( f96d1f...c3fb18 )
by Thorsten
01:48
created

RelatedEntityList   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 76
Duplicated Lines 100 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 0%

Importance

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