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

Link::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
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
}