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
05:07
created

DumpServices::generateSupportedFile()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 32
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 32
rs 9.568
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\n- $product";
56
            foreach ($versions as $version) {
57
                $versionsNum++;
58
                $list .= "\n    - $version";
59
            }
60
        }
61
62
        $en = <<<EOT
63
# Supported
64
**Alibaba Cloud SDK for PHP** has supported quick access to $versionsNum versions of $productNum products, as listed below:
65
$list
66
67
EOT;
68
69
        $cn = <<<EOT
70
# 支持
71
**Alibaba Cloud SDK for PHP** 已经支持 $productNum 个产品的 $versionsNum 个版本的快捷访问,列表如下:
72
$list
73
74
EOT;
75
76
        $file = __DIR__ . DIRECTORY_SEPARATOR . '../SUPPORTED.md';
77
        \file_put_contents($file, $en);
78
79
        $file = __DIR__ . DIRECTORY_SEPARATOR . '../SUPPORTED-CN.md';
80
        \file_put_contents($file, $cn);
81
    }
82
83
    /**
84
     * @param string $productDirectory
85
     *
86
     * @return array
87
     */
88
    private static function generateVersionResolverFile($productDirectory)
89
    {
90
        $productName = basename($productDirectory);
91
92
        $versions = self::getVersions($productDirectory);
93
        if ($versions === []) {
94
            return [];
95
        }
96
97
        $method = '';
98
        foreach ($versions as $version) {
99
            $lcVersion = \lcfirst($version);
100
            $method    .= \PHP_EOL . " * @method {$version}\\{$productName}ApiResolver {$lcVersion}()";
101
        }
102
103
        $php = <<<EOT
104
<?php
105
106
namespace AlibabaCloud\\{$productName};
107
108
use AlibabaCloud\VersionResolverTrait;
109
110
/**
111
 * Resolve version based on the method name.
112
 *{$method}
113
 */
114
class {$productName}
115
{
116
    use VersionResolverTrait;
117
}
118
119
EOT;
120
121
        $fileName = $productDirectory . DIRECTORY_SEPARATOR . $productName . '.php';
122
        \file_put_contents($fileName, $php);
123
124
        return $versions;
125
    }
126
127
    /**
128
     * @param string $productDirectory
129
     *
130
     * @return array
131
     */
132
    private static function getVersions($productDirectory)
133
    {
134
        $versions = [];
135
        foreach (glob($productDirectory . DIRECTORY_SEPARATOR . '*') as $versionDirectory) {
136
            // Product have versions.
137
            if (is_dir($versionDirectory) && \mb_strlen(\basename($versionDirectory)) === 9) {
138
                $versions[] = \basename($versionDirectory);
139
            }
140
        }
141
142
        // Product have no versions.
143
        if ($versions === []) {
144
            self::generateNoVersionApiResolverFile($productDirectory);
145
        }
146
147
        return $versions;
148
    }
149
150
    /**
151
     * @param string $versionDirectory
152
     *
153
     * @return array
154
     */
155
    private static function getApis($versionDirectory)
156
    {
157
        $apis = [];
158
        foreach (glob($versionDirectory . DIRECTORY_SEPARATOR . '*') as $apiDirectory) {
159
            if (\is_file($apiDirectory)) {
160
                $apis[] = \basename($apiDirectory);
161
            }
162
        }
163
164
        return $apis;
165
    }
166
167
    /**
168
     * @param string $directory
169
     *
170
     * @return void
171
     */
172
    private static function generateNoVersionApiResolverFile($directory)
173
    {
174
        $product = basename($directory);
175
        $apis    = self::getApis($directory);
176
        $method  = '';
177
        foreach ($apis as $api) {
178
            $api   = \str_replace('.php', '', $api);
179
            $lcApi = \lcfirst($api);
180
181
            // Avoid adding the itself to the code
182
            if ($api !== $product) {
183
                $method .= \PHP_EOL . " * @method static $api {$lcApi}(array \$options = [])";
184
            }
185
        }
186
187
        $php = <<<EOT
188
<?php
189
190
namespace AlibabaCloud\\{$product};
191
192
use AlibabaCloud\ApiResolverTrait;
193
194
/**
195
 * Class Find the specified Api of the $product based on the method name as the Api name.
196
 *
197
 *{$method}
198
 */
199
class {$product}
200
{
201
    use ApiResolverTrait;
202
}
203
204
EOT;
205
206
        $fileName = $directory . DIRECTORY_SEPARATOR . $product . '.php';
207
        \file_put_contents($fileName, $php);
208
    }
209
210
    /**
211
     * @return void
212
     */
213
    private static function generateServiceResolverTraitFile()
214
    {
215
        $method = '';
216
        foreach (self::$products as $product) {
217
            $lcProduct = \lcfirst($product);
218
            if (self::isStaticMethodExists($lcProduct)) {
219
                $lcProduct .= 'Service';
220
            }
221
            $method .= \PHP_EOL . " * @method static {$product}\\{$product} {$lcProduct}()";
222
        }
223
224
        $php = <<<EOT
225
<?php
226
227
namespace AlibabaCloud;
228
229
use AlibabaCloud\Client\AlibabaCloud;
230
231
/**
232
 * Resolve product based on the static method name.
233
 *
234
 * @mixin     AlibabaCloud
235
 *
236
 * @codeCoverageIgnore
237
 *{$method}
238
 */
239
trait ServiceResolverTrait
240
{
241
}
242
243
EOT;
244
245
        \file_put_contents(self::getServiceResolverTraitFilePath(), $php);
246
    }
247
248
    /**
249
     * @param string $methodName
250
     *
251
     * @return bool
252
     */
253
    private static function isStaticMethodExists($methodName)
254
    {
255
        try {
256
            $class = new \ReflectionClass(AlibabaCloud::class);
257
            foreach ($class->getMethods() as $method) {
258
                if ($method->name === $methodName) {
259
                    return true;
260
                }
261
            }
262
263
            return false;
264
        } catch (ReflectionException $e) {
265
            echo $e->getMessage();
266
            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...
267
        }
268
    }
269
270
    /**
271
     * @return string
272
     */
273
    private static function getServiceResolverTraitFilePath()
274
    {
275
        return __DIR__ . DIRECTORY_SEPARATOR . 'ServiceResolverTrait.php';
276
    }
277
}
278