Passed
Push — master ( 2772d3...ae4d04 )
by Bruno
03:33
created

CanDirective::processPolicyFieldDirective()   B

Complexity

Conditions 10
Paths 56

Size

Total Lines 60
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 36
CRAP Score 10.0145

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 10
eloc 37
c 2
b 1
f 0
nc 56
nop 3
dl 0
loc 60
ccs 36
cts 38
cp 0.9474
crap 10.0145
rs 7.6666

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 Modelarium\Laravel\Targets\PolicyGenerator;
7
use Modelarium\Laravel\Targets\Interfaces\PolicyDirectiveInterface;
8
use Modelarium\Parser;
9
10
class CanDirective implements PolicyDirectiveInterface
11
{
12
    public static function processPolicyTypeDirective(
13
        PolicyGenerator $generator,
0 ignored issues
show
Unused Code introduced by
The parameter $generator is not used and could be removed. ( Ignorable by Annotation )

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

13
        /** @scrutinizer ignore-unused */ PolicyGenerator $generator,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
14
        \GraphQL\Language\AST\DirectiveNode $directive
0 ignored issues
show
Unused Code introduced by
The parameter $directive is not used and could be removed. ( Ignorable by Annotation )

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

14
        /** @scrutinizer ignore-unused */ \GraphQL\Language\AST\DirectiveNode $directive

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
15
    ): void {
16
    }
17
18 1
    public static function processPolicyFieldDirective(
19
        PolicyGenerator $generator,
20
        \GraphQL\Type\Definition\FieldDefinition $field,
21
        \GraphQL\Language\AST\DirectiveNode $directive
22
    ): void {
23 1
        $ability = '';
24 1
        $find = '';
25 1
        $injected = false;
26 1
        $args = false;
27
28 1
        list($type, $isRequired) = Parser::getUnwrappedType($field->type);
29
30
        /**
31
         * @var DirectiveNode $directive
32
         */
33 1
        $model = $type->name; /** @phpstan-ignore-line */
34
        
35 1
        foreach ($directive->arguments as $arg) {
36 1
            switch ($arg->name->value) {
37 1
                case 'ability':
38
                    // @phpstan-ignore-next-line
39 1
                    $ability = $arg->value->value;
0 ignored issues
show
Bug introduced by
Accessing value on the interface GraphQL\Language\AST\ValueNode suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
40 1
                break;
41 1
                case 'find':
42
                    // @phpstan-ignore-next-line
43 1
                    $find = $arg->value->value;
44 1
                break;
45 1
                case 'model':
46
                    // @phpstan-ignore-next-line
47
                    $model = $arg->value->value;
48
                break;
49 1
                case 'injectArgs':
50 1
                    $injected = true;
51 1
                break;
52 1
                case 'args':
53 1
                    $args = true;
54 1
                break;
55
            }
56
        }
57
58 1
        list($namespace, $modelClassName, $relativePath) = $generator->splitClassName($model);
59
60 1
        $class = $generator->getPolicyClass($modelClassName);
61
62 1
        $method = $class->addMethod($ability);
63 1
        $method->setPublic()
64 1
            ->setReturnType('bool')
65 1
            ->addBody(
66 1
                'return false;'
67
            );
68 1
        $method->addParameter('user')->setType('\\App\\Models\\User')->setNullable(true);
69
70 1
        if ($find) {
71 1
            $method->addParameter('model')->setType('\\App\\Models\\' . $modelClassName);
72
        }
73 1
        if ($injected) {
74 1
            $method->addParameter('injectedArgs')->setType('array');
75
        }
76 1
        if ($args) {
77 1
            $method->addParameter('staticArgs')->setType('array');
78
        }
79 1
    }
80
}
81