Completed
Push — stable ( ef4570...c41d70 )
by Nuno
03:23
created

Kernel::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
ccs 4
cts 5
cp 0.8
rs 9.4285
cc 2
eloc 6
nc 2
nop 2
crap 2.032
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\Console\Application as Artisan;
16
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...
17
use Symfony\Component\Console\Output\OutputInterface;
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 19
    public function __construct(
59
        \Illuminate\Contracts\Foundation\Application $app,
60
        \Illuminate\Contracts\Events\Dispatcher $events
61
    ) {
62 19
        if (! defined('ARTISAN_BINARY')) {
63
            define('ARTISAN_BINARY', basename($_SERVER['SCRIPT_FILENAME']));
64
        }
65
66 19
        parent::__construct($app, $events);
67 19
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function handle($input, $output = null)
73
    {
74
        $this->app->instance(OutputInterface::class, $output);
75
76
        return parent::handle($input, $output);
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82 19
    public function bootstrap()
83
    {
84 19
        parent::bootstrap();
85
86 19
        if ($commandClass = $this->app['config']->get('commands.default')) {
87 19
            $this->getArtisan()
88 19
                ->setDefaultCommand(
89 19
                    $this->app[$commandClass]->getName()
90
                );
91
        }
92 19
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97 19
    protected function commands(): void
98
    {
99 19
        $config = $this->app['config'];
100
101
        /*
102
         * Loads commands paths.
103
         */
104 19
        $this->load($config->get('commands.paths', $this->app->path('Commands')));
105
106
        /**
107
         * Loads configurated commands.
108
         */
109 19
        $commands = collect($config->get('commands.add', []))
110 19
            ->merge($config->get('commands.hidden', []));
111
112 19
        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...
113 19
            $commands->push($config->get('commands.default'));
114
        }
115
116
        /*
117
         * Loads development commands.
118
         */
119 19
        if ($this->app->environment() !== 'production') {
120 19
            $commands = $commands->merge($this->developmentCommands);
121
        }
122
123 19
        $commands = $commands->diff($toRemoveCommands = $config->get('commands.remove', []));
124
125
        Artisan::starting(function ($artisan) use ($toRemoveCommands) {
126 19
            $reflectionClass = new ReflectionClass(Artisan::class);
127 19
            $commands = collect($artisan->all())
128 19
                ->filter(
129
                    function ($command) use ($toRemoveCommands) {
130 19
                        return ! in_array(get_class($command), $toRemoveCommands);
131 19
                    }
132
                )
133 19
                ->toArray();
134
135 19
            $property = $reflectionClass->getParentClass()
136 19
                ->getProperty('commands');
137
138 19
            $property->setAccessible(true);
139 19
            $property->setValue($artisan, $commands);
140 19
            $property->setAccessible(false);
141 19
        });
142
143
        /*
144
         * Registers a bootstrap callback on the artisan console application
145
         * in order to call the schedule method on each Laravel Zero
146
         * command class.
147
         */
148
        Artisan::starting(function ($artisan) use ($commands) {
149 19
            $artisan->resolveCommands($commands->toArray());
150 19
        });
151
152
        Artisan::starting(function ($artisan) use ($config) {
153
            collect($artisan->all())->each(function ($command) use ($config) {
154 19
                if (in_array(get_class($command), $config->get('commands.hidden', []))) {
155 19
                    $command->setHidden(true);
156
                }
157
158 19
                if ($command instanceof Commands\Command) {
159 19
                    $this->app->call([$command, 'schedule']);
160
                }
161 19
            }
162
            );
163 19
        });
164 19
    }
165
166
    /**
167
     * Gets the application name.
168
     *
169
     * @return string
170
     */
171 1
    public function getName(): string
172
    {
173 1
        return $this->getArtisan()
174 1
            ->getName();
175
    }
176
}
177