Passed
Push — master ( 08575b...064303 )
by Jonas
03:09
created

DeepRelationsHook::run()   B

Complexity

Conditions 10
Paths 6

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 10.9841

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 10
eloc 14
c 3
b 0
f 0
nc 6
nop 2
dl 0
loc 24
ccs 11
cts 14
cp 0.7856
crap 10.9841
rs 7.6666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Staudenmeir\EloquentHasManyDeep\IdeHelper;
4
5
use Barryvdh\LaravelIdeHelper\Console\ModelsCommand;
6
use Barryvdh\LaravelIdeHelper\Contracts\ModelHookInterface;
7
use Illuminate\Database\Eloquent\Collection;
8
use Illuminate\Database\Eloquent\Model;
9
use Illuminate\Database\Eloquent\Relations\Relation;
10
use Illuminate\Support\Str;
11
use ReflectionClass;
12
use ReflectionMethod;
13
use Staudenmeir\EloquentHasManyDeep\HasManyDeep;
14
use Staudenmeir\EloquentHasManyDeep\HasOneDeep;
15
use Staudenmeir\EloquentHasManyDeep\HasRelationships;
16
use Throwable;
17
18
class DeepRelationsHook implements ModelHookInterface
19
{
20 2
    public function run(ModelsCommand $command, Model $model): void
21
    {
22 2
        $traits = class_uses_recursive($model);
23
24 2
        if (!in_array(HasRelationships::class, $traits)) {
25
            return;
26
        }
27
28 2
        $methods = (new ReflectionClass($model))->getMethods(ReflectionMethod::IS_PUBLIC);
29
30 2
        foreach ($methods as $method) {
31 2
            if ($method->isAbstract() || $method->isStatic() || !$method->isPublic()
32 2
                || $method->getNumberOfParameters() > 0 || $method->getDeclaringClass()->getName() === Model::class) {
33 2
                continue;
34
            }
35
36
            try {
37 2
                $relationship = $method->invoke($model);
38
            } catch (Throwable) {
39
                continue;
40
            }
41
42 2
            if ($relationship instanceof HasManyDeep) {
43 2
                $this->addRelationship($command, $method, $relationship);
44
            }
45
        }
46
    }
47
48 2
    protected function addRelationship(ModelsCommand $command, ReflectionMethod $method, Relation $relationship): void
49
    {
50 2
        $manyRelation = !$relationship instanceof HasOneDeep;
51
52 2
        $type = $manyRelation
53 2
            ? '\\' . Collection::class . '|\\' . $relationship->getRelated()::class . '[]'
54 2
            : '\\' . $relationship->getRelated()::class;
55
56 2
        $command->setProperty(
57 2
            $method->getName(),
58 2
            $type,
59 2
            true,
60 2
            false,
61 2
            '',
62 2
            !$manyRelation
63 2
        );
64
65 2
        if ($manyRelation) {
66 2
            $command->setProperty(
67 2
                Str::snake($method->getName()) . '_count',
68 2
                'int',
69 2
                true,
70 2
                false,
71 2
                null,
72 2
                true
73 2
            );
74
        }
75
    }
76
}
77