ClasslessProxyTrait   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 59.09%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
dl 0
loc 41
ccs 13
cts 22
cp 0.5909
rs 10
c 1
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __get() 0 13 3
A __set() 0 14 3
A __debugInfo() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cycle\ORM\Mapper\Proxy;
6
7
use Cycle\ORM\Reference\ReferenceInterface;
8
use Cycle\ORM\RelationMap;
9
10
/**
11
 * @internal
12
 */
13
trait ClasslessProxyTrait
14
{
15
    public RelationMap $__cycle_orm_rel_map;
16
    public array $__cycle_orm_rel_data = [];
17
18
    public function __get(string $name)
19 16
    {
20
        $relation = $this->__cycle_orm_rel_map->getRelations()[$name] ?? null;
21 16
        if ($relation === null) {
22 16
            throw new \RuntimeException(\sprintf('Undefined property %s.%s.', static::class, $name));
23
        }
24
        $value = $this->__cycle_orm_rel_data[$name] ?? null;
25 16
        if ($value instanceof ReferenceInterface) {
26 16
            $this->$name = $relation->collect($relation->resolve($value, true));
27
            unset($this->__cycle_orm_rel_data[$name]);
28
            return $this->$name;
29
        }
30
        return $value ?? $this->$name;
31 16
    }
32
33
    public function __set(string $name, mixed $value): void
34 56
    {
35
        if (!\array_key_exists($name, $this->__cycle_orm_rel_map->getRelations())) {
36 56
            $this->$name = $value;
37
            return;
38
        }
39
40
        if ($value instanceof ReferenceInterface) {
41 56
            $this->__cycle_orm_rel_data[$name] = $value;
42 56
            return;
43 56
        }
44
        unset($this->__cycle_orm_rel_data[$name]);
45 56
46
        $this->$name = $value;
47 56
    }
48
49
    public function __debugInfo(): array
50
    {
51
        $result = (array) $this;
52
        unset($result['__cycle_orm_rel_map'], $result['__cycle_orm_rel_data'], $result['__cycle_orm_relation_props']);
53
        return $result;
54
    }
55
}
56