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.
Completed
Push — master ( 884458...9473b8 )
by Patrique
01:07
created

Uri::withScheme()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Patoui\Router;
6
7
use Psr\Http\Message\UriInterface;
8
9
class Uri implements UriInterface
10
{
11
    /** @var string */
12
    private $scheme;
13
14
    /** @var string */
15
    private $host;
16
17
    /** @var int */
18
    private $port;
19
20
    /** @var string */
21
    private $user;
22
23
    /** @var string */
24
    private $password;
25
26
    /** @var string */
27
    private $path;
28
29
    /** @var string */
30
    private $query;
31
32
    /** @var string */
33
    private $fragment;
34
35
    public function __construct(string $uri)
36
    {
37
        $scheme = parse_url($uri, PHP_URL_SCHEME);
38
        $host = parse_url($uri, PHP_URL_HOST);
39
        $port = parse_url($uri, PHP_URL_PORT);
40
        $port = $port ? (int) $port : 80;
41
        $user = parse_url($uri, PHP_URL_USER);
42
        $password = parse_url($uri, PHP_URL_PASS);
43
        $path = parse_url($uri, PHP_URL_PATH);
44
        $query = parse_url($uri, PHP_URL_QUERY);
45
        $fragment = parse_url($uri, PHP_URL_FRAGMENT);
46
47
        $this->scheme = $scheme ?: '';
48
        $this->host = $host ?: '';
49
        $this->port = $port;
50
        $this->user = $user ?: '';
51
        $this->path = $path ?: '';
52
        $this->password = $password ?: '';
53
        $this->query = $query ?: '';
54
        $this->fragment = $fragment ?: '';
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function getScheme()
61
    {
62
        return $this->scheme;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function getAuthority()
69
    {
70
        $userInfo = $this->getUserInfo();
71
        $port = $this->getPort();
72
        $authority = ($userInfo !== '' ? $userInfo . '@' : '');
73
        $authority .=  $this->getHost();
74
        $authority .= ($port !== null ? ':' . $port : '');
75
76
        return $authority;
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function getUserInfo()
83
    {
84
        if (! $this->user) {
85
            return '';
86
        }
87
88
        return $this->user . ($this->password ? ":{$this->password}" : '');
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function getHost()
95
    {
96
        return $this->host;
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function getPort()
103
    {
104
        return $this->port;
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110
    public function getPath()
111
    {
112
        return $this->path;
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118
    public function getQuery()
119
    {
120
        return $this->query;
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126
    public function getFragment()
127
    {
128
        return $this->fragment;
129
    }
130
131
    /**
132
     * Return an instance with the specified scheme.
133
     *
134
     * This method MUST retain the state of the current instance, and return
135
     * an instance that contains the specified scheme.
136
     *
137
     * Implementations MUST support the schemes "http" and "https" case
138
     * insensitively, and MAY accommodate other schemes if required.
139
     *
140
     * An empty scheme is equivalent to removing the scheme.
141
     *
142
     * @param  string  $scheme  The scheme to use with the new instance.
143
     * @return static A new instance with the specified scheme.
144
     * @throws \InvalidArgumentException for invalid or unsupported schemes.
145
     */
146
    public function withScheme($scheme)
147
    {
148
        if (preg_match('/^[a-zA-Z0-9+-.]+$/', $scheme) === 0) {
149
            throw new \InvalidArgumentException(
150
                "Invalid scheme {$scheme}; please reference RFC3986 for additional details"
151
            );
152
        }
153
154
        $instance = clone $this;
155
        $instance->scheme = strtolower($scheme);
156
157
        return $instance;
158
    }
159
160
    /**
161
     * {@inheritdoc}
162
     */
163
    public function withUserInfo($user, $password = null)
164
    {
165
        $instance = clone $this;
166
        $instance->user = $user;
167
        if ($password) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $password of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
168
            $instance->password = $password;
169
        }
170
171
        return $instance;
172
    }
173
174
    /**
175
     * {@inheritdoc}
176
     */
177
    public function withHost($host)
178
    {
179
        if (!is_string($host)) {
180
            throw new \InvalidArgumentException("Invalid host: {$host}");
181
        }
182
183
        if (filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
184
            $host = "[{$host}]";
185
        }
186
187
        $instance = clone $this;
188
        $instance->host = $host;
189
190
        return $instance;
191
    }
192
193
    /**
194
     * {@inheritdoc}
195
     */
196
    public function withPort($port)
197
    {
198
        if (!is_int($port)) {
199
            throw new \InvalidArgumentException("Invalid port: {$port}");
200
        }
201
202
        $instance = clone $this;
203
        $instance->port = $port;
204
205
        return $instance;
206
    }
207
208
    /**
209
     * {@inheritdoc}
210
     */
211
    public function withPath($path)
212
    {
213
        if (!is_string($path)) {
214
            throw new \InvalidArgumentException("Invalid path: {$path}");
215
        }
216
217
        $instance = clone $this;
218
        $instance->path = $path;
219
220
        return $instance;
221
    }
222
223
    /**
224
     * {@inheritdoc}
225
     */
226 View Code Duplication
    public function withQuery($query)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
227
    {
228
        if (!is_string($query)) {
229
            throw new \InvalidArgumentException("Invalid query: {$query}");
230
        }
231
232
        $instance = clone $this;
233
        $instance->query = ltrim($query, '?');
234
235
        return $instance;
236
    }
237
238
    /**
239
     * {@inheritdoc}
240
     */
241 View Code Duplication
    public function withFragment($fragment)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
242
    {
243
        if (!is_string($fragment)) {
244
            throw new \InvalidArgumentException("Invalid fragment: {$fragment}");
245
        }
246
247
        $instance = clone $this;
248
        $instance->fragment = ltrim($fragment, '#');
249
250
        return $instance;
251
    }
252
253
    /**
254
     * {@inheritdoc}
255
     */
256
    public function __toString()
257
    {
258
        $scheme = $this->getScheme();
259
        $authority = $this->getAuthority();
260
        $path = $this->getPath();
261
        $query = $this->getQuery();
262
        $fragment = $this->getFragment();
263
        $uri = $scheme ? "{$scheme}:" : '';
264
        $uri .= $authority ? "//{$authority}" : '';
265
266
        if ($path) {
267
            if ($authority && strlen(ltrim($path, '/')) === 0) {
268
                $uri .= '/';
269
            } elseif (!$authority && strpos($path, '//') === 0) {
270
                $uri .= '/' . ltrim($path, '/');
271
            } else {
272
                $uri .= $path;
273
            }
274
        }
275
276
        $uri .= $query ? '?' . ltrim($query, '?') : '';
277
        $uri .= $fragment ? '#' . ltrim($fragment, '#') : '';
278
279
        return $uri;
280
    }
281
}
282