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

PolicyGenerator::processCan()   B

Complexity

Conditions 11
Paths 112

Size

Total Lines 60
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 60
ccs 37
cts 40
cp 0.925
rs 7.2166
cc 11
nc 112
nop 2
crap 11.051

1 Method

Rating   Name   Duplication   Size   Complexity  
A PolicyGenerator::getGenerateFilename() 0 3 1

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\Targets;
4
5
use GraphQL\Type\Definition\ObjectType;
6
use GraphQL\Type\Definition\Type;
7
use Illuminate\Support\Str;
8
use Modelarium\BaseGenerator;
9
use Modelarium\GeneratedCollection;
10
use Modelarium\GeneratedItem;
11
use Modelarium\Parser;
12
use Nette\PhpGenerator\ClassType;
13
use Nette\PhpGenerator\PhpNamespace;
14
15
class PolicyGenerator extends BaseGenerator
16
{
17
    /**
18
     * @var string
19
     */
20
    protected $stubDir = __DIR__ . "/stubs/";
21
22
    /**
23
     * @var ObjectType
24
     */
25
    protected $type = null;
26
27
    /**
28
     * @var GeneratedCollection
29
     */
30
    protected $collection = null;
31
32
    /**
33
     *
34
     * @var ClassType[]
35
     */
36
    protected $policyClasses = [];
37
38
    /**
39
     * @param Parser $parser
40
     * @param string $name
41
     * @param Type|string $type
42
     * @phpstan-ignore-next-line
43
     */
44 2
    public function __construct(Parser $parser, string $name, $type = null)
45
    {
46 2
        parent::__construct($parser, '', $type);
47 2
        $this->collection = new GeneratedCollection();
48 2
    }
49
50 2
    public function generate(): GeneratedCollection
51
    {
52 2
        foreach ($this->type->getFields() as $field) {
53 1
            $directives = $field->astNode->directives;
54 1
            $this->processDirectives($field, $directives);
55
        }
56
57
58 2
        $printer = new \Nette\PhpGenerator\PsrPrinter;
59
60 2
        foreach ($this->policyClasses as $name => $c) {
61 1
            $namespace = new PhpNamespace('App\\Policies');
62 1
            $namespace->addUse('App\\Models\\User');
63 1
            $namespace->addUse('App\\Models\\' . $name);
64 1
            $namespace->add($c);
65
66 1
            $this->collection->push(
67 1
                new GeneratedItem(
68 1
                    GeneratedItem::TYPE_POLICY,
69 1
                    "<?php declare(strict_types=1);\n\n" . $printer->printNamespace($namespace),
70 1
                    $this->getGenerateFilename($name)
71
                )
72
            );
73
        }
74 2
        return $this->collection;
75
    }
76
77 1
    public function processDirectives(
78
        \GraphQL\Type\Definition\FieldDefinition $field,
79
        \GraphQL\Language\AST\NodeList $directives
80
    ): void {
81 1
        foreach ($directives as $directive) {
82 1
            $name = $directive->name->value;
83 1
            $className = $this->getDirectiveClass($name);
84 1
            if ($className) {
85 1
                $methodName = "$className::processPolicyFieldDirective";
86 1
                $methodName(
87 1
                    $this,
88
                    $field,
89
                    $directive
90
                );
91
            }
92
        }
93 1
    }
94
95 1
    public function getClass(string $name): ClassType
96
    {
97 1
        if (array_key_exists($name, $this->policyClasses)) {
98 1
            return $this->policyClasses[$name];
99
        }
100
101
        /**
102
         * @var ClassType $class
103
         */
104 1
        $class = new ClassType($name . 'Policy');
105
        $class
106 1
            ->addComment("This file was automatically generated by Modelarium.")
107 1
            ->setTraits(['Illuminate\Auth\Access\HandlesAuthorization']);
108 1
        $this->policyClasses[$name] = $class;
109 1
        return $class;
110
    }
111
112 1
    public function getGenerateFilename(string $name): string
113
    {
114 1
        return $this->getBasePath('app/Policies/'. Str::studly($name) . 'Policy.php');
115
    }
116
}
117