CommandsBuilder::buildCommand()   B
last analyzed

Complexity

Conditions 5
Paths 12

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5.0113

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 12
cts 13
cp 0.9231
rs 8.439
c 0
b 0
f 0
cc 5
eloc 13
nc 12
nop 2
crap 5.0113
1
<?php
2
3
/*
4
 * This file is part of Rocketeer
5
 *
6
 * (c) Maxime Fabre <[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
13
namespace Rocketeer\Services\Builders\Modules;
14
15
use Rocketeer\Console\Commands\AbstractCommand;
16
use Rocketeer\Console\Commands\BaseTaskCommand;
17
use Rocketeer\Services\Builders\TaskCompositionException;
18
19
/**
20
 * Builds Symfony console commands.
21
 */
22
class CommandsBuilder extends AbstractBuilderModule
23
{
24
    /**
25
     * Build the command bound to a task.
26
     *
27
     * @param string|\Rocketeer\Tasks\AbstractTask $task
28
     * @param string|null                          $slug
29
     *
30
     * @return \Rocketeer\Console\Commands\AbstractCommand
31
     */
32 91
    public function buildCommand($task, $slug = null)
33
    {
34
        // Build the task instance
35
        try {
36 91
            $instance = $this->modulable->buildTask($task);
0 ignored issues
show
Documentation Bug introduced by Maxime Fabre
The method buildTask does not exist on object<Rocketeer\Services\Builders\Builder>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
37 91
        } catch (TaskCompositionException $exception) {
38
            $instance = null;
39
        }
40
41
        // Get the command name
42 91
        $name = $instance ? $instance->getName() : null;
43 91
        $command = $this->modulable->findQualifiedName($name, 'commands');
44
45
        // If no command found, use BaseTaskCommand or task name
46 91
        if ($command === BaseTaskCommand::class) {
47 90
            $name = is_string($task) ? $task : $name;
48 90
            $command = $this->modulable->findQualifiedName($name, 'commands');
49 90
        }
50
51
        /** @var AbstractCommand $command */
52 91
        $command = new $command($instance, $slug);
53 91
        $command->setContainer($this->container);
54
55 91
        return $command;
56
    }
57
58
    /**
59
     * @return string[]
60
     */
61 442
    public function getProvided()
62
    {
63
        return [
64 442
            'buildCommand',
65 442
        ];
66
    }
67
}
68