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

RelatedEntityList::equals()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 16
Code Lines 9

Duplication

Lines 16
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 16
loc 16
ccs 0
cts 15
cp 0
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 9
nc 5
nop 1
crap 30
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