Completed
Push — master ( 4deee6...b383ba )
by Zach
05:43 queued 03:44
created

ConsoleKernel::getCommands()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 10
rs 9.4285
1
<?php
2
3
namespace Yarak\Console;
4
5
use Yarak\Exceptions\FileNotFound;
6
use Yarak\Exceptions\InvalidCommand;
7
8
class ConsoleKernel
9
{
10
    /**
11
     * User defined commands.
12
     *
13
     * @var array
14
     */
15
    protected $commands = [];
16
17
    /**
18
     * Get all user defined commands.
19
     *
20
     * @return array
21
     */
22
    public function getCommands()
23
    {
24
        if (property_exists($this, 'commands')) {
25
            return array_map(function ($command) {
26
                $this->verifyCommand($command);
27
28
                return $command;
29
            }, $this->commands);
30
        }
31
    }
32
33
    /**
34
     * Verify user defined commands.
35
     *
36
     * @param string $command
37
     *
38
     * @throws FileNotFound|InvalidCommand
39
     */
40
    protected function verifyCommand($command)
41
    {
42
        if (!class_exists($command)) {
43
            throw FileNotFound::commandNotFound($command);
44
        }
45
46
        $class = new $command();
47
48
        if (!is_a($class, Command::class)) {
49
            throw InvalidCommand::doesntExtendCommand();
50
        }
51
    }
52
}
53