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

NestedEntityList::equals()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 15
Code Lines 9

Duplication

Lines 15
Ratio 100 %

Code Coverage

Tests 9
CRAP Score 5

Importance

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