ClasslessProxyTrait::__debugInfo()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
ccs 0
cts 3
cp 0
crap 2
rs 10
c 1
b 0
f 0
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