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 (#10)
by Oliver
01:51
created

Reader::resolveContainer()   D

Complexity

Conditions 21
Paths 24

Size

Total Lines 73
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 21
eloc 49
nc 24
nop 0
dl 0
loc 73
rs 4.1666
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 \UnexpectedValueException(
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
        $stream = '';
49
        if ($this->fileType === 'application/x-gzip') {
50
            $stream = 'compress.zlib://';
51
        } elseif ($this->fileType === 'application/x-bzip2') {
52
            $stream = 'compress.bzip2://';
53
        }
54
55
        $stubContent = null;
56
        $manifestContent = null;
57
        $manifestLength = null;
58
        $resource = fopen($stream . $this->fileName, 'r');
59
        while (!feof($resource)) {
0 ignored issues
show
Bug introduced by
It seems like $resource can also be of type false; however, parameter $handle of feof() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

59
        while (!feof(/** @scrutinizer ignore-type */ $resource)) {
Loading history...
60
            $line = fgets($resource);
0 ignored issues
show
Bug introduced by
It seems like $resource can also be of type false; however, parameter $handle of fgets() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

60
            $line = fgets(/** @scrutinizer ignore-type */ $resource);
Loading history...
61
            // stop reading file when manifest can be extracted
62
            if ($manifestLength !== null && $manifestContent !== null && strlen($manifestContent) >= $manifestLength) {
63
                break;
64
            }
65
66
            $stubPosition = strpos($line, '<?php');
67
            $manifestPosition = strpos($line, '__HALT_COMPILER()');
68
69
            // line contains both, start of (empty) stub and start of manifest
70
            if ($stubContent === null && $stubPosition !== false
71
                && $manifestContent === null && $manifestPosition !== false) {
72
                $stubContent = substr($line, $stubPosition, $manifestPosition - $stubPosition - 1);
73
                $manifestContent = preg_replace('#^.*__HALT_COMPILER\(\)[^>]*\?>(\r|\n)*#', '', $line);
74
                $manifestLength = $this->resolveManifestLength($manifestContent);
75
            // line contains start of stub
76
            } elseif ($stubContent === null && $stubPosition !== false) {
77
                $stubContent = substr($line, $stubPosition);
78
            // line contains start of manifest
79
            } elseif ($manifestContent === null && $manifestPosition !== false) {
80
                $manifestContent = preg_replace('#^.*__HALT_COMPILER\(\)[^>]*\?>(\r|\n)*#', '', $line);
81
                $manifestLength = $this->resolveManifestLength($manifestContent);
82
            // manifest has been started (thus is cannot be stub anymore), add content
83
            } elseif ($manifestContent !== null) {
84
                $manifestContent .= $line;
85
                $manifestLength = $this->resolveManifestLength($manifestContent);
86
            // stub has been started (thus cannot be manifest here, yet), add content
87
            } elseif ($stubContent !== null) {
88
                $stubContent .= $line;
89
            }
90
        }
91
        fclose($resource);
0 ignored issues
show
Bug introduced by
It seems like $resource can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

91
        fclose(/** @scrutinizer ignore-type */ $resource);
Loading history...
92
93
        if ($stubContent === null) {
94
            throw new \UnexpectedValueException(
95
                'Cannot resolve stub',
96
                1547807881
97
            );
98
        }
99
        if ($manifestContent === null || $manifestLength === null) {
100
            throw new \UnexpectedValueException(
101
                'Cannot resolve manifest',
102
                1547807882
103
            );
104
        }
105
        if (strlen($manifestContent) < $manifestLength) {
106
            throw new \UnexpectedValueException(
107
                sprintf(
108
                    'Exected manifest length %d, got %d',
109
                    strlen($manifestContent),
110
                    $manifestLength
111
                ),
112
                1547807883
113
            );
114
        }
115
116
        return new Container(
117
            Stub::fromContent($stubContent),
118
            Manifest::fromContent($manifestContent)
119
        );
120
    }
121
122
    /**
123
     * @return string
124
     */
125
    private function determineFileType()
126
    {
127
        $fileInfo = new \finfo();
128
        return $fileInfo->file($this->fileName, FILEINFO_MIME_TYPE);
129
    }
130
131
    /**
132
     * @param string $content
133
     * @return int|null
134
     */
135
    private function resolveManifestLength(string $content)
136
    {
137
        if (strlen($content) < 4) {
138
            return null;
139
        }
140
        return static::resolveFourByteLittleEndian($content, 0);
141
    }
142
143
    /**
144
     * @param string $content
145
     * @param int $start
146
     * @return int
147
     */
148
    public static function resolveFourByteLittleEndian(string $content, int $start): int
149
    {
150
        $payload = substr($content, $start, 4);
151
        if (!is_string($payload)) {
0 ignored issues
show
introduced by
The condition is_string($payload) is always true.
Loading history...
152
            throw new \UnexpectedValueException(
153
                sprintf('Cannot resolve value at offset %d', $start),
154
                1539614260
155
            );
156
        }
157
158
        $value = unpack('V', $payload);
159
        if (!isset($value[1])) {
160
            throw new \UnexpectedValueException(
161
                sprintf('Cannot resolve value at offset %d', $start),
162
                1539614261
163
            );
164
        }
165
        return $value[1];
166
    }
167
168
    /**
169
     * @param string $content
170
     * @param int $start
171
     * @return int
172
     */
173
    public static function resolveTwoByteBigEndian(string $content, int $start): int
174
    {
175
        $payload = substr($content, $start, 2);
176
        if (!is_string($payload)) {
0 ignored issues
show
introduced by
The condition is_string($payload) is always true.
Loading history...
177
            throw new \UnexpectedValueException(
178
                sprintf('Cannot resolve value at offset %d', $start),
179
                1539614263
180
            );
181
        }
182
183
        $value = unpack('n', $payload);
184
        if (!isset($value[1])) {
185
            throw new \UnexpectedValueException(
186
                sprintf('Cannot resolve value at offset %d', $start),
187
                1539614264
188
            );
189
        }
190
        return $value[1];
191
    }
192
}
193