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.

ScriptManager::doGetTasks()   B
last analyzed

Complexity

Conditions 9
Paths 17

Size

Total Lines 34
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 21.6484

Importance

Changes 0
Metric Value
cc 9
eloc 23
c 0
b 0
f 0
nc 17
nop 1
dl 0
loc 34
rs 8.0555
ccs 6
cts 13
cp 0.4615
crap 21.6484
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\Task;
12
13
use Deployer\Exception\Exception;
14
15
use function Deployer\Support\array_flatten;
16
17
class ScriptManager
18
{
19 17
    /**
20
     * @var TaskCollection
21 17
     */
22 17
    private $tasks;
23
    /**
24
     * @var bool
25
     */
26
    private $hooksEnabled = true;
27
    /**
28
     * @var array
29 17
     */
30
    private $visitedTasks = [];
31 17
32 17
    public function __construct(TaskCollection $tasks)
33
    {
34 15
        $this->tasks = $tasks;
35 15
    }
36
37
    /**
38
     * Return tasks to run.
39
     *
40
     * @return Task[]
41
     */
42
    public function getTasks(string $name, ?string $startFrom = null, array &$skipped = []): array
43
    {
44
        $tasks = [];
45
        $this->visitedTasks = [];
46
        $allTasks = $this->doGetTasks($name);
47
48
        if ($startFrom === null) {
49
            $tasks = $allTasks;
50
        } else {
51
            $skip = true;
52 15
            foreach ($allTasks as $task) {
53
                if ($skip) {
54
                    if ($task->getName() === $startFrom) {
55
                        $skip = false;
56
                    } else {
57
                        $skipped[] = $task->getName();
58
                        continue;
59 17
                    }
60
                }
61 17
                $tasks[] = $task;
62
            }
63 17
            if (count($tasks) === 0) {
64
                throw new Exception('All tasks skipped via --start-from option. Nothing to run.');
65 15
            }
66 15
        }
67
68
        $enabledTasks = [];
69 15
        foreach ($tasks as $task) {
70 5
            if ($task->isEnabled()) {
71 5
                $enabledTasks[] = $task;
72 5
            }
73 5
        }
74 5
75
        return $enabledTasks;
76
    }
77
78 15
    /**
79
     * @return Task[]
80
     */
81 15
    public function doGetTasks(string $name): array
82 15
    {
83
        if (array_key_exists($name, $this->visitedTasks)) {
84
            if ($this->visitedTasks[$name] >= 100) {
85 15
                throw new Exception("Looks like a circular dependency with \"$name\" task.");
86
            }
87
            $this->visitedTasks[$name]++;
88
        } else {
89
            $this->visitedTasks[$name] = 1;
90
        }
91
92
        $tasks = [];
93 12
        $task = $this->tasks->get($name);
94
        if ($this->hooksEnabled) {
95 12
            $tasks = array_merge(array_map([$this, 'doGetTasks'], $task->getBefore()), $tasks);
96 12
        }
97
        if ($task instanceof GroupTask) {
98
            foreach ($task->getGroup() as $taskName) {
99
                $subTasks = $this->doGetTasks($taskName);
100
                foreach ($subTasks as $subTask) {
101
                    $subTask->addSelector($task->getSelector());
102
                    if ($task->isOnce()) {
103
                        $subTask->once();
104
                    }
105
                    $tasks[] = $subTask;
106
                }
107
            }
108
        } else {
109
            $tasks[] = $task;
110
        }
111
        if ($this->hooksEnabled) {
112
            $tasks = array_merge($tasks, array_map([$this, 'doGetTasks'], $task->getAfter()));
113
        }
114
        return array_flatten($tasks);
115
    }
116
117
    public function getHooksEnabled(): bool
118
    {
119
        return $this->hooksEnabled;
120
    }
121
122
    public function setHooksEnabled(bool $hooksEnabled): void
123
    {
124
        $this->hooksEnabled = $hooksEnabled;
125
    }
126
}
127