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

CleanParamParser::check()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 15
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 15
loc 15
rs 8.8571
cc 5
eloc 9
nc 3
nop 1
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