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

Entity::fromArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Daikon\Entity\Entity;
4
5
use Daikon\Entity\Assert\Assertion;
6
use Daikon\Entity\Entity\Path\ValuePathParser;
7
use Daikon\Entity\EntityType\EntityTypeInterface;
8
use Daikon\Entity\Error\UnknownAttribute;
9
use Daikon\Entity\ValueObject\Nil;
10
use Daikon\Entity\ValueObject\ValueObjectInterface;
11
12
abstract class Entity implements TypedEntityInterface
13
{
14
    /**
15
     * @var EntityTypeInterface
16
     */
17
    private $type;
18
19
    /**
20
     * @var EntityInterface
21
     */
22
    private $parent;
23
24
    /**
25
     * @var ValueObjectMap
26
     */
27
    private $valueObjectMap;
28
29
    /**
30
     * @param ValuePathParser
31
     */
32
    private $pathParser;
33
34 32
    public static function fromNative($nativeState): EntityInterface
35
    {
36 32
        $entityType = $nativeState["@type"];
37 32
        Assertion::isInstanceOf($entityType, EntityTypeInterface::class);
38 32
        $parent = null;
39 32
        if (isset($nativeState["@parent"])) {
40 25
            $parent = $nativeState["@parent"];
41 25
            Assertion::isInstanceOf($parent, TypedEntityInterface::class);
42 25
            unset($nativeState["@parent"]);
43
        }
44 32
        return new static($entityType, $nativeState, $parent);
45
    }
46
47 7
    public function toNative(): array
48
    {
49 7
        $entityState = $this->valueObjectMap->toArray();
50 7
        $entityState[self::ENTITY_TYPE] = $this->getEntityType()->getName();
51 7
        return $entityState;
52
    }
53
54 1
    public function isSameAs(EntityInterface $entity): bool
55
    {
56 1
        Assertion::isInstanceOf($entity, static::class);
57 1
        return $this->getIdentity()->equals($entity->getIdentity());
58
    }
59
60 13
    public function withValue(string $attributeName, $value): EntityInterface
61
    {
62 13
        $copy = clone $this;
63 13
        $copy->valueObjectMap = $this->valueObjectMap->withValue($attributeName, $value);
64 13
        return $copy;
65
    }
66
67 1
    public function withValues(array $values): EntityInterface
68
    {
69 1
        $copy = clone $this;
70 1
        $copy->valueObjectMap = $this->valueObjectMap->withValues($values);
71 1
        return $copy;
72
    }
73
74 5
    public function getValueObjectMap(): ValueObjectMap
75
    {
76 5
        return $this->valueObjectMap;
77
    }
78
79 2
    public function has(string $attributeName): bool
80
    {
81 2
        if (!$this->valueObjectMap->has($attributeName)) {
82 1
            throw new UnknownAttribute("Attribute '$attributeName' is not known to the entity's value-map. ");
83
        }
84 1
        return !$this->valueObjectMap->get($attributeName) instanceof Nil;
85
    }
86
87 12
    public function get(string $valuePath): ValueObjectInterface
88
    {
89 12
        if (mb_strpos($valuePath, ".")) {
90 3
            return $this->evaluatePath($valuePath);
91
        }
92 12
        if (!$this->valueObjectMap->has($valuePath)) {
93 1
            throw new UnknownAttribute("Attribute '$valuePath' is unknown by type ".$this->getEntityType()->getName());
94
        }
95 11
        return $this->valueObjectMap->get($valuePath);
96
    }
97
98 1
    public function getEntityRoot(): TypedEntityInterface
99
    {
100 1
        $tmpParent = $this->getEntityParent();
101 1
        $root = $tmpParent;
102 1
        while ($tmpParent) {
103 1
            $root = $tmpParent;
104 1
            $tmpParent = $tmpParent->getEntityParent();
105
        }
106 1
        return $root ?? $this;
107
    }
108
109 2
    public function getEntityParent(): ?TypedEntityInterface
110
    {
111 2
        return $this->parent;
112
    }
113
114 32
    public function getEntityType(): EntityTypeInterface
115
    {
116 32
        return $this->type;
117
    }
118
119 32
    protected function __construct(EntityTypeInterface $type, array $values = [], TypedEntityInterface $parent = null)
120
    {
121 32
        $this->type = $type;
122 32
        $this->parent = $parent;
123 32
        $this->valueObjectMap = ValueObjectMap::forEntity($this, $values);
124 32
        $this->pathParser = ValuePathParser::create();
125 32
    }
126
127 3
    private function evaluatePath($valuePath): ValueObjectInterface
128
    {
129 3
        $value = null;
130 3
        $entity = $this;
131 3
        foreach ($this->pathParser->parse($valuePath) as $pathPart) {
132
            /* @var TypedEntityInterface $value */
133 3
            $value = $entity->get($pathPart->getAttributeName());
0 ignored issues
show
Bug introduced by
The method get does only exist in Daikon\Entity\Entity\Entity, but not in Daikon\Entity\ValueObject\ValueObjectInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
134 2
            if ($pathPart->hasPosition()) {
135 2
                $entity = $value->get($pathPart->getPosition());
136 2
                $value = $entity;
137
            }
138
        }
139 2
        return $value;
140
    }
141
}
142