RelationBlock   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 8
eloc 31
c 2
b 0
f 1
dl 0
loc 56
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getReplaceKeys() 0 15 5
A wrapItem() 0 7 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cycle\Schema\Renderer\PhpFileRenderer\Item;
6
7
use Cycle\ORM\Relation;
8
use Cycle\Schema\Renderer\PhpFileRenderer\Exporter\ArrayBlock;
9
use Cycle\Schema\Renderer\PhpFileRenderer\Exporter\ArrayItem;
10
11
class RelationBlock extends ArrayBlock
12
{
13
    /**
14
     * @see SchemaInterface
15
     */
16
    private const RELATION_SCHEMA_KEYS = [
17
        'MORPH_KEY',
18
        'CASCADE',
19
        'NULLABLE',
20
        'OUTER_KEY',
21
        'INNER_KEY',
22
        'INVERSION',
23
        'WHERE',
24
        'ORDER_BY',
25
        'THROUGH_INNER_KEY',
26
        'THROUGH_OUTER_KEY',
27
        'THROUGH_ENTITY',
28
        'THROUGH_WHERE',
29
        // Cycle ORM v1 deprecations:
30
        'THOUGH_INNER_KEY',
31
        'THOUGH_OUTER_KEY',
32
        'THOUGH_ENTITY',
33
        'THOUGH_WHERE',
34
    ];
35
36
    /**
37
     * @param int|string $key
38
     * @param mixed $value
39
     */
40
    protected function wrapItem($key, $value): ArrayItem
41
    {
42
        $item = parent::wrapItem($key, $value);
43
        if ($key === Relation::SCHEMA && is_array($value)) {
44
            $item->setValue(new self($value, $this->getReplaceKeys()), false);
45
        }
46
        return $item;
47
    }
48
49
    /**
50
     * @return array<int, string>
51
     */
52
    private function getReplaceKeys(): array
53
    {
54
        static $result = [];
55
        if ($result !== []) {
56
            return $result;
57
        }
58
        $constants = (new \ReflectionClass(Relation::class))->getConstants();
59
        foreach ($constants as $name => $value) {
60
            if (!in_array($name, self::RELATION_SCHEMA_KEYS, true) || array_key_exists($value, $result)) {
61
                continue;
62
            }
63
            $result[$value] = 'Relation::' . $name;
64
        }
65
66
        return $result;
67
    }
68
}
69