Completed
Push — master ( 0cbd7a...258693 )
by Jan-Petter
04:51
created

CleanParamParserCore::renderCompressed()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 14
rs 9.2
cc 4
eloc 9
nc 5
nop 1
1
<?php
2
/**
3
 * vipnytt/RobotsTxtParser
4
 *
5
 * @link https://github.com/VIPnytt/RobotsTxtParser
6
 * @license https://github.com/VIPnytt/RobotsTxtParser/blob/master/LICENSE The MIT License (MIT)
7
 */
8
9
namespace vipnytt\RobotsTxtParser\Parser\Directives;
10
11
use vipnytt\RobotsTxtParser\Handler\RenderHandler;
12
use vipnytt\RobotsTxtParser\Parser\UriParser;
13
use vipnytt\RobotsTxtParser\RobotsTxtInterface;
14
15
/**
16
 * Class CleanParamParserCore
17
 *
18
 * @package vipnytt\RobotsTxtParser\Parser\Directives
19
 */
20
abstract class CleanParamParserCore implements ParserInterface, RobotsTxtInterface
21
{
22
    /**
23
     * Clean-param array
24
     * @var string[][]
25
     */
26
    protected $cleanParam = [];
27
28
    /**
29
     * CleanParamParserCore 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 = '/';
47
        if (isset($array[1])) {
48
            $uriParser = new UriParser(preg_replace('/[^A-Za-z0-9\.-\/\*\_]/', '', $array[1]));
49
            $path = $uriParser->encode();
50
        }
51
        $param = array_map('trim', explode('&', $array[0]));
52
        foreach ($param as $key) {
53
            $this->cleanParam[$key][] = $path;
54
        }
55
        return true;
56
    }
57
58
    /**
59
     * Render
60
     *
61
     * @param RenderHandler $handler
62
     * @return bool
63
     */
64
    public function render(RenderHandler $handler)
65
    {
66
        ksort($this->cleanParam);
67
        return $handler->getMode() >= 3 ? $this->renderExtensive($handler) : $this->renderCompressed($handler);
68
    }
69
70
    /**
71
     * Render extensive
72
     *
73
     * @param RenderHandler $handler
74
     * @return bool
75
     */
76
    private function renderExtensive(RenderHandler $handler)
77
    {
78
        foreach ($this->cleanParam as $param => $paths) {
79
            foreach ($paths as $path) {
80
                $handler->add(self::DIRECTIVE_CLEAN_PARAM, $param . ' ' . $path);
81
            }
82
        }
83
        return true;
84
    }
85
86
    /**
87
     * Render compressed
88
     *
89
     * @param RenderHandler $handler
90
     * @return bool
91
     */
92
    private function renderCompressed(RenderHandler $handler)
93
    {
94
        $pair = $this->cleanParam;
95
        while (!empty($pair)) {
96
            $equalParams = array_keys($pair, current($pair));
97
            foreach (current($pair) as $path) {
0 ignored issues
show
Bug introduced by
The expression current($pair) of type array<integer,string>|false is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
98
                $handler->add(self::DIRECTIVE_CLEAN_PARAM, implode('&', $equalParams) . ' ' . $path);
99
            }
100
            foreach ($equalParams as $param) {
101
                unset($pair[$param]);
102
            }
103
        }
104
        return true;
105
    }
106
}
107