Passed
Pull Request — master (#131)
by
unknown
12:02
created

RecursiveRelationsHook::run()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 30
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 30
rs 9.2568
cc 5
nc 6
nop 2
1
<?php
2
3
namespace Staudenmeir\LaravelAdjacencyList;
4
5
use Barryvdh\LaravelIdeHelper\Console\ModelsCommand;
6
use Barryvdh\LaravelIdeHelper\Contracts\ModelHookInterface;
7
use Illuminate\Database\Eloquent\Model;
8
use Staudenmeir\LaravelAdjacencyList\Eloquent\Collection;
9
use Staudenmeir\LaravelAdjacencyList\Eloquent\HasRecursiveRelationships;
10
11
/**
12
 * @template RT { name: string, manyRelation: boolean, comment: string, relationClass: class-string|null }
13
 */
14
class RecursiveRelationsHook implements ModelHookInterface
15
{
16
    /**
17
     * @var array<array<RT>>
18
     */
19
    private static array $relationMap = [
20
        [
21
            'name' => 'ancestors',
22
            'manyRelation' => true,
23
            'comment' => 'The model\'s recursive parents.',
24
            'relationClass' => Collection::class
25
        ],
26
        [
27
            'name' => 'ancestorsAndSelf',
28
            'manyRelation' => true,
29
            'comment' => 'The model\'s recursive parents and itself.',
30
            'relationClass' => Collection::class
31
        ],
32
        [
33
            'name' => 'bloodline',
34
            'manyRelation' => true,
35
            'comment' => 'The model\'s ancestors, descendants and itself.',
36
            'relationClass' => Collection::class
37
        ],
38
        [
39
            'name' => 'children',
40
            'manyRelation' => true,
41
            'comment' => 'The model\'s direct children.',
42
            'relationClass' => Collection::class
43
        ],
44
        [
45
            'name' => 'childrenAndSelf',
46
            'manyRelation' => true,
47
            'comment' => 'The model\'s direct children and itself.',
48
            'relationClass' => Collection::class
49
        ],
50
        [
51
            'name' => 'descendants',
52
            'manyRelation' => true,
53
            'comment' => 'The model\'s recursive children.',
54
            'relationClass' => Collection::class
55
        ],
56
        [
57
            'name' => 'descendantsAndSelf',
58
            'manyRelation' => true,
59
            'comment' => 'The model\'s recursive children and itself.',
60
            'relationClass' => Collection::class
61
        ],
62
        [
63
            'name' => 'parent',
64
            'manyRelation' => false,
65
            'comment' => 'The model\'s direct parent.',
66
            'relationClass' => null
67
        ],
68
        [
69
            'name' => 'parentAndSelf',
70
            'manyRelation' => true,
71
            'comment' => 'The model\'s direct parent and itself.',
72
            'relationClass' => Collection::class
73
        ],
74
        [
75
            'name' => 'rootAncestor',
76
            'manyRelation' => false,
77
            'comment' => 'The model\'s topmost parent.',
78
            'relationClass' => null
79
        ],
80
        [
81
            'name' => 'siblings',
82
            'manyRelation' => true,
83
            'comment' => 'The parent\'s other children.',
84
            'relationClass' => Collection::class
85
        ],
86
        [
87
            'name' => 'siblingsAndSelf',
88
            'manyRelation' => true,
89
            'comment' => 'All the parent\'s children.',
90
            'relationClass' => Collection::class
91
        ]
92
    ];
93
94
    public function run(ModelsCommand $command, Model $model): void
95
    {
96
        $traits = collect(class_uses_recursive($model));
0 ignored issues
show
Bug introduced by
class_uses_recursive($model) of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

96
        $traits = collect(/** @scrutinizer ignore-type */ class_uses_recursive($model));
Loading history...
97
98
        if ($traits->doesntContain(HasRecursiveRelationships::class)) {
99
            return;
100
        }
101
102
        foreach (self::$relationMap as $relationDefinition) {
103
            $type = $relationDefinition['manyRelation']
104
                ? '\\' . $relationDefinition['relationClass'] . '|' . class_basename($model) . '[]'
105
                : class_basename($model);
106
107
            $command->setProperty(
108
                $relationDefinition['name'],
109
                $type,
110
                true,
111
                false,
112
                $relationDefinition['comment'],
113
                !$relationDefinition['manyRelation']
114
            );
115
116
            if ($relationDefinition['manyRelation']) {
117
                $command->setProperty(
118
                    $relationDefinition['name'] . '_count',
119
                    'int',
120
                    true,
121
                    false,
122
                    null,
123
                    true
124
                );
125
            }
126
        }
127
    }
128
}
129