Passed
Push — update-deps ( 14dfe0 )
by Jonathan
16:47
created

Parser::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Purl;
6
7
use InvalidArgumentException;
8
use Pdp\Parser as PslParser;
0 ignored issues
show
Bug introduced by
The type Pdp\Parser was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
introduced by
Type Pdp\Parser (as PslParser) is not used in this file.
Loading history...
9
use Pdp\Rules;
0 ignored issues
show
Bug introduced by
The type Pdp\Rules was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
introduced by
Type Pdp\Rules is not used in this file.
Loading history...
10
use function array_merge;
11
use function array_reverse;
12
use function explode;
13
use function implode;
14
use function parse_url;
15
use function preg_match;
0 ignored issues
show
introduced by
Type preg_match is not used in this file.
Loading history...
16
use function sprintf;
17
18
/**
19
 * Parser class.
20
 */
21
class Parser implements ParserInterface
22
{
23
    /** @var mixed[] */
24
    private static $defaultParts = [
25
        'scheme'             => null,
26
        'host'               => null,
27
        'port'               => null,
28
        'user'               => null,
29
        'pass'               => null,
30
        'path'               => null,
31
        'query'              => null,
32
        'fragment'           => null,
33
        'canonical'          => null,
34
        'resource'           => null,
35
    ];
36
37
    /**
38
     * @param string|Url|null $url
39
     *
40
     * @return mixed[]
41
     */
42
    public function parseUrl($url) : array
43
    {
44
        $url = (string) $url;
45
46
        $parsedUrl = $this->doParseUrl($url);
47
48
        if ($parsedUrl === []) {
49
            throw new InvalidArgumentException(sprintf('Invalid url %s', $url));
50
        }
51
52
        $parsedUrl = array_merge(self::$defaultParts, $parsedUrl);
53
54
        if (isset($parsedUrl['host'])) {
55
            $parsedUrl['canonical'] = implode('.', array_reverse(explode('.', $parsedUrl['host']))) . ($parsedUrl['path'] ?? '') . (isset($parsedUrl['query']) ? '?' . $parsedUrl['query'] : '');
56
57
            $parsedUrl['resource'] = $parsedUrl['path'] ?? '';
58
59
            if (isset($parsedUrl['query'])) {
60
                $parsedUrl['resource'] .= '?' . $parsedUrl['query'];
61
            }
62
        }
63
64
        return $parsedUrl;
65
    }
66
67
    /**
68
     * @return mixed[]
69
     */
70
    protected function doParseUrl(string $url) : array
71
    {
72
        $parsedUrl = parse_url($url);
73
74
        return $parsedUrl !== false ? $parsedUrl : [];
75
    }
76
}
77