Url::normalizeUrl()   B
last analyzed

Complexity

Conditions 10
Paths 21

Size

Total Lines 37
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 10

Importance

Changes 4
Bugs 1 Features 0
Metric Value
cc 10
eloc 26
c 4
b 1
f 0
nc 21
nop 1
dl 0
loc 37
ccs 25
cts 25
cp 1
crap 10
rs 7.6666

How to fix   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
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of Wszetko Sitemap.
7
 *
8
 * (c) Paweł Kłopotek-Główczewski <[email protected]>
9
 *
10
 * This source file is subject to the MIT license that is bundled
11
 * with this source code in the file LICENSE.
12
 */
13
14
namespace Wszetko\Sitemap\Helpers;
15
16
use League\Uri\Components\Domain;
17
use League\Uri\Components\Fragment;
18
use League\Uri\Components\Host;
19
use League\Uri\Components\Path;
20
use League\Uri\Components\Port;
21
use League\Uri\Components\Query;
22
use League\Uri\Components\Scheme;
23
use League\Uri\Components\UserInfo;
24
use League\Uri\Exceptions\SyntaxError;
25
use League\Uri\Uri;
26
use League\Uri\UriModifier;
27
use League\Uri\UriString;
28
29
/**
30
 * Class Url.
31
 *
32
 * @package Wszetko\Sitemap\Helpers
33
 */
34
class Url
35
{
36
    /**
37
     * @param string $url
38
     *
39
     * @return bool|string
40
     *
41
     * @throws \League\Uri\Exceptions\SyntaxError
42
     */
43 260
    public static function normalizeUrl(string $url)
44
    {
45
        try {
46 260
            $uri = UriModifier::removeDotSegments(Uri::createFromString($url));
47 240
            $parts = [];
48 240
            $parts['scheme'] = Scheme::createFromUri($uri)->getContent();
49 240
            $parts['user'] = UserInfo::createFromUri($uri)->getUser();
50 240
            $parts['pass'] = UserInfo::createFromUri($uri)->getPass();
51 240
            $parts['host'] = Host::createFromUri($uri);
52 240
            $parts['port'] = Port::createFromUri($uri)->getContent();
53 240
            $parts['path'] = Path::createFromUri($uri)->getContent();
54 240
            $parts['query'] = Query::createFromUri($uri)->getContent();
55 240
            $parts['fragment'] = Fragment::createFromUri($uri)->getContent();
56
57
            if (
58 240
                '' === $parts['scheme'] ||
59 240
                null === $parts['scheme'] ||
60 228
                '' === $parts['host']->getContent() ||
61 228
                null === $parts['host']->getContent() ||
62 240
                (false === $parts['host']->isDomain() && false === $parts['host']->isIp())
63
            ) {
64 30
                return false;
65
            }
66
67 212
            if (true === $parts['host']->isDomain()) {
68 204
                $domain = new Domain($parts['host']);
69
70 204
                if (true === $domain->isAbsolute()) {
71 2
                    return false;
72
                }
73
            }
74
75 210
            $parts['host'] = (string) $parts['host'];
76
77 210
            return UriString::build($parts);
78 20
        } catch (\TypeError | SyntaxError $e) {
79 20
            return false;
80
        }
81
    }
82
}
83