Passed
Push — master ( e36ace...f978e8 )
by Théo
04:17 queued 01:56
created

get_prefix()   B

Complexity

Conditions 8
Paths 20

Size

Total Lines 47
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 8
eloc 25
c 2
b 0
f 0
nc 20
nop 0
dl 0
loc 47
rs 8.4444
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the box project.
7
 *
8
 * (c) Kevin Herrera <[email protected]>
9
 *     Théo Fidry <[email protected]>
10
 *
11
 * This source file is subject to the MIT license that is bundled
12
 * with this source code in the file LICENSE.
13
 */
14
15
function get_prefix(): string
16
{
17
    $gitHubToken = getenv('GITHUB_TOKEN');
18
19
    $headerOption = false === $gitHubToken || '' === $gitHubToken
20
        ? ''
21
        : "-H \"Authorization: token $gitHubToken\""
22
    ;
23
24
    $lastReleaseEndpointContents = shell_exec(<<<BASH
25
curl -s $headerOption https://api.github.com/repos/humbug/box/releases/latest
26
BASH
27
    );
28
29
    if (null === $lastReleaseEndpointContents) {
30
        throw new RuntimeException('Could not retrieve the last release endpoint.');
31
    }
32
33
    $contents = json_decode($lastReleaseEndpointContents, false, 512, JSON_PRETTY_PRINT);
34
35
    if (JSON_ERROR_NONE !== json_last_error()) {
36
        // TODO: switch to safe json parsing in the future
37
        throw new RuntimeException(
38
            sprintf(
39
                'Could not parse the request contents: "%d: %s"',
40
                json_last_error(),
41
                json_last_error_msg()
42
            )
43
        );
44
    }
45
46
    if (false === isset($contents->tag_name) || false === is_string($contents->tag_name)) {
47
        throw new RuntimeException(
48
            sprintf(
49
                'No tag name could be found in: %s',
50
                $lastReleaseEndpointContents
51
            )
52
        );
53
    }
54
55
    $lastRelease = trim($contents->tag_name);
56
57
    if ('' === $lastRelease) {
58
        throw new RuntimeException('Invalid tag name found.');
59
    }
60
61
    return 'HumbugBox'.str_replace('.', '', $lastRelease);
62
}
63
64
return [
65
    'prefix' => get_prefix(),
66
67
    'whitelist-global-classes' => false,
68
    'whitelist-global-functions' => false,
69
    'whitelist' => [
70
        \Composer\Semver\Semver::class,
71
    ],
72
73
    'patchers' => [
74
        // `stream_isatty()` is a PHP 7.2 function hence not defined in PHP 7.1. Unlike its function call, the string
75
        // in `function_exists()` is prefixed because the name can be resolved to a FQCN and appears as a user-land
76
        // function.
77
        //
78
        // Function whitelisting is not used here since most checks are in the form of:
79
        //
80
        // ```
81
        // if (function_exists('stream_isatty') return @stream_isatty($stream);
82
        // ```
83
        //
84
        // If the function is simply whitelisted, then it will check if the function `Humbug\stream_isatty` exists which
85
        // it will always even though the underlying function may not exists.
86
        //
87
        // The following patcher can be safely removed however once https://github.com/humbug/php-scoper/issues/278 is
88
        // fixed.
89
        //
90
        static function (string $filePath, string $prefix, string $contents): string {
91
            $files = [
92
                'vendor/symfony/console/Output/StreamOutput.php',
93
                'vendor/symfony/phpunit-bridge/DeprecationErrorHandler.php',
94
                'vendor/symfony/polyfill-php72/bootstrap.php',
95
                'vendor/symfony/polyfill-php72/Php72.php',
96
                'vendor/symfony/var-dumper/Dumper/CliDumper.php',
97
                'vendor/composer/xdebug-handler/src/Process.php',
98
            ];
99
100
            if (false === in_array($filePath, $files, true)) {
101
                return $contents;
102
            }
103
104
            $contents = preg_replace(
105
                '/function_exists\(\''.$prefix.'\\\\(\\\\)?stream_isatty\'\)/',
106
                "function_exists('stream_isatty')",
107
                $contents
108
            );
109
110
            $contents = preg_replace(
111
                '/function_exists\(\''.$prefix.'\\\\(\\\\)?sapi_windows_vt100_support\'\)/',
112
                "function_exists('sapi_windows_vt100_support')",
113
                $contents
114
            );
115
116
            return $contents;
117
        },
118
    ],
119
];
120