Passed
Push — master ( cab6b5...845ff4 )
by Bruno
04:13 queued 01:09
created

CanDirective   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Test Coverage

Coverage 88.1%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 41
dl 0
loc 73
ccs 37
cts 42
cp 0.881
rs 10
c 1
b 0
f 0
wmc 12

2 Methods

Rating   Name   Duplication   Size   Complexity  
A processPolicyTypeDirective() 0 4 1
B processPolicyFieldDirective() 0 65 11
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
    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
        if ($field->type instanceof NonNull) {
29
            $type = $field->type->getWrappedType();
30
        } else {
31 1
            $type = $field->type;
32
        }
33
34
        /**
35
         * @var DirectiveNode $directive
36
         */
37
38 1
        $model = $type->name; /** @phpstan-ignore-line */
39
        
40 1
        foreach ($directive->arguments as $arg) {
41 1
            switch ($arg->name->value) {
42 1
                case 'ability':
43
                    // @phpstan-ignore-next-line
44 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...
45 1
                break;
46 1
                case 'find':
47
                    // @phpstan-ignore-next-line
48 1
                    $find = $arg->value->value;
49 1
                break;
50 1
                case 'model':
51
                    // @phpstan-ignore-next-line
52
                    $model = $arg->value->value;
53
                break;
54 1
                case 'injectArgs':
55 1
                    $injected = true;
56 1
                break;
57 1
                case 'args':
58 1
                    $args = true;
59 1
                break;
60
            }
61
        }
62
63 1
        list($namespace, $modelClassName, $relativePath) = $generator->splitClassName($model);
64
65 1
        $class = $generator->getPolicyClass($modelClassName);
66
67 1
        $method = $class->addMethod($ability);
68 1
        $method->setPublic()
69 1
            ->setReturnType('bool')
70 1
            ->addBody(
71 1
                'return false;'
72
            );
73 1
        $method->addParameter('user')->setType('\\App\\Models\\User')->setNullable(true);
74
75 1
        if ($find) {
76 1
            $method->addParameter('model')->setType('\\App\\Models\\' . $modelClassName);
77
        }
78 1
        if ($injected) {
79 1
            $method->addParameter('injectedArgs')->setType('array');
80
        }
81 1
        if ($args) {
82 1
            $method->addParameter('staticArgs')->setType('array');
83
        }
84 1
    }
85
}
86