Completed
Push — master ( d3d41a...4aec35 )
by Paweł
03:09
created

Url::normalizeUrl()   B

Complexity

Conditions 10
Paths 21

Size

Total Lines 36
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 36
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 246
    public static function normalizeUrl(string $url)
44
    {
45
        try {
46 246
            $uri = UriModifier::removeDotSegments(Uri::createFromString($url));
47 226
            $parts = [];
48 226
            $parts['scheme'] = Scheme::createFromUri($uri)->getContent();
49 226
            $parts['user'] = UserInfo::createFromUri($uri)->getUser();
50 226
            $parts['pass'] = UserInfo::createFromUri($uri)->getPass();
51 226
            $parts['host'] = Host::createFromUri($uri);
52 226
            $parts['port'] = Port::createFromUri($uri)->getContent();
53 226
            $parts['path'] = Path::createFromUri($uri)->getContent();
54 226
            $parts['query'] = Query::createFromUri($uri)->getContent();
55 226
            $parts['fragment'] = Fragment::createFromUri($uri)->getContent();
56
57
            if (
58 226
                '' === $parts['scheme'] ||
59 226
                null === $parts['scheme'] ||
60 214
                '' === $parts['host']->getContent() ||
61 214
                null === $parts['host']->getContent() ||
62 226
                (false === $parts['host']->isDomain() && false === $parts['host']->isIp())
63
            ) {
64 24
                return false;
65
            }
66
67 204
            if (true === $parts['host']->isDomain()) {
68 196
                $domain = new Domain($parts['host']);
69 196
                if (true === $domain->isAbsolute()) {
70 2
                    return false;
71
                }
72
            }
73
74 202
            $parts['host'] = (string) $parts['host'];
75
76 202
            return UriString::build($parts);
77 20
        } catch (\TypeError | SyntaxError $e) {
78 20
            return false;
79
        }
80
    }
81
}
82