Passed
Push — master ( 0dc38a...c6b107 )
by Jonas
09:59 queued 06:57
created

JsonRelationsHook::run()   B

Complexity

Conditions 11
Paths 6

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 12.1908

Importance

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

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\EloquentJsonRelations\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\EloquentJsonRelations\HasJsonRelationships;
14
use Staudenmeir\EloquentJsonRelations\Relations\BelongsToJson;
15
use Staudenmeir\EloquentJsonRelations\Relations\HasManyJson;
16
use Throwable;
17
18
class JsonRelationsHook implements ModelHookInterface
19
{
20 9
    public function run(ModelsCommand $command, Model $model): void
21
    {
22 9
        $traits = class_uses_recursive($model);
23
24 9
        if (!in_array(HasJsonRelationships::class, $traits)) {
25
            return;
26
        }
27
28 9
        $methods = (new ReflectionClass($model))->getMethods(ReflectionMethod::IS_PUBLIC);
29
30 9
        foreach ($methods as $method) {
31 9
            if ($method->isAbstract() || $method->isStatic() || !$method->isPublic()
32 9
                || $method->getNumberOfParameters() > 0 || $method->getDeclaringClass()->getName() === Model::class) {
33 9
                continue;
34
            }
35
36
            try {
37 9
                $relationship = $method->invoke($model);
38
            } catch (Throwable) {
39
                continue;
40
            }
41
42 9
            if ($relationship instanceof BelongsToJson || $relationship instanceof HasManyJson) {
43 9
                $this->addRelationship($command, $method, $relationship);
44
            }
45
        }
46
    }
47
48 9
    protected function addRelationship(ModelsCommand $command, ReflectionMethod $method, Relation $relationship): void
49
    {
50 9
        $type = '\\' . Collection::class . '|\\' . $relationship->getRelated()::class . '[]';
51
52 9
        $command->setProperty(
53 9
            $method->getName(),
54 9
            $type,
55 9
            true,
56 9
            false
57 9
        );
58
59 9
        $command->setProperty(
60 9
            Str::snake($method->getName()) . '_count',
61 9
            'int',
62 9
            true,
63 9
            false,
64 9
            null,
65 9
            true
66 9
        );
67
    }
68
}
69