DebugCommand::createTreeFromTaskName()   B
last analyzed

Complexity

Conditions 10
Paths 54

Size

Total Lines 46

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 0
Metric Value
cc 10
nc 54
nop 3
dl 0
loc 46
ccs 0
cts 25
cp 0
crap 110
rs 7.3115
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/* (c) Anton Medvedev <[email protected]>
3
/* (c) Oskar van Velden <[email protected]>
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Deployer\Console;
10
11
use Deployer\Deployer;
12
use Deployer\Task\GroupTask;
13
use Deployer\Task\TaskCollection;
14
use Symfony\Component\Console\Command\Command;
15
use Symfony\Component\Console\Input\InputArgument;
16
use Symfony\Component\Console\Input\InputInterface as Input;
17
use Symfony\Component\Console\Output\OutputInterface as Output;
18
19
class DebugCommand extends Command
20
{
21
    /** @var Output */
22
    protected $output;
23
24
    /**
25
     * @var TaskCollection
26
     */
27
    private $tasks;
28
29
    /**
30
     * @var Deployer
31
     */
32
    private $deployer;
33
34
    /**
35
     * @var array
36
     */
37
    private $tree;
38
39
    /**
40
     * Depth of nesting (for rendering purposes)
41
     * @var int
42
     */
43
    private $depth = 0;
44
45
    /**
46
     * @var array
47
     */
48
    private $openGroupDepths = [];
49
50
    /**
51
     * @param Deployer $deployer
52
     */
53 17
    public function __construct(Deployer $deployer)
54
    {
55 17
        parent::__construct('debug:task');
56 17
        $this->setDescription('Display the task-tree for a given task');
57 17
        $this->deployer = $deployer;
58 17
        $this->tree = [];
59 17
    }
60
61
    /**
62
     * Configures the command
63
     */
64 17
    protected function configure()
65
    {
66 17
        $this->addArgument(
67 17
            'task',
68 17
            InputArgument::REQUIRED,
69 17
            'Task to display the tree for'
70
        );
71 17
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    protected function execute(Input $input, Output $output)
77
    {
78
        $this->output = $output;
79
80
        $rootTaskName = $input->getArgument('task');
81
82
        $this->buildTree($rootTaskName);
83
        $this->outputTree($rootTaskName);
84
    }
85
86
    /**
87
     * Build the tree based on the given taskName
88
     * @param $taskName
89
     *
90
     * @return void
91
     */
92
    private function buildTree($taskName)
93
    {
94
        $this->tasks = Deployer::get()->tasks;
0 ignored issues
show
Documentation Bug introduced by Oskar van Velden
It seems like \Deployer\Deployer::get()->tasks can also be of type array<integer,object<Deployer\Task\Task>>. However, the property $tasks is declared as type object<Deployer\Task\TaskCollection>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
95
        $this->createTreeFromTaskName($taskName, '', true);
96
    }
97
98
    /**
99
     * Create a tree from the given taskname
100
     *
101
     * @param string $taskName
102
     * @param string $postfix
103
     * @param bool $isLast
104
     *
105
     * @return void
106
     */
107
    private function createTreeFromTaskName($taskName, $postfix = '', $isLast = false)
108
    {
109
        $task = $this->tasks->get($taskName);
110
111
        if ($task->getBefore()) {
112
            $beforePostfix = sprintf(' [before:%s]', $task->getName());
113
114
            foreach ($task->getBefore() as $beforeTask) {
115
                $this->createTreeFromTaskName($beforeTask, $beforePostfix);
116
            }
117
        }
118
119
        if ($task instanceof GroupTask) {
120
            $isLast = $isLast && empty($task->getAfter());
121
122
            $this->addTaskToTree($task->getName() . $postfix, $isLast);
123
124
            if (!$isLast) {
125
                $this->openGroupDepths[] = $this->depth;
126
            }
127
128
            $this->depth++;
129
130
            $taskGroup = $task->getGroup();
131
            foreach ($taskGroup as $subtask) {
132
                $isLastSubtask = $subtask === end($taskGroup);
133
                $this->createTreeFromTaskName($subtask, '', $isLastSubtask);
134
            }
135
136
            if (!$isLast) {
137
                array_pop($this->openGroupDepths);
138
            }
139
140
            $this->depth--;
141
        } else {
142
            $this->addTaskToTree($task->getName() . $postfix, $isLast);
143
        }
144
145
        if ($task->getAfter()) {
146
            $afterPostfix = sprintf(' [after:%s]', $task->getName());
147
148
            foreach ($task->getAfter() as $afterTask) {
149
                $this->createTreeFromTaskName($afterTask, $afterPostfix);
150
            }
151
        }
152
    }
153
154
    /**
155
     * Add the (formatted) taskName to the rendertree, with some additional information
156
     *
157
     * @param string $taskName formatted with prefixes if needed
158
     * @param bool $isLast indication for what symbol to use for rendering
159
     */
160
    private function addTaskToTree($taskName, $isLast = false)
161
    {
162
        $this->tree[] = [
163
            'taskName' => $taskName,
164
            'depth' => $this->depth,
165
            'isLast' => $isLast,
166
            'openDepths' => $this->openGroupDepths
167
        ];
168
    }
169
170
    /**
171
     * Render the tree, after everything is build
172
     *
173
     * @param $taskName
174
     */
175
    private function outputTree($taskName)
176
    {
177
        $this->output->writeln("The task-tree for <fg=cyan>$taskName</fg=cyan>:");
178
179
        /**
180
         * @var $REPEAT_COUNT number of spaces for each depth increase
181
         */
182
        $REPEAT_COUNT = 4;
183
184
        foreach ($this->tree as $treeItem) {
185
            $depth = $treeItem['depth'];
186
187
            $startSymbol = $treeItem['isLast'] || $treeItem === end($this->tree) ? '└' : '├';
188
189
            $prefix = '';
190
191
            for ($i = 0; $i < $depth; $i++) {
192
                if (in_array($i, $treeItem['openDepths'])) {
193
                    $prefix .= '│' . str_repeat(' ', $REPEAT_COUNT - 1);
194
                } else {
195
                    $prefix .= str_repeat(' ', $REPEAT_COUNT);
196
                }
197
            }
198
199
            $prefix .=  $startSymbol . '──';
200
201
            $this->output->writeln(sprintf('%s %s', $prefix, $treeItem['taskName']));
202
        }
203
    }
204
}
205