Completed
Push — master ( 705095...78cb27 )
by Jan-Petter
04:57
created

Toolbox   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
c 1
b 0
f 0
lcom 0
cbo 1
dl 0
loc 88
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B checkPath() 0 35 6
A generateRulePair() 0 19 4
A validateDirective() 0 7 2
1
<?php
2
namespace vipnytt\RobotsTxtParser\Parser;
3
4
use vipnytt\RobotsTxtParser\Exceptions\ParserException;
5
6
/**
7
 * Trait Toolbox
8
 *
9
 * @package vipnytt\RobotsTxtParser\Parser
10
 */
11
trait Toolbox
12
{
13
    /**
14
     * Check basic rule
15
     *
16
     * @param string $path
17
     * @param array $paths
18
     * @return bool
19
     */
20
    protected function checkPath($path, $paths)
21
    {
22
        foreach ($paths as $rule) {
23
            $escape = ['?' => '\?', '.' => '\.', '*' => '.*'];
24
            foreach ($escape as $search => $replace) {
25
                /**
26
                 * str_replace needs to be replaced by something better, multi-byte safe
27
                 */
28
                $rule = str_replace($search, $replace, $rule);
29
            }
30
            /**
31
             * Warning: preg_match need to be replaced
32
             *
33
             * Bug report
34
             * @link https://github.com/t1gor/Robots.txt-Parser-Class/issues/62
35
             *
36
             * An robots.txt parser, where a bug-fix is planned
37
             * @link https://github.com/diggin/Diggin_RobotRules
38
             *
39
             * The solution?
40
             * PHP PEG (parsing expression grammar)
41
             * @link https://github.com/hafriedlander/php-peg
42
             */
43
            if (preg_match('~' . $rule . '~', $path)) {
44
                if (mb_stripos($rule, '$') !== false) {
45
                    if (mb_strlen($rule) - 1 == mb_strlen($path)) {
46
                        return true;
47
                    }
48
                } else {
49
                    return true;
50
                }
51
            }
52
        }
53
        return false;
54
    }
55
56
    /**
57
     * Generate directive/rule pair
58
     *
59
     * @param string $line
60
     * @param array $whiteList
61
     * @return array|false
62
     */
63
    protected function generateRulePair($line, $whiteList)
64
    {
65
        $whiteList = array_map('mb_strtolower', $whiteList);
66
        // Split by directive and rule
67
        $pair = array_map('trim', mb_split(':', $line, 2));
68
        // Check if the line contains a rule
69
        if (
70
            empty($pair[1]) ||
71
            empty($pair[0]) ||
72
            !in_array(($pair[0] = mb_strtolower($pair[0])), $whiteList)
73
        ) {
74
            // Line does not contain any supported directive
75
            return false;
76
        }
77
        return [
78
            'directive' => $pair[0],
79
            'value' => $pair[1],
80
        ];
81
    }
82
83
    /**
84
     * Validate directive
85
     *
86
     * @param $directive
87
     * @param $directives
88
     * @return string
89
     * @throws ParserException
90
     */
91
    protected function validateDirective($directive, $directives)
92
    {
93
        if (!in_array($directive, $directives, true)) {
94
            throw new ParserException('Directive is not allowed here');
95
        }
96
        return mb_strtolower($directive);
97
    }
98
}
99