Passed
Push — master ( 3948f8...d3c224 )
by Dāvis
07:09
created

Lexer::tokenise()   C

Complexity

Conditions 11
Paths 11

Size

Total Lines 77
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 41
nc 11
nop 2
dl 0
loc 77
rs 5.3974
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Sludio\HelperBundle\Script\Utils;
4
5
class Lexer
6
{
7
    const CLASS_UPPERCASE_UNAMBIGUOUS = 1;
8
9
    private $logger;
0 ignored issues
show
introduced by
The private property $logger is not used, and could be removed.
Loading history...
10
11
    /**
12
     * Tokeniser explodes input into components describing the properties expressed in the pattern.
13
     *
14
     * @param string  $pattern
15
     * @param boolean $expand Augment token definition with the haystack of possible values.
16
     *
17
     * @return array
18
     */
19
    public function tokenise($pattern, $expand = false)
20
    {
21
        preg_match_all('
22
        /
23
                (?<class_U_explicit>\\\U)
24
                \{
25
                    (?<class_U_repetition>[0-9]+)
26
                \}
27
            |
28
                (?<class_U_implicit>\\\U)
29
            |
30
                \[
31
                    (?<range_token_explicit>[^]]+)
32
                \]
33
                \{
34
                    (?<range_repetition>[0-9]+)
35
                \}
36
            |
37
                \[
38
                    (?<range_token_implicit>[^]]+)
39
                \]
40
            |
41
                (?<literal_string>[^\\\[]+)
42
        /x
43
        ', $pattern, $matches, \PREG_SET_ORDER);
44
        $tokens = [];
45
        foreach ($matches as $match) {
46
47
            if (!empty($match['class_U_explicit'])) {
48
                $token = [
49
                    'type' => 'class',
50
                    'class' => static::CLASS_UPPERCASE_UNAMBIGUOUS,
51
                    'repetition' => (int)$match['class_U_repetition'],
52
                ];
53
                if ($expand) {
54
                    $token['haystack'] = 'ABCDEFGHKMNOPRSTUVWXYZ23456789';
55
                }
56
                $tokens[] = $token;
57
            } elseif (!empty($match['class_U_implicit'])) {
58
                $token = [
59
                    'type' => 'class',
60
                    'class' => static::CLASS_UPPERCASE_UNAMBIGUOUS,
61
                    'repetition' => 1,
62
                ];
63
                if ($expand) {
64
                    $token['haystack'] = 'ABCDEFGHKMNOPRSTUVWXYZ23456789';
65
                }
66
                $tokens[] = $token;
67
            } elseif (!empty($match['range_token_explicit'])) {
68
                $token = [
69
                    'type' => 'range',
70
                    'token' => $match['range_token_explicit'],
71
                    'repetition' => (int)$match['range_repetition'],
72
                ];
73
                if ($expand) {
74
                    $token['haystack'] = static::expandRange($match['range_token_explicit']);
75
                }
76
                $tokens[] = $token;
77
            } elseif (!empty($match['range_token_implicit'])) {
78
                $token = [
79
                    'type' => 'range',
80
                    'token' => $match['range_token_implicit'],
81
                    'repetition' => 1,
82
                ];
83
                if ($expand) {
84
                    $token['haystack'] = static::expandRange($match['range_token_implicit']);
85
                }
86
                $tokens[] = $token;
87
            } elseif (!empty($match['literal_string'])) {
88
                $tokens[] = [
89
                    'type' => 'literal',
90
                    'string' => $match['literal_string'],
91
                ];
92
            }
93
        }
94
95
        return $tokens;
96
    }
97
98
    /**
99
     * @param string $range_definition Set of characters defined individually, using range or both.
100
     *
101
     * @return string All characters that fit in the range.
102
     */
103
    static public function expandRange($range_definition)
104
    {
105
        $haystack = preg_replace_callback('/(?<from>.)\-(?<to>.)/', function ($e) {
106
            if (is_numeric($e['from']) || is_numeric($e['to'])) {
107
                $from = $e['from'];
108
                $to = $e['to'];
109
                if ($from > $to) {
110
                    throw new Exception\LogicException('Invalid range definition. Start greater than end.');
0 ignored issues
show
Bug introduced by
The type Sludio\HelperBundle\Scri...xception\LogicException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
111
                }
112
                $haystack = '';
113
                for ($from; $from <= $to; $from++) {
114
                    $haystack .= $from;
115
                }
116
            } else {
117
                $from = ord($e['from']);
118
                $to = ord($e['to']);
119
                if ($from > $to) {
120
                    throw new Exception\LogicException('Invalid range definition. Start greater than end.');
121
                }
122
                $haystack = '';
123
                for ($from; $from <= $to; $from++) {
124
                    $haystack .= chr($from);
125
                }
126
            }
127
128
            return $haystack;
129
        }, $range_definition);
130
        $haystack = implode('', array_unique(str_split($haystack)));
131
132
        return $haystack;
133
    }
134
}
135