GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

TreeCommand::createTreeFromTaskName()   C
last analyzed

Complexity

Conditions 12
Paths 162

Size

Total Lines 51
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 29
nc 162
nop 3
dl 0
loc 51
c 0
b 0
f 0
cc 12
rs 6.45

How to fix   Long Method    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
3
declare(strict_types=1);
4
5
/* (c) Anton Medvedev <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Deployer\Command;
12
13
use Deployer\Deployer;
14
use Deployer\Task\GroupTask;
15
use Symfony\Component\Console\Command\Command;
16
use Symfony\Component\Console\Completion\CompletionInput;
17
use Symfony\Component\Console\Completion\CompletionSuggestions;
18
use Symfony\Component\Console\Input\InputArgument;
19
use Symfony\Component\Console\Input\InputInterface as Input;
20
use Symfony\Component\Console\Output\OutputInterface as Output;
21
22
class TreeCommand extends Command
23
{
24
    /**
25
     * @var Output
26
     */
27
    protected $output;
28
    /**
29
     * @var Deployer
30
     */
31
    private $deployer;
32
    /**
33
     * @var array
34
     */
35
    private $tree;
36
    /**
37
     * @var int
38
     */
39
    private $depth = 0;
40
    /**
41
     * @var array
42
     */
43
    private $openGroupDepths = [];
44
45
    public function __construct(Deployer $deployer)
46
    {
47
        parent::__construct('tree');
48
        $this->setDescription('Display the task-tree for a given task');
49
        $this->deployer = $deployer;
50
        $this->tree = [];
51
    }
52
53
    protected function configure()
54
    {
55
        $this->addArgument(
56
            'task',
57
            InputArgument::REQUIRED,
58
            'Task to display the tree for',
59
        );
60
    }
61
62
    protected function execute(Input $input, Output $output): int
63
    {
64
        $this->output = $output;
65
66
        $rootTaskName = $input->getArgument('task');
67
68
        $this->buildTree($rootTaskName);
69
        $this->outputTree($rootTaskName);
70
        return 0;
71
    }
72
73
    private function buildTree(string $taskName)
74
    {
75
        $this->createTreeFromTaskName($taskName, '', true);
76
    }
77
78
    private function createTreeFromTaskName(string $taskName, string $postfix = '', bool $isLast = false)
79
    {
80
        $task = $this->deployer->tasks->get($taskName);
81
82
        if (!$task->isEnabled()) {
83
            if (empty($postfix)) {
84
                $postfix = '  // disabled';
85
            } else {
86
                $postfix .= '; disabled';
87
            }
88
        }
89
90
        if ($task->getBefore()) {
91
            $beforePostfix = sprintf("  // before %s", $task->getName());
92
93
            foreach ($task->getBefore() as $beforeTask) {
94
                $this->createTreeFromTaskName($beforeTask, $beforePostfix);
95
            }
96
        }
97
98
        if ($task instanceof GroupTask) {
99
            $isLast = $isLast && empty($task->getAfter());
100
101
            $this->addTaskToTree($task->getName() . $postfix, $isLast);
102
103
            if (!$isLast) {
104
                $this->openGroupDepths[] = $this->depth;
105
            }
106
107
            $this->depth++;
108
109
            $taskGroup = $task->getGroup();
110
            foreach ($taskGroup as $subtask) {
111
                $isLastSubtask = $subtask === end($taskGroup);
112
                $this->createTreeFromTaskName($subtask, '', $isLastSubtask);
113
            }
114
115
            if (!$isLast) {
116
                array_pop($this->openGroupDepths);
117
            }
118
119
            $this->depth--;
120
        } else {
121
            $this->addTaskToTree($task->getName() . $postfix, $isLast);
122
        }
123
124
        if ($task->getAfter()) {
125
            $afterPostfix = sprintf("  // after %s", $task->getName());
126
127
            foreach ($task->getAfter() as $afterTask) {
128
                $this->createTreeFromTaskName($afterTask, $afterPostfix);
129
            }
130
        }
131
    }
132
133
    private function addTaskToTree(string $taskName, bool $isLast = false)
134
    {
135
        $this->tree[] = [
136
            'taskName' => $taskName,
137
            'depth' => $this->depth,
138
            'isLast' => $isLast,
139
            'openDepths' => $this->openGroupDepths,
140
        ];
141
    }
142
143
    private function outputTree(string $taskName)
144
    {
145
        $this->output->writeln("The task-tree for <info>$taskName</info>:");
146
147
        /**
148
         * @var int number of spaces for each depth increase
149
         */
150
        $REPEAT_COUNT = 4;
151
152
        foreach ($this->tree as $treeItem) {
153
            $depth = $treeItem['depth'];
154
155
            $startSymbol = $treeItem['isLast'] || $treeItem === end($this->tree) ? '└' : '├';
156
157
            $prefix = '';
158
159
            for ($i = 0; $i < $depth; $i++) {
160
                if (in_array($i, $treeItem['openDepths'])) {
161
                    $prefix .= '│' . str_repeat(' ', $REPEAT_COUNT - 1);
162
                } else {
163
                    $prefix .= str_repeat(' ', $REPEAT_COUNT);
164
                }
165
            }
166
167
            $prefix .= $startSymbol . '──';
168
169
            $this->output->writeln(sprintf('%s %s', $prefix, $treeItem['taskName']));
170
        }
171
    }
172
173
    public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
174
    {
175
        parent::complete($input, $suggestions);
176
        if ($input->mustSuggestArgumentValuesFor('task')) {
177
            $suggestions->suggestValues(array_keys($this->deployer->tasks->all()));
178
        }
179
    }
180
}
181