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 ( 695db5...fba8ee )
by Anton
02:16
created

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