Terminal   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 60
c 0
b 0
f 0
wmc 5
lcom 1
cbo 2
ccs 17
cts 17
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A execute() 0 6 1
A retrieveCommand() 0 12 2
A getAvailableCommands() 0 13 1
1
<?php
2
3
namespace DeGraciaMathieu\Clike;
4
5
class Terminal {
6
7
    /**
8
     * @param array $availableCommands
9
     */
10 3
    public function __construct(array $availableCommands)
11
    {
12 3
        $this->availableCommands = $availableCommands;
0 ignored issues
show
Bug introduced by
The property availableCommands 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...
13 3
    }
14
15
    /**
16
     * try to launch a command class from a command line
17
     * @param  string $commandLine
18
     * @throws \DeGraciaMathieu\Clike\Exceptions\UnknowCommand
19
     * @return array
20
     */
21 2
    public function execute(string $commandLine) :array
22
    {
23 2
        $command = $this->retrieveCommand($commandLine);
24
25 1
        return (new Command)->execute($command);
26
    }
27
28
    /**
29
     * Make Command class from binding
30
     * @param  string $binding
31
     * @throws \DeGraciaMathieu\Clike\Exceptions\UnknowCommand
32
     * @return \DeGraciaMathieu\Clike\Contracts\Command
33
     */
34 2
    protected function retrieveCommand(string $binding) :Contracts\Command
35
    {
36
        $command = array_filter($this->availableCommands, function($availableCommand) use($binding) {
37 2
            return (new $availableCommand)->binding() === $binding;
38 2
        });
39
40 2
        if (! isset($command[0])) {
41 1
            throw new Exceptions\UnknowCommand();
42
        }
43
44 1
        return new $command[0];
45
    }
46
47
    /**
48
     * Get all available commands
49
     * @return array
50
     */
51 1
    public function getAvailableCommands()
52
    {
53
        return array_map(function($availableCommand){
54
55 1
            $command = new $availableCommand;
56
57
            return [
58 1
                'binding' => $command->binding(),
59 1
                'description' => $command->description(),
60
            ];
61
62 1
        }, $this->availableCommands);
63
    }
64
}
65