Completed
Push — master ( 03240e...4d781a )
by jelmer
02:19
created

Url   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 26.42 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 2
dl 14
loc 53
ccs 14
cts 14
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setUrl() 14 14 2
A getUrl() 0 4 1
A __toString() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Pageon\SlackWebhookMonolog\General;
4
5
use Pageon\SlackWebhookMonolog\General\Exceptions\InvalidUrlException;
6
7
/**
8
 * @author Jelmer Prins <[email protected]>
9
 *
10
 * @since 0.3.2
11
 */
12
class Url extends SerializeToString
13
{
14
    /**
15
     * @var string
16
     */
17
    private $url;
18
19
    /**
20
     * @param string $url
21
     */
22 25
    public function __construct($url)
23
    {
24 25
        $this->setUrl($url);
25 23
    }
26
27
    /**
28
     * @param string $url
29
     *
30
     * @throws InvalidUrlException Thrown when the url is not valid
31
     *
32
     * @return $this
33
     */
34 25 View Code Duplication
    private function setUrl($url)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in 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...
35
    {
36
        // remove the whitespace
37 25
        $url = trim($url);
38
39
        // @see https://gist.github.com/dperini/729294 and https://mathiasbynens.be/demo/url-regex
40 25
        $urlValidationRegex = '_^(?:(?:https?|ftp)://)(?:\S+(?::\S*)?@)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.168(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)*(?:\.(?:[a-z\x{00a1}-\x{ffff}]{2,})))(?::\d{2,5})?(?:/[^\s]*)?$_iuS';
41 25
        if (!preg_match($urlValidationRegex, $url)) {
42 2
            throw new InvalidUrlException(sprintf('The url: "%s" is not a valid url.', $url), 400);
43
        }
44 23
        $this->url = $url;
45
46 23
        return $this;
47
    }
48
49
    /**
50
     * @return string
51
     */
52 20
    public function getUrl()
53
    {
54 20
        return $this->url;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 19
    public function __toString()
61
    {
62 19
        return $this->getUrl();
63
    }
64
}
65