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 ( b3cbcf...88f5a9 )
by Oliver
44s
created

Reader::resolveStream()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
namespace TYPO3\PharStreamWrapper\Phar;
4
5
/*
6
 * This file is part of the TYPO3 project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under the terms
9
 * of the MIT License (MIT). For the full copyright and license information,
10
 * please read the LICENSE file that was distributed with this source code.
11
 *
12
 * The TYPO3 project - inspiring people to share!
13
 */
14
15
class Reader
16
{
17
    /**
18
     * @var string
19
     */
20
    private $fileName;
21
22
    /**
23
     * @var string
24
     */
25
    private $fileType;
26
27
    /**
28
     * @param string $fileName
29
     */
30
    public function __construct(string $fileName)
31
    {
32
        if (strpos($fileName, '://') !== false) {
33
            throw new ReaderException(
34
                'File name must not contain stream prefix',
35
                1539623708
36
            );
37
        }
38
39
        $this->fileName = $fileName;
40
        $this->fileType = $this->determineFileType();
41
    }
42
43
    /**
44
     * @return Container
45
     */
46
    public function resolveContainer(): Container
47
    {
48
        $data = $this->extractData($this->resolveStream() . $this->fileName);
49
50
        if ($data['stubContent'] === null) {
51
            throw new ReaderException(
52
                'Cannot resolve stub',
53
                1547807881
54
            );
55
        }
56
        if ($data['manifestContent'] === null || $data['manifestLength'] === null) {
57
            throw new ReaderException(
58
                'Cannot resolve manifest',
59
                1547807882
60
            );
61
        }
62
        if (strlen($data['manifestContent']) < $data['manifestLength']) {
63
            throw new ReaderException(
64
                sprintf(
65
                    'Exected manifest length %d, got %d',
66
                    strlen($data['manifestContent']),
67
                    $data['manifestLength']
68
                ),
69
                1547807883
70
            );
71
        }
72
73
        return new Container(
74
            Stub::fromContent($data['stubContent']),
75
            Manifest::fromContent($data['manifestContent'])
76
        );
77
    }
78
79
    /**
80
     * @param string $fileName e.g. '/path/file.phar' or 'compress.zlib:///path/file.phar'
81
     * @return array
82
     */
83
    private function extractData(string $fileName): array
84
    {
85
        $stubContent = null;
86
        $manifestContent = null;
87
        $manifestLength = null;
88
89
        $resource = fopen($fileName, 'r');
90
        if (!is_resource($resource)) {
91
            throw new ReaderException(
92
                sprintf('Resource %s could not be opened', $fileName),
93
                1547902055
94
            );
95
        }
96
97
        while (!feof($resource)) {
98
            $line = fgets($resource);
99
            // stop reading file when manifest can be extracted
100
            if ($manifestLength !== null && $manifestContent !== null && strlen($manifestContent) >= $manifestLength) {
101
                break;
102
            }
103
104
            $stubPosition = strpos($line, '<?php');
105
            $manifestPosition = strpos($line, '__HALT_COMPILER()');
106
107
            // line contains both, start of (empty) stub and start of manifest
108
            if ($stubContent === null && $stubPosition !== false
109
                && $manifestContent === null && $manifestPosition !== false) {
110
                $stubContent = substr($line, $stubPosition, $manifestPosition - $stubPosition - 1);
111
                $manifestContent = preg_replace('#^.*__HALT_COMPILER\(\)[^>]*\?>(\r|\n)*#', '', $line);
112
                $manifestLength = $this->resolveManifestLength($manifestContent);
113
            // line contains start of stub
114
            } elseif ($stubContent === null && $stubPosition !== false) {
115
                $stubContent = substr($line, $stubPosition);
116
            // line contains start of manifest
117
            } elseif ($manifestContent === null && $manifestPosition !== false) {
118
                $manifestContent = preg_replace('#^.*__HALT_COMPILER\(\)[^>]*\?>(\r|\n)*#', '', $line);
119
                $manifestLength = $this->resolveManifestLength($manifestContent);
120
            // manifest has been started (thus is cannot be stub anymore), add content
121
            } elseif ($manifestContent !== null) {
122
                $manifestContent .= $line;
123
                $manifestLength = $this->resolveManifestLength($manifestContent);
124
            // stub has been started (thus cannot be manifest here, yet), add content
125
            } elseif ($stubContent !== null) {
126
                $stubContent .= $line;
127
            }
128
        }
129
        fclose($resource);
130
131
        return [
132
            'stubContent' => $stubContent,
133
            'manifestContent' => $manifestContent,
134
            'manifestLength' => $manifestLength,
135
        ];
136
    }
137
138
    /**
139
     * Resolves stream in order to handle compressed Phar archives.
140
     *
141
     * @return string
142
     */
143
    private function resolveStream(): string
144
    {
145
        if ($this->fileType === 'application/x-gzip') {
146
            return 'compress.zlib://';
147
        } elseif ($this->fileType === 'application/x-bzip2') {
148
            return 'compress.bzip2://';
149
        }
150
        return '';
151
    }
152
153
    /**
154
     * @return string
155
     */
156
    private function determineFileType()
157
    {
158
        $fileInfo = new \finfo();
159
        return $fileInfo->file($this->fileName, FILEINFO_MIME_TYPE);
160
    }
161
162
    /**
163
     * @param string $content
164
     * @return int|null
165
     */
166
    private function resolveManifestLength(string $content)
167
    {
168
        if (strlen($content) < 4) {
169
            return null;
170
        }
171
        return static::resolveFourByteLittleEndian($content, 0);
172
    }
173
174
    /**
175
     * @param string $content
176
     * @param int $start
177
     * @return int
178
     */
179
    public static function resolveFourByteLittleEndian(string $content, int $start): int
180
    {
181
        $payload = substr($content, $start, 4);
182
        if (!is_string($payload)) {
0 ignored issues
show
introduced by
The condition is_string($payload) is always true.
Loading history...
183
            throw new ReaderException(
184
                sprintf('Cannot resolve value at offset %d', $start),
185
                1539614260
186
            );
187
        }
188
189
        $value = unpack('V', $payload);
190
        if (!isset($value[1])) {
191
            throw new ReaderException(
192
                sprintf('Cannot resolve value at offset %d', $start),
193
                1539614261
194
            );
195
        }
196
        return $value[1];
197
    }
198
199
    /**
200
     * @param string $content
201
     * @param int $start
202
     * @return int
203
     */
204
    public static function resolveTwoByteBigEndian(string $content, int $start): int
205
    {
206
        $payload = substr($content, $start, 2);
207
        if (!is_string($payload)) {
0 ignored issues
show
introduced by
The condition is_string($payload) is always true.
Loading history...
208
            throw new ReaderException(
209
                sprintf('Cannot resolve value at offset %d', $start),
210
                1539614263
211
            );
212
        }
213
214
        $value = unpack('n', $payload);
215
        if (!isset($value[1])) {
216
            throw new ReaderException(
217
                sprintf('Cannot resolve value at offset %d', $start),
218
                1539614264
219
            );
220
        }
221
        return $value[1];
222
    }
223
}
224