Passed
Push — master ( 865ea9...f5a1ef )
by butschster
29:08 queued 21:20
created

ScaffolderBootloader::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 81
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 53
CRAP Score 2.0001

Importance

Changes 0
Metric Value
eloc 48
dl 0
loc 81
ccs 53
cts 55
cp 0.9636
rs 9.1344
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2.0001

How to fix   Long Method   

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
2
3
declare(strict_types=1);
4
5
namespace Spiral\Scaffolder\Bootloader;
6
7
use Cocur\Slugify\Slugify;
8
use Cocur\Slugify\SlugifyInterface;
9
use ReflectionClass;
10
use ReflectionException;
11
use Spiral\Boot\Bootloader\Bootloader;
12
use Spiral\Boot\KernelInterface;
13
use Spiral\Config\ConfiguratorInterface;
14
use Spiral\Config\Patch\Append;
15
use Spiral\Console\Bootloader\ConsoleBootloader;
16
use Spiral\Scaffolder\Command;
17
use Spiral\Scaffolder\Config\ScaffolderConfig;
18
use Spiral\Scaffolder\Declaration;
19
20
class ScaffolderBootloader extends Bootloader
21
{
22
    protected const BINDINGS = [
23
        SlugifyInterface::class => Slugify::class,
24
    ];
25
26 337
    public function __construct(
27
        private readonly ConfiguratorInterface $config,
28
        private readonly KernelInterface $kernel,
29
    ) {
30 337
    }
31
32 337
    public function init(ConsoleBootloader $console): void
33
    {
34 337
        $console->addCommand(Command\BootloaderCommand::class);
35 337
        $console->addCommand(Command\CommandCommand::class);
36 337
        $console->addCommand(Command\ConfigCommand::class);
37 337
        $console->addCommand(Command\ControllerCommand::class);
38 337
        $console->addCommand(Command\JobHandlerCommand::class);
39 337
        $console->addCommand(Command\MiddlewareCommand::class);
40 337
        $console->addCommand(Command\FilterCommand::class);
41
42
        try {
43 337
            $defaultNamespace = (new ReflectionClass($this->kernel))->getNamespaceName();
44
        } catch (ReflectionException) {
45
            $defaultNamespace = '';
46
        }
47
48 337
        $this->config->setDefaults(ScaffolderConfig::CONFIG, [
49
            /*
50
             * This is set of comment lines to be applied to every scaffolded file, you can use env() function
51
             * to make it developer specific or set one universal pattern per project.
52
             */
53 337
            'header' => [],
54
55
            /*
56
             * Base directory for generated classes, class will be automatically localed into sub directory
57
             * using given namespace.
58
             */
59 337
            'directory' => directory('app') . 'src/',
60
61
            /*
62
             * Default namespace to be applied for every generated class. By default uses Kernel namespace
63
             *
64
             * Example: 'namespace' => 'MyApplication'
65
             * Controllers: MyApplication\Controllers\SampleController
66
             */
67 337
            'namespace' => $defaultNamespace,
68
69 337
            'declarations' => [],
70
71
            /*
72
             * This is set of default settings to be used for your scaffolding commands.
73
             */
74 337
            'defaults' => [
75 337
                'declarations' => [
76 337
                    Declaration\BootloaderDeclaration::TYPE => [
77 337
                        'namespace' => 'Bootloader',
78 337
                        'postfix' => 'Bootloader',
79 337
                        'class' => Declaration\BootloaderDeclaration::class,
80 337
                    ],
81 337
                    Declaration\ConfigDeclaration::TYPE => [
82 337
                        'namespace' => 'Config',
83 337
                        'postfix' => 'Config',
84 337
                        'class' => Declaration\ConfigDeclaration::class,
85 337
                        'options' => [
86 337
                            'directory' => directory('config'),
87 337
                        ],
88 337
                    ],
89 337
                    Declaration\ControllerDeclaration::TYPE => [
90 337
                        'namespace' => 'Controller',
91 337
                        'postfix' => 'Controller',
92 337
                        'class' => Declaration\ControllerDeclaration::class,
93 337
                    ],
94 337
                    Declaration\FilterDeclaration::TYPE => [
95 337
                        'namespace' => 'Filter',
96 337
                        'postfix' => 'Filter',
97 337
                        'class' => Declaration\FilterDeclaration::class,
98 337
                    ],
99 337
                    Declaration\MiddlewareDeclaration::TYPE => [
100 337
                        'namespace' => 'Middleware',
101 337
                        'postfix' => '',
102 337
                        'class' => Declaration\MiddlewareDeclaration::class,
103 337
                    ],
104 337
                    Declaration\CommandDeclaration::TYPE => [
105 337
                        'namespace' => 'Command',
106 337
                        'postfix' => 'Command',
107 337
                        'class' => Declaration\CommandDeclaration::class,
108 337
                    ],
109 337
                    Declaration\JobHandlerDeclaration::TYPE => [
110 337
                        'namespace' => 'Job',
111 337
                        'postfix' => 'Job',
112 337
                        'class' => Declaration\JobHandlerDeclaration::class,
113 337
                    ],
114 337
                ],
115 337
            ],
116 337
        ]);
117
    }
118
119
    /**
120
     * Register new Scaffolder declaration.
121
     *
122
     * @param non-empty-string $name
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
123
     */
124 3
    public function addDeclaration(string $name, array $declaration): void
125
    {
126 3
        $this->config->modify(
127 3
            ScaffolderConfig::CONFIG,
128 3
            new Append('defaults.declarations', $name, $declaration),
129 3
        );
130
    }
131
}
132