Completed
Push — master ( a6d8dc...680f8e )
by Jan-Petter
03:57
created

Host   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 110
Duplicated Lines 7.27 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 19
c 2
b 1
f 0
lcom 1
cbo 1
dl 8
loc 110
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
D add() 0 28 10
B check() 0 22 4
A export() 0 4 2
A render() 8 8 2

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
namespace vipnytt\RobotsTxtParser\Parser\Directives;
3
4
use vipnytt\RobotsTxtParser\Parser\RobotsTxtInterface;
5
use vipnytt\RobotsTxtParser\Parser\UrlParser;
6
7
/**
8
 * Class Host
9
 *
10
 * @package vipnytt\RobotsTxtParser\Parser\Directives
11
 */
12
class Host implements DirectiveInterface, RobotsTxtInterface
13
{
14
    use UrlParser;
15
16
    /**
17
     * Directive
18
     */
19
    const DIRECTIVE = self::DIRECTIVE_HOST;
20
21
    /**
22
     * Host array
23
     * @var string[]
24
     */
25
    protected $array = [];
26
27
    /**
28
     * Host constructor.
29
     */
30
    public function __construct()
31
    {
32
    }
33
34
    /**
35
     * Add
36
     *
37
     * @param string $line
38
     * @return bool
39
     */
40
    public function add($line)
41
    {
42
        if (($parsed = parse_url(($line = $this->urlEncode(mb_strtolower($line))))) === false) {
43
            return false;
44
        }
45
        $line = isset($parsed['host']) ? $parsed['host'] : $parsed['path'];
46
        if (
47
            !$this->urlValidateHost($line) ||
48
            (
49
                isset($parsed['scheme']) &&
50
                !$this->urlValidateScheme($parsed['scheme'])
51
            )
52
        ) {
53
            return false;
54
        }
55
        $scheme = isset($parsed['scheme']) ? $parsed['scheme'] . '://' : '';
56
        $port = isset($parsed['port']) ? ':' . $parsed['port'] : '';
57
58
        $host = $scheme . $line . $port;
59
        if (
60
            $line !== $host ||
61
            in_array($host, $this->array)
62
        ) {
63
            return false;
64
        }
65
        $this->array[] = $line;
66
        return true;
67
    }
68
69
    /**
70
     * Check
71
     *
72
     * @param string $url
73
     * @return bool
74
     */
75
    public function check($url)
76
    {
77
        if (empty($this->array)) {
78
            return false;
79
        }
80
        $url = mb_strtolower($this->urlEncode($url));
81
        $parts = [
82
            'scheme' => parse_url($url, PHP_URL_SCHEME),
83
            'host' => parse_url($url, PHP_URL_HOST),
84
        ];
85
        $parts['port'] = is_int($port = parse_url($url, PHP_URL_PORT)) ? $port : getservbyname($parts['scheme'], 'tcp');
86
        $cases = [
87
            $parts['host'],
88
            $parts['host'] . ':' . $parts['port'],
89
            $parts['scheme'] . '://' . $parts['host'],
90
            $parts['scheme'] . '://' . $parts['host'] . ':' . $parts['port']
91
        ];
92
        if (in_array($this->array[0], $cases)) {
93
            return true;
94
        }
95
        return false;
96
    }
97
98
    /**
99
     * Export rules
100
     *
101
     * @return string[][]
102
     */
103
    public function export()
104
    {
105
        return empty($this->array) ? [] : [self::DIRECTIVE => $this->array];
106
    }
107
108
    /**
109
     * Render
110
     *
111
     * @return string[]
112
     */
113 View Code Duplication
    public function render()
0 ignored issues
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...
114
    {
115
        $result = [];
116
        foreach ($this->array as $value) {
117
            $result[] = self::DIRECTIVE . ': ' . $value;
118
        }
119
        return $result;
120
    }
121
}
122