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.
Test Setup Failed
Pull Request — master (#1904)
by Anton
01:48
created

Task::shouldBePerformed()   B

Complexity

Conditions 9
Paths 14

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 9

Importance

Changes 0
Metric Value
cc 9
nc 14
nop 1
dl 0
loc 24
ccs 10
cts 10
cp 1
crap 9
rs 8.0555
c 0
b 0
f 0
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\Task;
9
10
use Deployer\Host\Host;
11
use function Deployer\Support\array_flatten;
12
13
class Task
14
{
15
    /**
16
     * @var string
17
     */
18
    private $name;
19
20
    /**
21
     * @var callable
22
     */
23
    private $callback;
24
25
    /**
26
     * @var string
27
     */
28
    private $description;
29
30
    /**
31
     * Should we run this task locally?
32
     *
33
     * @var bool
34
     */
35
    private $local = false;
36
37
    /**
38
     * Lists of hosts, roles there task should be executed.
39
     *
40
     * @var array
41
     */
42
    private $on = ['hosts' => [], 'roles' => []];
43
44
    /**
45
     * List of task names to run before.
46
     *
47
     * @var array
48
     */
49
    private $before = [];
50
51
    /**
52
     * List of task names to run after.
53
     *
54
     * @var array
55
     */
56
    private $after = [];
57
58
    /**
59
     * Make task internal and not visible in CLI.
60
     *
61
     * @var bool
62
     */
63
    private $private = false;
64
65
    /**
66
     * Mark task to run only once, of the first node from the pool
67
     *
68
     * @var bool
69
     */
70
    private $once = false;
71
72
    /**
73
     * Mark if the task has run at least once
74
     *
75
     * @var bool
76
     */
77
    private $hasRun = false;
78
79
    /**
80
     * Shallow task will not print execution message/finish messages.
81
     * Useful for success messages and info printing.
82
     *
83
     * @var bool
84
     */
85
    private $shallow = false;
86
87
    /**
88
     * @param string $name Tasks name
89
     * @param callable $callback Task code
90
     */
91 24
    public function __construct($name, callable $callback = null)
92
    {
93 24
        $this->name = $name;
94 24
        $this->callback = $callback;
95 24
    }
96
97
    /**
98
     * @param Context $context
99
     */
100 14
    public function run(Context $context)
101
    {
102 14
        Context::push($context);
103
104
        // Call task
105 14
        call_user_func($this->callback);
106
107 14
        if ($this->once) {
108 1
            $this->hasRun = true;
109
        }
110
111
        // Clear working_path
112 14
        if ($context->getConfig() !== null) {
113 11
            $context->getConfig()->set('working_path', false);
114
        }
115
116 14
        Context::pop();
117 14
    }
118
119
    public function getName()
120
    {
121
        return $this->name;
122 16
    }
123
124 16
    public function __toString()
125
    {
126
        return $this->getName();
127
    }
128
129
    public function getDescription()
130 13
    {
131
        return $this->description;
132 13
    }
133
134
    /**
135
     * @param string $description
136
     * @return $this
137
     */
138
    public function desc($description)
139 9
    {
140
        $this->description = $description;
141 9
        return $this;
142 9
    }
143
144
    /**
145
     * Mark this task local
146
     *
147
     * @return $this
148
     */
149
    public function local()
150 13
    {
151
        $this->local = true;
152 13
        return $this;
153 13
    }
154
155
    /**
156
     * @return bool
157
     */
158
    public function isLocal()
159 12
    {
160
        return $this->local;
161 12
    }
162
163
    public function once()
164 10
    {
165
        $this->once = true;
166 10
        return $this;
167 10
    }
168
169
    public function isOnce()
170 1
    {
171
        return $this->once;
172 1
    }
173
174
    /**
175
     * @param array $hosts
176
     * @return $this
177
     */
178
    public function onHosts(...$hosts)
179 2
    {
180
        $this->on['hosts'] = array_flatten($hosts);
181 2
        return $this;
182 2
    }
183
184
    /**
185
     * @param array $roles
186
     * @return $this
187
     */
188
    public function onRoles(...$roles)
189 2
    {
190
        $this->on['roles'] = array_flatten($roles);
191 2
        return $this;
192 2
    }
193
194
    /**
195
     * Checks what task should be performed on one of hosts.
196
     *
197
     * @param Host[] $hosts
198
     * @return bool
199 2
     */
200
    public function shouldBePerformed(...$hosts)
201 2
    {
202 2
        // don't allow to run again it the task has been marked to run only once
203
        if ($this->once && $this->hasRun) {
204
            return false;
205
        }
206
207
        foreach ($hosts as $host) {
208
            $onHost = empty($this->on['hosts']) || in_array($host->alias(), $this->on['hosts'], true);
0 ignored issues
show
Bug introduced by
The method alias cannot be called on $host (of type array<integer,object<Deployer\Host\Host>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
209
210
            $onRole = empty($this->on['roles']);
211 16
            foreach ((array) $host->get('roles', []) as $role) {
212
                if (in_array($role, $this->on['roles'], true)) {
213
                    $onRole = true;
214 16
                }
215 1
            }
216
217
            if ($onHost && $onRole) {
218 16
                return true;
219 13
            }
220
        }
221 13
222 13
        return empty($hosts);
223 6
    }
224 1
225
    /**
226
     * @return boolean
227
     */
228 13
    public function isPrivate()
229 13
    {
230 2
        return $this->private;
231 1
    }
232
233
    /**
234
     * Mark task as private
235 13
     *
236 13
     * @return $this
237
     */
238
    public function setPrivate()
239
    {
240 4
        $this->private = true;
241
        return $this;
242
    }
243
244
    /**
245
     * @param string $task
246 13
     *
247
     * @return $this
248 13
     */
249
    public function addBefore(string $task)
250
    {
251
        array_unshift($this->before, $task);
252
        return $this;
253
    }
254
255
    /**
256 9
     * @param string $task
257
     *
258 9
     * @return $this
259 9
     */
260
    public function addAfter(string $task)
261
    {
262
        array_push($this->after, $task);
263
        return $this;
264
    }
265
266
    /**
267 1
     * Get before tasks names.
268
     * @return string[]
269 1
     */
270 1
    public function getBefore()
271
    {
272
        return $this->before;
273
    }
274
275
    /**
276
     * Get after tasks names.
277
     * @return string[]
278 1
     */
279
    public function getAfter()
280 1
    {
281 1
        return $this->after;
282
    }
283
284
    /**
285
     * Sets task shallow.
286
     *
287
     * Shallow task will not print execution message/finish messages.
288 14
     *
289
     * @return $this
290 14
     */
291
    public function shallow()
292
    {
293
        $this->shallow = true;
294
        return $this;
295
    }
296
297 14
    /**
298
     * @return bool
299 14
     */
300
    public function isShallow()
301
    {
302
        return $this->shallow;
303
    }
304
305
    /**
306
     * @internal this is used by ParallelExecutor and prevent multiple run
307
     */
308
    public function setHasRun()
309 8
    {
310
        if ($this->isOnce()) {
311 8
            $this->hasRun = true;
312 8
        }
313
    }
314
}
315