Completed
Push — master ( 706ec6...902e66 )
by Jan-Petter
04:48
created

CleanParamClient::detectWithCommon()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 9
rs 9.6666
cc 1
eloc 6
nc 1
nop 2
1
<?php
2
namespace vipnytt\RobotsTxtParser\Client\Directives;
3
4
/**
5
 * Class CleanParamClient
6
 *
7
 * @see https://github.com/VIPnytt/RobotsTxtParser/blob/master/docs/methods/CleanParamClient.md for documentation
8
 * @package vipnytt\RobotsTxtParser\Client\Directives
9
 */
10
class CleanParamClient implements ClientInterface
11
{
12
    use DirectiveClientCommons;
13
14
    /**
15
     * Common dynamic uri parameters
16
     * @var string
17
     */
18
    protected $commonParam = [
19
        'popup',
20
        'ref',
21
        'token'
22
    ];
23
24
    /**
25
     * Clean-param
26
     * @var string[][]
27
     */
28
    private $cleanParam = [];
29
30
    /**
31
     * CleanParamClient constructor.
32
     *
33
     * @param string[][] $cleanParam
34
     */
35
    public function __construct(array $cleanParam)
36
    {
37
        $this->cleanParam = $cleanParam;
38
    }
39
40
    /**
41
     * Has robots.txt defined dynamic or common dynamic parameters check
42
     *
43
     * @param string $uri
44
     * @param string[] $customParam
45
     * @return string[]
46
     */
47
    public function detectWithCommon($uri, array $customParam = [])
48
    {
49
        $pairs = array_merge_recursive(
50
            $this->cleanParam,
51
            $this->appendPath($this->commonParam),
0 ignored issues
show
Documentation introduced by
$this->commonParam is of type string, but the function expects a array<integer,string>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
52
            $this->appendPath($customParam)
53
        );
54
        return $this->parse($uri, $pairs);
55
    }
56
57
    /**
58
     * Convert param list to an valid Clean-param list
59
     *
60
     * @param string[] $parameters
61
     * @return array
62
     */
63
    private function appendPath(array $parameters)
64
    {
65
        $result = [];
66
        foreach ($parameters as $parameter) {
67
            $result[$parameter] = ['/'];
68
        }
69
        return $result;
70
    }
71
72
    /**
73
     * Parse uri and return detected parameters
74
     *
75
     * @param string $uri
76
     * @param array $pairs
77
     * @return string[]
78
     */
79
    private function parse($uri, array $pairs)
80
    {
81
        $result = [];
82
        foreach ($pairs as $param => $paths) {
83
            if (
84
                (
85
                    strpos($uri, "?$param=") ||
86
                    strpos($uri, "&$param=")
87
                ) &&
88
                $this->checkPaths($uri, $paths)
89
            ) {
90
                $result[] = $param;
91
            }
92
        }
93
        sort($result);
94
        return $result;
95
    }
96
97
    /**
98
     * Detect dynamic parameters
99
     *
100
     * @param  string $uri
101
     * @return string[]
102
     */
103
    public function detect($uri)
104
    {
105
        return $this->parse($uri, $this->cleanParam);
106
    }
107
108
    /**
109
     * Export
110
     *
111
     * @return string[][]
112
     */
113
    public function export()
114
    {
115
        return $this->cleanParam;
116
    }
117
}
118