Passed
Push — master ( 77daa9...c77a82 )
by Jonas
03:36
created

RecursiveRelationsHook::run()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 30
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 30
ccs 0
cts 15
cp 0
rs 9.2568
cc 5
nc 6
nop 2
crap 30
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
        ],
25
        [
26
            'name' => 'ancestorsAndSelf',
27
            'manyRelation' => true,
28
            'comment' => 'The model\'s recursive parents and itself.'
29
        ],
30
        [
31
            'name' => 'bloodline',
32
            'manyRelation' => true,
33
            'comment' => 'The model\'s ancestors, descendants and itself.'
34
        ],
35
        [
36
            'name' => 'children',
37
            'manyRelation' => true,
38
            'comment' => 'The model\'s direct children.'
39
        ],
40
        [
41
            'name' => 'childrenAndSelf',
42
            'manyRelation' => true,
43
            'comment' => 'The model\'s direct children and itself.'
44
        ],
45
        [
46
            'name' => 'descendants',
47
            'manyRelation' => true,
48
            'comment' => 'The model\'s recursive children.'
49
        ],
50
        [
51
            'name' => 'descendantsAndSelf',
52
            'manyRelation' => true,
53
            'comment' => 'The model\'s recursive children and itself.'
54
        ],
55
        [
56
            'name' => 'parent',
57
            'manyRelation' => false,
58
            'comment' => 'The model\'s direct parent.'
59
        ],
60
        [
61
            'name' => 'parentAndSelf',
62
            'manyRelation' => true,
63
            'comment' => 'The model\'s direct parent and itself.'
64
        ],
65
        [
66
            'name' => 'rootAncestor',
67
            'manyRelation' => false,
68
            'comment' => 'The model\'s topmost parent.'
69
        ],
70
        [
71
            'name' => 'siblings',
72
            'manyRelation' => true,
73
            'comment' => 'The parent\'s other children.'
74
        ],
75
        [
76
            'name' => 'siblingsAndSelf',
77
            'manyRelation' => true,
78
            'comment' => 'All the parent\'s children.'
79
        ]
80
    ];
81
82
    public function run(ModelsCommand $command, Model $model): void
83
    {
84
        $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

84
        $traits = collect(/** @scrutinizer ignore-type */ class_uses_recursive($model));
Loading history...
85
86
        if ($traits->doesntContain(HasRecursiveRelationships::class)) {
87
            return;
88
        }
89
90
        foreach (self::$relationMap as $relationDefinition) {
91
            $type = $relationDefinition['manyRelation']
92
                ? '\\' . Collection::class . '|' . class_basename($model) . '[]'
93
                : class_basename($model);
94
95
            $command->setProperty(
96
                $relationDefinition['name'],
97
                $type,
98
                true,
99
                false,
100
                $relationDefinition['comment'],
101
                !$relationDefinition['manyRelation']
102
            );
103
104
            if ($relationDefinition['manyRelation']) {
105
                $command->setProperty(
106
                    $relationDefinition['name'] . '_count',
107
                    'int',
108
                    true,
109
                    false,
110
                    null,
111
                    true
112
                );
113
            }
114
        }
115
    }
116
}
117