Completed
Push — dev ( 122982...5e45c8 )
by Zach
02:18
created

ConsoleKernel::verifyCommand()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Yarak\Console;
4
5
use Yarak\Console\Command;
6
use Yarak\Exceptions\FileNotFound;
7
use Yarak\Exceptions\InvalidCommand;
8
9
class ConsoleKernel
10
{
11
    /**
12
     * Get all user defined commands.
13
     *
14
     * @return array
15
     */
16
    public function getCommands()
17
    {
18
        if (property_exists($this, 'commands')) {   
19
            return array_map(function ($command) {
20
                $this->verifyCommand($command);
21
22
                return $command;
23
            }, $this->commands);
0 ignored issues
show
Bug introduced by
The property commands does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
24
        }
25
    }
26
27
    /**
28
     * Verify user defined commands.
29
     *
30
     * @param  string $command
31
     *
32
     * @throws FileNotFound|InvalidCommand
33
     */
34
    protected function verifyCommand($command)
35
    {
36
        if (!class_exists($command)) {
37
            throw FileNotFound::commandNotFound($command);
38
        }
39
40
        $class = new $command();
41
42
        if (!is_a($class, Command::class)) {
43
            throw InvalidCommand::doesntExtendCommand();
44
        }
45
    }
46
}
47