Completed
Push — stable ( 86ecc3...3a45ea )
by Nuno
07:34
created

Kernel::commands()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 67
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 35
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 67
ccs 35
cts 35
cp 1
rs 8.5896
c 0
b 0
f 0
cc 5
eloc 30
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
/**
4
 * This file is part of Laravel Zero.
5
 *
6
 * (c) Nuno Maduro <[email protected]>
7
 *
8
 *  For the full copyright and license information, please view the LICENSE
9
 *  file that was distributed with this source code.
10
 */
11
12
namespace LaravelZero\Framework;
13
14
use ReflectionClass;
15
use Illuminate\Contracts\Events\Dispatcher;
16
use Illuminate\Console\Application as Artisan;
17
use Illuminate\Contracts\Foundation\Application;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, LaravelZero\Framework\Application.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
18
use Illuminate\Foundation\Console\Kernel as BaseKernel;
19
20
/**
21
 * This is the Laravel Zero Kernel implementation.
22
 */
23
class Kernel extends BaseKernel
24
{
25
    /**
26
     * The application's development commands.
27
     *
28
     * @var string[]
29
     */
30
    protected $developmentCommands = [
31
        Commands\App\Builder::class,
32
        Commands\App\Renamer::class,
33
        Commands\App\CommandMaker::class,
34
        Commands\App\Installer::class,
35
    ];
36
37
    /**
38
     * The application's bootstrap classes.
39
     *
40
     * @var string[]
41
     */
42
    protected $bootstrappers = [
43
        \LaravelZero\Framework\Bootstrap\CoreBindings::class,
44
        \LaravelZero\Framework\Bootstrap\LoadEnvironmentVariables::class,
45
        \LaravelZero\Framework\Bootstrap\LoadConfiguration::class,
46
        \Illuminate\Foundation\Bootstrap\HandleExceptions::class,
47
        \Illuminate\Foundation\Bootstrap\RegisterFacades::class,
48
        \LaravelZero\Framework\Bootstrap\RegisterProviders::class,
49
        \Illuminate\Foundation\Bootstrap\BootProviders::class,
50
    ];
51
52
    /**
53
     * Kernel constructor.
54
     *
55
     * @param \Illuminate\Contracts\Foundation\Application $app
56
     * @param \Illuminate\Contracts\Events\Dispatcher $events
57
     */
58 21
    public function __construct(
59
        \Illuminate\Contracts\Foundation\Application $app,
60
        \Illuminate\Contracts\Events\Dispatcher $events
61
    ) {
62 21
        if (! defined('ARTISAN_BINARY')) {
63
            define('ARTISAN_BINARY', basename($_SERVER['SCRIPT_FILENAME']));
64
        }
65
66 21
        parent::__construct($app, $events);
67 21
    }
68
69
    /**
70
     * Gets the application name.
71
     *
72
     * @return string
73
     */
74 1
    public function getName(): string
75
    {
76 1
        return $this->getArtisan()
77 1
            ->getName();
78
    }
79
80
    /**
81
     * Bootstrap the application for artisan commands.
82
     *
83
     * @return void
84
     */
85 21
    public function bootstrap()
86
    {
87 21
        parent::bootstrap();
88
89 21
        if ($commandClass = $this->app['config']->get('commands.default')) {
90 21
            $this->getArtisan()
91 21
                ->setDefaultCommand(
92 21
                    $this->app[$commandClass]->getName()
93
                );
94
        }
95 21
    }
96
97
    /**
98
     * Register the commands for the application.
99
     *
100
     * @return void
101
     */
102 21
    protected function commands(): void
103
    {
104 21
        $config = $this->app['config'];
105
106
        /*
107
         * Loads commands paths.
108
         */
109 21
        $this->load($config->get('commands.paths', $this->app->path('Commands')));
110
111
        /**
112
         * Loads configurated commands.
113
         */
114 21
        $commands = collect($config->get('commands.add', []))
115 21
            ->merge($config->get('commands.hidden', []));
116
117 21
        if ($command = $config->get('commands.default')) {
0 ignored issues
show
Unused Code introduced by
$command is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
118 21
            $commands->push($config->get('commands.default'));
119
        }
120
121
        /*
122
         * Loads development commands.
123
         */
124 21
        if ($this->app->environment() !== 'production') {
125 21
            $commands = $commands->merge($this->developmentCommands);
126
        }
127
128 21
        $commands = $commands->diff($toRemoveCommands = $config->get('commands.remove', []));
129
130 21
        Artisan::starting(function ($artisan) use ($toRemoveCommands) {
131 21
            $reflectionClass = new ReflectionClass(Artisan::class);
132 21
            $commands = collect($artisan->all())
133 21
                ->filter(
134 21
                    function ($command) use ($toRemoveCommands) {
135 21
                        return ! in_array(get_class($command), $toRemoveCommands);
136 21
                    }
137
                )
138 21
                ->toArray();
139
140 21
            $property = $reflectionClass->getParentClass()
141 21
                ->getProperty('commands');
142
143 21
            $property->setAccessible(true);
144 21
            $property->setValue($artisan, $commands);
145 21
            $property->setAccessible(false);
146 21
        });
147
148
        /*
149
         * Registers a bootstrap callback on the artisan console application
150
         * in order to call the schedule method on each Laravel Zero
151
         * command class.
152
         */
153 21
        Artisan::starting(function ($artisan) use ($commands) {
154 21
            $artisan->resolveCommands($commands->toArray());
155 21
        });
156
157
        Artisan::starting(function ($artisan) use ($config) {
158 21
            collect($artisan->all())->each(function ($command) use ($config) {
159 21
                if (in_array(get_class($command), $config->get('commands.hidden', []))) {
160 21
                    $command->setHidden(true);
161
                }
162
163 21
                if ($command instanceof Commands\Command) {
164 21
                    $this->app->call([$command, 'schedule']);
165 21
                }}
166
            );
167 21
        });
168 21
    }
169
}
170