Completed
Branch 2.0-dev (d250b8)
by Jan-Petter
03:02
created

CleanParamParser   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 90
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
c 1
b 0
f 0
lcom 1
cbo 2
dl 90
loc 90
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 3 3 1
A add() 12 12 3
B check() 15 15 5
A export() 4 4 2
A render() 10 10 3

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\UrlParser;
5
use vipnytt\RobotsTxtParser\RobotsTxtInterface;
6
7
/**
8
 * Class CleanParamParser
9
 *
10
 * @package vipnytt\RobotsTxtParser\Parser\Directives
11
 */
12 View Code Duplication
class CleanParamParser implements ParserInterface, RobotsTxtInterface
0 ignored issues
show
Duplication introduced by
This class 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...
13
{
14
    use DirectiveParserCommons;
15
    use UrlParser;
16
17
    /**
18
     * Directive
19
     */
20
    const DIRECTIVE = self::DIRECTIVE_CLEAN_PARAM;
21
22
    /**
23
     * Clean-param array
24
     * @var array
25
     */
26
    private $array = [];
27
28
    /**
29
     * CleanParam constructor.
30
     */
31
    public function __construct()
32
    {
33
    }
34
35
    /**
36
     * Add
37
     *
38
     * @param string $line
39
     * @return bool
40
     */
41
    public function add($line)
42
    {
43
        // split into parameter and path
44
        $array = array_map('trim', mb_split('\s+', $line, 2));
45
        // strip any invalid characters from path prefix
46
        $path = isset($array[1]) ? $this->urlEncode(preg_replace('/[^A-Za-z0-9\.-\/\*\_]/', '', $array[1])) : "/";
47
        $param = array_map('trim', mb_split('&', $array[0]));
48
        foreach ($param as $key) {
49
            $this->array[$key][] = $path;
50
        }
51
        return true;
52
    }
53
54
    /**
55
     * Check
56
     *
57
     * @param  string $path
58
     * @return bool
59
     */
60
    public function check($path)
61
    {
62
        foreach ($this->array as $param => $paths) {
63
            if (
64
                (
65
                    mb_stripos($path, "?$param=") ||
66
                    mb_stripos($path, "&$param=")
67
                ) &&
68
                $this->checkPath($path, $paths)
69
            ) {
70
                return true;
71
            }
72
        }
73
        return false;
74
    }
75
76
    /**
77
     * Export rules
78
     *
79
     * @return string[][][]
80
     */
81
    public function export()
82
    {
83
        return empty($this->array) ? [] : [self::DIRECTIVE => $this->array];
84
    }
85
86
    /**
87
     * Render
88
     *
89
     * @return string[]
90
     */
91
    public function render()
92
    {
93
        $result = [];
94
        foreach ($this->array as $param => $paths) {
95
            foreach ($paths as $path) {
96
                $result[] = self::DIRECTIVE . ':' . $param . ' ' . $path;
97
            }
98
        }
99
        return $result;
100
    }
101
}
102