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 ( ee651b...67f1c0 )
by Anton
03:43 queued 12s
created

Importer::hosts()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 8
c 1
b 0
f 0
nc 7
nop 1
dl 0
loc 11
rs 9.6111
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\Importer;
12
13
use Deployer\Exception\ConfigurationException;
14
use Deployer\Exception\Exception;
15
use Symfony\Component\Yaml\Yaml;
16
use function array_filter;
17
use function array_keys;
18
use function Deployer\after;
19
use function Deployer\before;
20
use function Deployer\cd;
21
use function Deployer\download;
22
use function Deployer\host;
23
use function Deployer\localhost;
24
use function Deployer\run;
25
use function Deployer\runLocally;
26
use function Deployer\set;
27
use function Deployer\Support\find_line_number;
28
use function Deployer\task;
29
use function Deployer\upload;
30
use const ARRAY_FILTER_USE_KEY;
31
32
class Importer
33
{
34
    /**
35
     * @var string
36
     */
37
    private static $recipeFilename;
38
    /**
39
     * @var string
40
     */
41
    private static $recipeSource;
42
43
    /**
44
     * @param string|string[] $paths
45
     */
46
    public static function import($paths)
47
    {
48
        if (!is_array($paths)) {
49
            $paths = [$paths];
50
        }
51
        foreach ($paths as $path) {
52
            if (preg_match('/\.php$/i', $path)) {
53
                // Prevent variable leak into deploy.php file
54
                call_user_func(function () use ($path) {
55
                    // Reorder autoload stack
56
                    $originStack = spl_autoload_functions();
57
58
                    require $path;
59
60
                    $newStack = spl_autoload_functions();
61
                    if ($originStack[0] !== $newStack[0]) {
62
                        foreach (array_reverse($originStack) as $loader) {
63
                            spl_autoload_unregister($loader);
64
                            spl_autoload_register($loader, true, true);
65
                        }
66
                    }
67
                });
68
            } elseif (preg_match('/\.ya?ml$/i', $path)) {
69
                self::$recipeFilename = basename($path);
70
                self::$recipeSource = file_get_contents($path, true);
71
72
                $root = array_filter(Yaml::parse(self::$recipeSource), static function (string $key) {
73
                    return !str_starts_with($key, '.');
74
                }, ARRAY_FILTER_USE_KEY);
75
76
                foreach (array_keys($root) as $key) {
77
                    static::$key($root[$key]);
78
                }
79
            } else {
80
                throw new Exception("Unknown file format: $path\nOnly .php and .yaml supported.");
81
            }
82
        }
83
    }
84
85
    protected static function hosts(array $hosts)
86
    {
87
        foreach ($hosts as $alias => $config) {
88
            if ($config['local'] ?? false) {
89
                $host = localhost($alias);
90
            } else {
91
                $host = host($alias);
92
            }
93
            if (is_array($config)) {
94
                foreach ($config as $key => $value) {
95
                    $host->set($key, $value);
0 ignored issues
show
Bug introduced by
The method set() does not exist on Deployer\Support\ObjectProxy. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

95
                    $host->/** @scrutinizer ignore-call */ 
96
                           set($key, $value);
Loading history...
96
                }
97
            }
98
        }
99
    }
100
101
    protected static function config(array $config)
102
    {
103
        foreach ($config as $key => $value) {
104
            set($key, $value);
105
        }
106
    }
107
108
    protected static function tasks(array $tasks)
109
    {
110
        $buildTask = function ($name, $steps) {
111
            $body = function () {};
112
            $task = task($name, $body);
113
114
            foreach ($steps as $step) {
115
                $buildStep = function ($step) use (&$body, $task) {
116
                    extract($step);
117
118
                    if (isset($cd)) {
119
                        $prev = $body;
120
                        $body = function () use ($cd, $prev) {
121
                            $prev();
122
                            cd($cd);
123
                        };
124
                    }
125
126
                    if (isset($run)) {
127
                        $has = 'run';
128
                        $prev = $body;
129
                        $body = function () use ($run, $prev) {
130
                            $prev();
131
                            try {
132
                                run($run);
133
                            } catch (Exception $e) {
134
                                $e->setTaskFilename(self::$recipeFilename);
135
                                $e->setTaskLineNumber(find_line_number(self::$recipeSource, $run));
136
                                throw $e;
137
                            }
138
                        };
139
                    }
140
141
                    if (isset($run_locally)) {
142
                        if (isset($has)) {
143
                            throw new ConfigurationException("Task step can not have both $has and run_locally.");
144
                        }
145
                        $has = 'run_locally';
146
                        $prev = $body;
147
                        $body = function () use ($run_locally, $prev) {
148
                            $prev();
149
                            try {
150
                                runLocally($run_locally);
151
                            } catch (Exception $e) {
152
                                $e->setTaskFilename(self::$recipeFilename);
153
                                $e->setTaskLineNumber(find_line_number(self::$recipeSource, $run_locally));
154
                                throw $e;
155
                            }
156
                        };
157
                    }
158
159
                    if (isset($upload)) {
160
                        if (isset($has)) {
161
                            throw new ConfigurationException("Task step can not have both $has and upload.");
162
                        }
163
                        $has = 'upload';
164
                        $prev = $body;
165
                        $body = function () use ($upload, $prev) {
166
                            $prev();
167
                            upload($upload['src'], $upload['dest']);
168
                        };
169
                    }
170
171
                    if (isset($download)) {
172
                        if (isset($has)) {
173
                            throw new ConfigurationException("Task step can not have both $has and download.");
174
                        }
175
                        $has = 'download';
176
                        $prev = $body;
177
                        $body = function () use ($download, $prev) {
178
                            $prev();
179
                            download($download['src'], $download['dest']);
180
                        };
181
                    }
182
183
                    $methods = [
184
                        'desc',
185
                        'once',
186
                        'hidden',
187
                        'limit',
188
                        'select',
189
                    ];
190
                    foreach ($methods as $method) {
191
                        if (isset($$method)) {
192
                            $task->$method($$method);
193
                        }
194
                    }
195
                };
196
197
                $buildStep($step);
198
                $task->setCallback($body);
199
            }
200
        };
201
202
        foreach ($tasks as $name => $config) {
203
            foreach ($config as $key => $value) {
204
                if (!is_int($key) || !is_string($value)) {
205
                    goto not_a_group_task;
206
                }
207
            }
208
209
            // Create a group task.
210
            task($name, $config);
211
            continue;
212
213
            not_a_group_task:
214
            $buildTask($name, $config);
215
        }
216
    }
217
218
    protected static function after(array $after)
219
    {
220
        foreach ($after as $key => $value) {
221
            if (is_array($value)) {
222
                foreach (array_reverse($value) as $v) {
223
                    after($key, $v);
224
                }
225
            } else {
226
                after($key, $value);
227
            }
228
        }
229
    }
230
231
    protected static function before(array $before)
232
    {
233
        foreach ($before as $key => $value) {
234
            if (is_array($value)) {
235
                foreach (array_reverse($value) as $v) {
236
                    before($key, $v);
237
                }
238
            } else {
239
                before($key, $value);
240
            }
241
        }
242
    }
243
}
244