CanDirective   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Test Coverage

Coverage 90%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 38
dl 0
loc 68
rs 10
c 3
b 1
f 0
ccs 36
cts 40
cp 0.9
wmc 11

2 Methods

Rating   Name   Duplication   Size   Complexity  
A processPolicyTypeDirective() 0 4 1
B processPolicyFieldDirective() 0 60 10
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->getType());
29
30
        /**
31
         * @var DirectiveNode $directive
32
         */
33 1
        $modelName = $type->name;
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 = (string)$arg->value->value;
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
                    $modelName = $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($modelName);
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