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

src/Link.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
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
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
}