Kernel::commands()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 70
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 32
CRAP Score 5

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 33
c 5
b 0
f 0
dl 0
loc 70
rs 9.0808
ccs 32
cts 32
cp 1
cc 5
nc 4
nop 0
crap 5

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
/**
6
 * This file is part of Laravel Zero.
7
 *
8
 * (c) Nuno Maduro <[email protected]>
9
 *
10
 *  For the full copyright and license information, please view the LICENSE
11
 *  file that was distributed with this source code.
12
 */
13
14
namespace LaravelZero\Framework;
15
16
use function collect;
17
use function define;
18
use function defined;
19
use function get_class;
20
use Illuminate\Console\Application as Artisan;
21
use Illuminate\Foundation\Console\Kernel as BaseKernel;
22
use function in_array;
23
use LaravelZero\Framework\Providers\CommandRecorder\CommandRecorderRepository;
24
use ReflectionClass;
25
use Symfony\Component\Console\Output\OutputInterface;
26
27
class Kernel extends BaseKernel
28
{
29
    /**
30
     * The application's development commands.
31
     *
32
     * @var string[]
33
     */
34
    protected $developmentCommands = [
35
        Commands\BuildCommand::class,
36
        Commands\RenameCommand::class,
37
        Commands\MakeCommand::class,
38
        Commands\InstallCommand::class,
39
    ];
40
41
    /**
42
     * The application's bootstrap classes.
43
     *
44
     * @var string[]
45
     */
46
    protected $bootstrappers = [
47
        \LaravelZero\Framework\Bootstrap\CoreBindings::class,
48
        \LaravelZero\Framework\Bootstrap\LoadEnvironmentVariables::class,
49
        \LaravelZero\Framework\Bootstrap\LoadConfiguration::class,
50
        \Illuminate\Foundation\Bootstrap\HandleExceptions::class,
51
        \LaravelZero\Framework\Bootstrap\RegisterFacades::class,
52
        \LaravelZero\Framework\Bootstrap\RegisterProviders::class,
53
        \Illuminate\Foundation\Bootstrap\BootProviders::class,
54
    ];
55
56
    /**
57
     * Kernel constructor.
58
     */
59 39
    public function __construct(
60
        \Illuminate\Contracts\Foundation\Application $app,
61
        \Illuminate\Contracts\Events\Dispatcher $events
62
    ) {
63 39
        if (! defined('ARTISAN_BINARY')) {
64
            define('ARTISAN_BINARY', basename($_SERVER['SCRIPT_FILENAME']));
65
        }
66
67 39
        parent::__construct($app, $events);
68 39
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73 1
    public function handle($input, $output = null)
74
    {
75 1
        $this->app->instance(OutputInterface::class, $output);
76
77 1
        return parent::handle($input, $output);
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83 39
    public function bootstrap(): void
84
    {
85 39
        parent::bootstrap();
86
87 39
        if ($commandClass = $this->app['config']->get('commands.default')) {
88 39
            $this->getArtisan()
89 39
                ->setDefaultCommand(
90 39
                    $this->app[$commandClass]->getName()
91
                );
92
        }
93 39
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98 39
    protected function commands(): void
99
    {
100 39
        $config = $this->app['config'];
101
102
        /*
103
         * Loads commands paths.
104
         */
105 39
        $this->load($config->get('commands.paths', $this->app->path('Commands')));
106
107
        /**
108
         * Loads configurated commands.
109
         */
110 39
        $commands = collect($config->get('commands.add', []))->merge($config->get('commands.hidden', []));
111
112 39
        if ($command = $config->get('commands.default')) {
113 39
            $commands->push($command);
114
        }
115
116
        /*
117
         * Loads development commands.
118
         */
119 39
        if ($this->app->environment() !== 'production') {
120 39
            $commands = $commands->merge($this->developmentCommands);
121
        }
122
123 39
        $commands = $commands->diff($toRemoveCommands = $config->get('commands.remove', []));
124
125 39
        Artisan::starting(
126
            function ($artisan) use ($toRemoveCommands) {
127 39
                $reflectionClass = new ReflectionClass(Artisan::class);
128 39
                $commands = collect($artisan->all())
129 39
                    ->filter(
130
                        function ($command) use ($toRemoveCommands) {
131 39
                            return ! in_array(get_class($command), $toRemoveCommands, true);
132 39
                        }
133
                    )
134 39
                    ->toArray();
135
136 39
                $property = $reflectionClass->getParentClass()
137 39
                    ->getProperty('commands');
138
139 39
                $property->setAccessible(true);
140 39
                $property->setValue($artisan, $commands);
141 39
                $property->setAccessible(false);
142 39
            }
143
        );
144
145
        /*
146
         * Registers a bootstrap callback on the artisan console application
147
         * in order to call the schedule method on each Laravel Zero
148
         * command class.
149
         */
150 39
        Artisan::starting(
151
            function ($artisan) use ($commands) {
152 39
                $artisan->resolveCommands($commands->toArray());
153 39
            }
154
        );
155
156 39
        Artisan::starting(
157
            function ($artisan) use ($config) {
158 39
                collect($artisan->all())->each(
159
                    function ($command) use ($config, $artisan) {
160 39
                        if (in_array(get_class($command), $config->get('commands.hidden', []), true)) {
161 39
                            $command->setHidden(true);
162
                        }
163
164 39
                        $command->setApplication($artisan);
165
166 39
                        if ($command instanceof Commands\Command) {
167 39
                            $this->app->call([$command, 'schedule']);
168
                        }
169 39
                    }
170
                );
171 39
            }
172
        );
173 39
    }
174
175
    /**
176
     * Gets the application name.
177
     */
178 1
    public function getName(): string
179
    {
180 1
        return $this->getArtisan()
181 1
            ->getName();
182
    }
183
184
    /**
185
     * {@inheritdoc}
186
     */
187 21
    public function call($command, array $parameters = [], $outputBuffer = null)
188
    {
189 21
        resolve(CommandRecorderRepository::class)->create($command, $parameters);
190
191 21
        return parent::call($command, $parameters, $outputBuffer);
192
    }
193
}
194