Completed
Push — master ( 9dc7a5...9e19cf )
by Denis
01:42
created

Link::__construct()   C

Complexity

Conditions 10
Paths 14

Size

Total Lines 22
Code Lines 15

Duplication

Lines 8
Ratio 36.36 %

Importance

Changes 0
Metric Value
cc 10
eloc 15
nc 14
nop 2
dl 8
loc 22
rs 6.1368
c 0
b 0
f 0

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
 * @author: Denis Beliaev
4
 */
5
6
namespace DenisBeliaev\SitemapParser;
7
8
/**
9
 * Class Link
10
 * @package DenisBeliaev\SitemapParser
11
 */
12
class Link
13
{
14
    private $url = '';
15
    private $components;
16
17
    /**
18
     * Link constructor.
19
     * @param string $link URL or <a> tag with href property
20
     * @param string $base Full URL to base page including protocol.
21
     */
22
    public function __construct($link, $base = '')
23
    {
24
        if(!empty($base)) {
25
            $this->components = parse_url($base);
26
        }
27
        $link = explode('#', $link)[0];
28
29
        if ($link == '/') {
30 View Code Duplication
            if (!empty($this->components['scheme']) && !empty($this->components['host'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
31
                $this->url = $this->components['scheme'] . '://' . $this->components['host'];
32
            }
33
        } elseif (preg_match('~^//[^/].*~', $link)) {
34
            $scheme = $this->components['scheme'] ?? 'http';
35
            $this->url = $scheme . ':' . $link;
36 View Code Duplication
        } elseif (preg_match('~^/[^/].*~', $link)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
37
            if (!empty($this->components['scheme']) && !empty($this->components['host'])) {
38
                $this->url = $this->components['scheme'] . '://' . $this->components['host'] . $link;
39
            }
40
        } elseif (preg_match('~^https?://[^/].*~', $link)) {
41
            $this->url = $link;
42
        }
43
    }
44
45
    public function __toString()
46
    {
47
        return $this->url;
48
    }
49
}