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

ConsoleKernel   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 38
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getCommands() 0 10 2
A verifyCommand() 0 12 3
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