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
Pull Request — master (#59)
by Yong
04:50
created

DumpServices::generateSupportedFile()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 37
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 37
rs 9.488
c 0
b 0
f 0
cc 3
nc 3
nop 0
1
<?php
2
3
namespace AlibabaCloud;
4
5
use AlibabaCloud\Client\AlibabaCloud;
6
use Composer\Script\Event;
7
use ReflectionException;
8
9
/**
10
 * CLass DumpServices
11
 *
12
 * @package   AlibabaCloud
13
 * @mixin     AlibabaCloud
14
 * @codeCoverageIgnore
15
 */
16
class DumpServices
17
{
18
19
    /**
20
     * @var array
21
     */
22
    private static $products = [];
23
24
    /**
25
     * @var array
26
     */
27
    private static $supported = [];
28
29
    /**
30
     * @param Event $event
31
     *
32
     * @return void
33
     */
34
    public static function dump(Event $event)
35
    {
36
        require_once $event->getComposer()->getConfig()->get('vendor-dir') . '/autoload.php';
37
        foreach (glob(__DIR__ . DIRECTORY_SEPARATOR . '*') as $productDirectory) {
38
            if (is_dir($productDirectory)) {
39
                $product                   = \basename($productDirectory);
40
                self::$products[]          = $product;
41
                $versions                  = self::generateVersionResolverFile($productDirectory);
42
                self::$supported[$product] = $versions;
43
            }
44
        }
45
        self::generateServiceResolverTraitFile();
46
        self::generateSupportedFile();
47
    }
48
49
    private static function generateSupportedFile()
50
    {
51
        $productNum  = count(self::$supported);
52
        $versionsNum = 0;
53
        $list        = '';
54
        foreach (self::$supported as $product => $versions) {
55
            $list .= "\n- **$product**";
56
            foreach ($versions as $version) {
57
                $version = str_replace('V', '', $version);
58
                $versionsNum++;
59
                $list .= " `$version`";
60
            }
61
        }
62
63
        $en = <<<EOT
64
English | [简体中文](./SUPPORTED-CN.md)
65
66
# Supported
67
Alibaba Cloud SDK for PHP has supported quick access to $versionsNum versions of $productNum products, as listed below:
68
$list
69
70
EOT;
71
72
        $cn = <<<EOT
73
[English](./SUPPORTED.md) | 简体中文
74
75
# 支持
76
Alibaba Cloud SDK for PHP 已经支持 $productNum 个产品的 $versionsNum 个版本的快捷访问,列表如下:
77
$list
78
79
EOT;
80
81
        $file = __DIR__ . DIRECTORY_SEPARATOR . '../SUPPORTED.md';
82
        \file_put_contents($file, $en);
83
84
        $file = __DIR__ . DIRECTORY_SEPARATOR . '../SUPPORTED-CN.md';
85
        \file_put_contents($file, $cn);
86
    }
87
88
    /**
89
     * @param string $productDirectory
90
     *
91
     * @return array
92
     */
93
    private static function generateVersionResolverFile($productDirectory)
94
    {
95
        $productName = basename($productDirectory);
96
97
        $versions = self::getVersions($productDirectory);
98
        if ($versions === []) {
99
            return [];
100
        }
101
102
        $method = '';
103
        foreach ($versions as $version) {
104
            $lcVersion = \lcfirst($version);
105
            $method    .= \PHP_EOL . " * @method {$version}\\{$productName}ApiResolver {$lcVersion}()";
106
        }
107
108
        $php = <<<EOT
109
<?php
110
111
namespace AlibabaCloud\\{$productName};
112
113
use AlibabaCloud\VersionResolverTrait;
114
115
/**
116
 * Resolve version based on the method name.
117
 *{$method}
118
 */
119
class {$productName}
120
{
121
    use VersionResolverTrait;
122
}
123
124
EOT;
125
126
        $fileName = $productDirectory . DIRECTORY_SEPARATOR . $productName . '.php';
127
        \file_put_contents($fileName, $php);
128
129
        return $versions;
130
    }
131
132
    /**
133
     * @param string $productDirectory
134
     *
135
     * @return array
136
     */
137
    private static function getVersions($productDirectory)
138
    {
139
        $versions = [];
140
        foreach (glob($productDirectory . DIRECTORY_SEPARATOR . '*') as $versionDirectory) {
141
            // Product have versions.
142
            if (is_dir($versionDirectory) && \mb_strlen(\basename($versionDirectory)) === 9) {
143
                $versions[] = \basename($versionDirectory);
144
            }
145
        }
146
147
        // Product have no versions.
148
        if ($versions === []) {
149
            self::generateNoVersionApiResolverFile($productDirectory);
150
        }
151
152
        return $versions;
153
    }
154
155
    /**
156
     * @param string $versionDirectory
157
     *
158
     * @return array
159
     */
160
    private static function getApis($versionDirectory)
161
    {
162
        $apis = [];
163
        foreach (glob($versionDirectory . DIRECTORY_SEPARATOR . '*') as $apiDirectory) {
164
            if (\is_file($apiDirectory)) {
165
                $apis[] = \basename($apiDirectory);
166
            }
167
        }
168
169
        return $apis;
170
    }
171
172
    /**
173
     * @param string $directory
174
     *
175
     * @return void
176
     */
177
    private static function generateNoVersionApiResolverFile($directory)
178
    {
179
        $product = basename($directory);
180
        $apis    = self::getApis($directory);
181
        $method  = '';
182
        foreach ($apis as $api) {
183
            $api   = \str_replace('.php', '', $api);
184
            $lcApi = \lcfirst($api);
185
186
            // Avoid adding the itself to the code
187
            if ($api !== $product) {
188
                $method .= \PHP_EOL . " * @method static $api {$lcApi}(array \$options = [])";
189
            }
190
        }
191
192
        $php = <<<EOT
193
<?php
194
195
namespace AlibabaCloud\\{$product};
196
197
use AlibabaCloud\ApiResolverTrait;
198
199
/**
200
 * Class Find the specified Api of the $product based on the method name as the Api name.
201
 *
202
 *{$method}
203
 */
204
class {$product}
205
{
206
    use ApiResolverTrait;
207
}
208
209
EOT;
210
211
        $fileName = $directory . DIRECTORY_SEPARATOR . $product . '.php';
212
        \file_put_contents($fileName, $php);
213
    }
214
215
    /**
216
     * @return void
217
     */
218
    private static function generateServiceResolverTraitFile()
219
    {
220
        $method = '';
221
        foreach (self::$products as $product) {
222
            $lcProduct = \lcfirst($product);
223
            if (self::isStaticMethodExists($lcProduct)) {
224
                $lcProduct .= 'Service';
225
            }
226
            $method .= \PHP_EOL . " * @method static {$product}\\{$product} {$lcProduct}()";
227
        }
228
229
        $php = <<<EOT
230
<?php
231
232
namespace AlibabaCloud;
233
234
use AlibabaCloud\Client\AlibabaCloud;
235
236
/**
237
 * Resolve product based on the static method name.
238
 *
239
 * @mixin     AlibabaCloud
240
 *
241
 * @codeCoverageIgnore
242
 *{$method}
243
 */
244
trait ServiceResolverTrait
245
{
246
}
247
248
EOT;
249
250
        \file_put_contents(self::getServiceResolverTraitFilePath(), $php);
251
    }
252
253
    /**
254
     * @param string $methodName
255
     *
256
     * @return bool
257
     */
258
    private static function isStaticMethodExists($methodName)
259
    {
260
        try {
261
            $class = new \ReflectionClass(AlibabaCloud::class);
262
            foreach ($class->getMethods() as $method) {
263
                if ($method->name === $methodName) {
264
                    return true;
265
                }
266
            }
267
268
            return false;
269
        } catch (ReflectionException $e) {
270
            echo $e->getMessage();
271
            exit(-1);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
272
        }
273
    }
274
275
    /**
276
     * @return string
277
     */
278
    private static function getServiceResolverTraitFilePath()
279
    {
280
        return __DIR__ . DIRECTORY_SEPARATOR . 'ServiceResolverTrait.php';
281
    }
282
}
283