Test Failed
Pull Request — stable (#306)
by Nuno
01:48
created

Kernel::commands()   A

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 0
Metric Value
eloc 33
dl 0
loc 70
ccs 32
cts 32
cp 1
rs 9.0808
c 0
b 0
f 0
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 define;
17
use ReflectionClass;
18
use function collect;
19
use function defined;
20
use function in_array;
21
use function get_class;
22
use Illuminate\Console\Application as Artisan;
23
use Symfony\Component\Console\Output\OutputInterface;
24
use Illuminate\Foundation\Console\Kernel as BaseKernel;
25
use LaravelZero\Framework\Providers\CommandRecorder\CommandRecorderRepository;
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 34
    public function __construct(
60
        \Illuminate\Contracts\Foundation\Application $app,
61
        \Illuminate\Contracts\Events\Dispatcher $events
62
    ) {
63 34
        if (! defined('ARTISAN_BINARY')) {
64
            define('ARTISAN_BINARY', basename($_SERVER['SCRIPT_FILENAME']));
65
        }
66
67 34
        parent::__construct($app, $events);
68 34
    }
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 34
    public function bootstrap(): void
84
    {
85 34
        parent::bootstrap();
86
87 34
        if ($commandClass = $this->app['config']->get('commands.default')) {
88 34
            $this->getArtisan()
89 34
                ->setDefaultCommand(
90 34
                    $this->app[$commandClass]->getName()
91
                );
92
        }
93 34
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98 34
    protected function commands(): void
99
    {
100 34
        $config = $this->app['config'];
101
102
        /*
103
         * Loads commands paths.
104
         */
105 34
        $this->load($config->get('commands.paths', $this->app->path('Commands')));
106
107
        /**
108
         * Loads configurated commands.
109
         */
110 34
        $commands = collect($config->get('commands.add', []))->merge($config->get('commands.hidden', []));
111
112 34
        if ($command = $config->get('commands.default')) {
113 34
            $commands->push($command);
114
        }
115
116
        /*
117
         * Loads development commands.
118
         */
119 34
        if ($this->app->environment() !== 'production') {
120 34
            $commands = $commands->merge($this->developmentCommands);
121
        }
122
123 34
        $commands = $commands->diff($toRemoveCommands = $config->get('commands.remove', []));
124
125 34
        Artisan::starting(
126
            function ($artisan) use ($toRemoveCommands) {
127 34
                $reflectionClass = new ReflectionClass(Artisan::class);
128 34
                $commands = collect($artisan->all())
129 34
                    ->filter(
130
                        function ($command) use ($toRemoveCommands) {
131 34
                            return ! in_array(get_class($command), $toRemoveCommands, true);
132 34
                        }
133
                    )
134 34
                    ->toArray();
135
136 34
                $property = $reflectionClass->getParentClass()
137 34
                    ->getProperty('commands');
138
139 34
                $property->setAccessible(true);
140 34
                $property->setValue($artisan, $commands);
141 34
                $property->setAccessible(false);
142 34
            }
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 34
        Artisan::starting(
151
            function ($artisan) use ($commands) {
152 34
                $artisan->resolveCommands($commands->toArray());
153 34
            }
154
        );
155
156 34
        Artisan::starting(
157
            function ($artisan) use ($config) {
158 34
                collect($artisan->all())->each(
159
                    function ($command) use ($config, $artisan) {
160 34
                        if (in_array(get_class($command), $config->get('commands.hidden', []), true)) {
161 34
                            $command->setHidden(true);
162
                        }
163
164 34
                        $command->setApplication($artisan);
165
166 34
                        if ($command instanceof Commands\Command) {
167 34
                            $this->app->call([$command, 'schedule']);
168
                        }
169 34
                    }
170
                );
171 34
            }
172
        );
173 34
    }
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 18
    public function call($command, array $parameters = [], $outputBuffer = null)
188
    {
189 18
        resolve(CommandRecorderRepository::class)->create($command, $parameters);
190
191 18
        return parent::call($command, $parameters, $outputBuffer);
192
    }
193
}
194