Passed
Push — master ( 340885...a41166 )
by Bruno
04:37 queued 01:25
created

CanDirective::processPolicyFieldDirective()   B

Complexity

Conditions 11
Paths 112

Size

Total Lines 61
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 37
CRAP Score 11.051

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 40
c 1
b 0
f 0
dl 0
loc 61
ccs 37
cts 40
cp 0.925
rs 7.2166
cc 11
nc 112
nop 3
crap 11.051

How to fix   Long Method    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 declare(strict_types=1);
2
3
namespace Modelarium\Laravel\Directives;
4
5
use GraphQL\Language\AST\DirectiveNode;
6
use GraphQL\Type\Definition\NonNull;
7
use Modelarium\Laravel\Targets\PolicyGenerator;
8
use Modelarium\Laravel\Targets\Interfaces\PolicyDirectiveInterface;
9
10
class CanDirective implements PolicyDirectiveInterface
11
{
12 1
    public static function processPolicyFieldDirective(
13
        PolicyGenerator $generator,
14
        \GraphQL\Type\Definition\FieldDefinition $field,
15
        \GraphQL\Language\AST\Node $directive
16
    ): void {
17 1
        $ability = '';
18 1
        $find = '';
19 1
        $injected = false;
20 1
        $args = false;
21
22 1
        if ($field->type instanceof NonNull) {
23
            $type = $field->type->getWrappedType();
24
        } else {
25 1
            $type = $field->type;
26
        }
27
28 1
        $model = $type->name; /** @phpstan-ignore-line */;
29
        
30 1
        foreach ($directive->arguments as $arg) {
31 1
            switch ($arg->name->value) {
32 1
                case 'ability':
33
                    // @phpstan-ignore-next-line
34 1
                    $ability = $arg->value->value;
35 1
                break;
36 1
                case 'find':
37
                    // @phpstan-ignore-next-line
38 1
                    $find = $arg->value->value;
39 1
                break;
40 1
                case 'model':
41
                    // @phpstan-ignore-next-line
42
                    $model = $arg->value->value;
43
                break;
44 1
                case 'injectArgs':
45 1
                    $injected = true;
46 1
                break;
47 1
                case 'args':
48 1
                    $args = true;
49 1
                break;
50
            }
51
        }
52
53 1
        list($namespace, $modelClassName, $relativePath) = $generator->splitClassName($model);
54
55 1
        $class = $generator->getClass($modelClassName);
56
57 1
        $method = $class->addMethod($ability);
58 1
        $method->setPublic()
59 1
            ->setReturnType('bool')
60 1
            ->addBody(
61 1
                'return false;'
62
            );
63 1
        $method->addParameter('user')->setType('\\App\\Models\\User')->setNullable(true);
64
65 1
        if ($find) {
66 1
            $method->addParameter('model')->setType('\\App\\Models\\' . $modelClassName);
67
        }
68 1
        if ($injected) {
69 1
            $method->addParameter('injectedArgs')->setType('array');
70
        }
71 1
        if ($args) {
72 1
            $method->addParameter('staticArgs')->setType('array');
73
        }
74 1
    }
75
}
76