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.
Passed
Push — master ( 97c63e...5f3221 )
by Anton
02:14
created

src/Console/DebugCommand.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 12
    public function __construct(Deployer $deployer)
54
    {
55 12
        parent::__construct('debug:task');
56 12
        $this->setDescription('Display the task-tree for a given task');
57 12
        $this->deployer = $deployer;
58 12
        $this->tree = [];
59 12
    }
60
61
    /**
62
     * Configures the command
63
     */
64 12
    protected function configure()
65
    {
66 12
        $this->addArgument(
67 12
            'task',
68 12
            InputArgument::REQUIRED,
69 12
            'Task to display the tree for'
70
        );
71 12
    }
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
        return 0;
85
    }
86
87
    /**
88
     * Build the tree based on the given taskName
89
     * @param $taskName
90
     *
91
     * @return void
92
     */
93
    private function buildTree($taskName)
94
    {
95
        $this->tasks = Deployer::get()->tasks;
0 ignored issues
show
Documentation Bug introduced by
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...
96
        $this->createTreeFromTaskName($taskName, '', true);
97
    }
98
99
    /**
100
     * Create a tree from the given taskname
101
     *
102
     * @param string $taskName
103
     * @param string $postfix
104
     * @param bool $isLast
105
     *
106
     * @return void
107
     */
108
    private function createTreeFromTaskName($taskName, $postfix = '', $isLast = false)
109
    {
110
        $task = $this->tasks->get($taskName);
111
112
        if ($task->getBefore()) {
113
            $beforePostfix = sprintf(' [before:%s]', $task->getName());
114
115
            foreach ($task->getBefore() as $beforeTask) {
116
                $this->createTreeFromTaskName($beforeTask, $beforePostfix);
117
            }
118
        }
119
120
        if ($task instanceof GroupTask) {
121
            $isLast = $isLast && empty($task->getAfter());
122
123
            $this->addTaskToTree($task->getName() . $postfix, $isLast);
124
125
            if (!$isLast) {
126
                $this->openGroupDepths[] = $this->depth;
127
            }
128
129
            $this->depth++;
130
131
            $taskGroup = $task->getGroup();
132
            foreach ($taskGroup as $subtask) {
133
                $isLastSubtask = $subtask === end($taskGroup);
134
                $this->createTreeFromTaskName($subtask, '', $isLastSubtask);
135
            }
136
137
            if (!$isLast) {
138
                array_pop($this->openGroupDepths);
139
            }
140
141
            $this->depth--;
142
        } else {
143
            $this->addTaskToTree($task->getName() . $postfix, $isLast);
144
        }
145
146
        if ($task->getAfter()) {
147
            $afterPostfix = sprintf(' [after:%s]', $task->getName());
148
149
            foreach ($task->getAfter() as $afterTask) {
150
                $this->createTreeFromTaskName($afterTask, $afterPostfix);
151
            }
152
        }
153
    }
154
155
    /**
156
     * Add the (formatted) taskName to the rendertree, with some additional information
157
     *
158
     * @param string $taskName formatted with prefixes if needed
159
     * @param bool $isLast indication for what symbol to use for rendering
160
     */
161
    private function addTaskToTree($taskName, $isLast = false)
162
    {
163
        $this->tree[] = [
164
            'taskName' => $taskName,
165
            'depth' => $this->depth,
166
            'isLast' => $isLast,
167
            'openDepths' => $this->openGroupDepths
168
        ];
169
    }
170
171
    /**
172
     * Render the tree, after everything is build
173
     *
174
     * @param $taskName
175
     */
176
    private function outputTree($taskName)
177
    {
178
        $this->output->writeln("The task-tree for <fg=cyan>$taskName</fg=cyan>:");
179
180
        /**
181
         * @var $REPEAT_COUNT number of spaces for each depth increase
182
         */
183
        $REPEAT_COUNT = 4;
184
185
        foreach ($this->tree as $treeItem) {
186
            $depth = $treeItem['depth'];
187
188
            $startSymbol = $treeItem['isLast'] || $treeItem === end($this->tree) ? '└' : '├';
189
190
            $prefix = '';
191
192
            for ($i = 0; $i < $depth; $i++) {
193
                if (in_array($i, $treeItem['openDepths'])) {
194
                    $prefix .= '│' . str_repeat(' ', $REPEAT_COUNT - 1);
195
                } else {
196
                    $prefix .= str_repeat(' ', $REPEAT_COUNT);
197
                }
198
            }
199
200
            $prefix .=  $startSymbol . '──';
201
202
            $this->output->writeln(sprintf('%s %s', $prefix, $treeItem['taskName']));
203
        }
204
    }
205
}
206