Passed
Pull Request — master (#927)
by Aleksei
08:21
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 323
    public function __construct(
27
        private readonly ConfiguratorInterface $config,
28
        private readonly KernelInterface $kernel,
29
    ) {
30 323
    }
31
32 323
    public function init(ConsoleBootloader $console): void
33
    {
34 323
        $console->addCommand(Command\BootloaderCommand::class);
35 323
        $console->addCommand(Command\CommandCommand::class);
36 323
        $console->addCommand(Command\ConfigCommand::class);
37 323
        $console->addCommand(Command\ControllerCommand::class);
38 323
        $console->addCommand(Command\JobHandlerCommand::class);
39 323
        $console->addCommand(Command\MiddlewareCommand::class);
40 323
        $console->addCommand(Command\FilterCommand::class);
41
42
        try {
43 323
            $defaultNamespace = (new ReflectionClass($this->kernel))->getNamespaceName();
44
        } catch (ReflectionException) {
45
            $defaultNamespace = '';
46
        }
47
48 323
        $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 323
            'header' => [],
54
55
            /*
56
             * Base directory for generated classes, class will be automatically localed into sub directory
57
             * using given namespace.
58
             */
59 323
            '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 323
            'namespace' => $defaultNamespace,
68
69 323
            'declarations' => [],
70
71
            /*
72
             * This is set of default settings to be used for your scaffolding commands.
73
             */
74 323
            'defaults' => [
75 323
                'declarations' => [
76 323
                    Declaration\BootloaderDeclaration::TYPE => [
77 323
                        'namespace' => 'Bootloader',
78 323
                        'postfix' => 'Bootloader',
79 323
                        'class' => Declaration\BootloaderDeclaration::class,
80 323
                    ],
81 323
                    Declaration\ConfigDeclaration::TYPE => [
82 323
                        'namespace' => 'Config',
83 323
                        'postfix' => 'Config',
84 323
                        'class' => Declaration\ConfigDeclaration::class,
85 323
                        'options' => [
86 323
                            'directory' => directory('config'),
87 323
                        ],
88 323
                    ],
89 323
                    Declaration\ControllerDeclaration::TYPE => [
90 323
                        'namespace' => 'Controller',
91 323
                        'postfix' => 'Controller',
92 323
                        'class' => Declaration\ControllerDeclaration::class,
93 323
                    ],
94 323
                    Declaration\FilterDeclaration::TYPE => [
95 323
                        'namespace' => 'Filter',
96 323
                        'postfix' => 'Filter',
97 323
                        'class' => Declaration\FilterDeclaration::class,
98 323
                    ],
99 323
                    Declaration\MiddlewareDeclaration::TYPE => [
100 323
                        'namespace' => 'Middleware',
101 323
                        'postfix' => '',
102 323
                        'class' => Declaration\MiddlewareDeclaration::class,
103 323
                    ],
104 323
                    Declaration\CommandDeclaration::TYPE => [
105 323
                        'namespace' => 'Command',
106 323
                        'postfix' => 'Command',
107 323
                        'class' => Declaration\CommandDeclaration::class,
108 323
                    ],
109 323
                    Declaration\JobHandlerDeclaration::TYPE => [
110 323
                        'namespace' => 'Job',
111 323
                        'postfix' => 'Job',
112 323
                        'class' => Declaration\JobHandlerDeclaration::class,
113 323
                    ],
114 323
                ],
115 323
            ],
116 323
        ]);
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