JsFunctionsScanner::getFunctions()   F
last analyzed

Complexity

Conditions 44
Paths 217

Size

Total Lines 155
Code Lines 112

Duplication

Lines 32
Ratio 20.65 %

Importance

Changes 0
Metric Value
cc 44
eloc 112
nc 217
nop 1
dl 32
loc 155
rs 3.7733
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 Gettext\Utils;
4
5
class JsFunctionsScanner extends FunctionsScanner
6
{
7
    protected $code;
8
    protected $status = [];
9
10
    /**
11
     * Constructor.
12
     *
13
     * @param string $code The php code to scan
14
     */
15
    public function __construct($code)
16
    {
17
        $this->code = $code;
18
    }
19
20
    /**
21
     * {@inheritdoc}
22
     */
23
    public function getFunctions(array $constants = [])
24
    {
25
        $length = strlen($this->code);
26
        $line = 1;
27
        $buffer = '';
28
        $functions = [];
29
        $bufferFunctions = [];
30
        $char = null;
31
32
        for ($pos = 0; $pos < $length; ++$pos) {
33
            $prev = $char;
34
            $char = $this->code[$pos];
35
            $next = isset($this->code[$pos + 1]) ? $this->code[$pos + 1] : null;
36
37
            switch ($char) {
38
                case "\n":
39
                    ++$line;
40
41
                    if ($this->status('line-comment')) {
42
                        $this->upStatus();
43
                    }
44
                    break;
45
46
                case '/':
47
                    switch ($this->status()) {
48
                        case 'simple-quote':
49
                        case 'double-quote':
50
                        case 'line-comment':
51
                            break;
52
53
                        case 'block-comment':
54
                            if ($prev === '*') {
55
                                $this->upStatus();
56
                            }
57
                            break;
58
59
                        default:
60
                            if ($next === '/') {
61
                                $this->downStatus('line-comment');
62
                            } elseif ($next === '*') {
63
                                $this->downStatus('block-comment');
64
                            }
65
                            break;
66
                    }
67
                    break;
68
69 View Code Duplication
                case "'":
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
70
                    switch ($this->status()) {
71
                        case 'simple-quote':
72
                            $this->upStatus();
73
                            break;
74
75
                        case 'line-comment':
76
                        case 'block-comment':
77
                        case 'double-quote':
78
                            break;
79
80
                        default:
81
                            $this->downStatus('simple-quote');
82
                            break;
83
                    }
84
                    break;
85
86 View Code Duplication
                case '"':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
87
                    switch ($this->status()) {
88
                        case 'double-quote':
89
                            $this->upStatus();
90
                            break;
91
92
                        case 'line-comment':
93
                        case 'block-comment':
94
                        case 'simple-quote':
95
                            break;
96
97
                        default:
98
                            $this->downStatus('double-quote');
99
                            break;
100
                    }
101
                    break;
102
103
                case '(':
104
                    switch ($this->status()) {
105
                        case 'simple-quote':
106
                        case 'double-quote':
107
                        case 'line-comment':
108
                        case 'block-comment':
109
                        case 'line-comment':
110
                            break;
111
112
                        default:
113
                            if ($buffer && preg_match('/(\w+)$/', $buffer, $matches)) {
114
                                $this->downStatus('function');
115
                                array_unshift($bufferFunctions, [$matches[1], $line, []]);
116
                                $buffer = '';
117
                                continue 3;
118
                            }
119
                            break;
120
                    }
121
                    break;
122
123
                case ')':
124
                    switch ($this->status()) {
125
                        case 'function':
126
                            if (($argument = self::prepareArgument($buffer))) {
127
                                $bufferFunctions[0][2][] = $argument;
128
                            }
129
130
                            if (!empty($bufferFunctions)) {
131
                                $functions[] = array_shift($bufferFunctions);
132
                            }
133
134
                            $this->upStatus();
135
                            $buffer = '';
136
                            continue 3;
137
                    }
138
                    break;
139
140
                case ',':
141
                    switch ($this->status()) {
142
                        case 'function':
143
                            if (($argument = self::prepareArgument($buffer))) {
144
                                $bufferFunctions[0][2][] = $argument;
145
                            }
146
147
                            $buffer = '';
148
                            continue 3;
149
                    }
150
                    break;
151
152
                case ' ':
153
                case '\t':
154
                    switch ($this->status()) {
155
                        case 'double-quote':
156
                        case 'simple-quote':
157
                            break;
158
159
                        default:
160
                            continue 3;
161
                    }
162
                    break;
163
            }
164
165
            switch ($this->status()) {
166
                case 'line-comment':
167
                case 'block-comment':
168
                    break;
169
170
                default:
171
                    $buffer .= $char;
172
                    break;
173
            }
174
        }
175
176
        return $functions;
177
    }
178
179
    /**
180
     * Get the current context of the scan.
181
     *
182
     * @param null|string $match To check whether the current status is this value
183
     *
184
     * @return string|bool
185
     */
186
    protected function status($match = null)
187
    {
188
        $status = isset($this->status[0]) ? $this->status[0] : null;
189
190
        if ($match !== null) {
191
            return $status === $match;
192
        }
193
194
        return $status;
195
    }
196
197
    /**
198
     * Add a new status to the stack.
199
     *
200
     * @param string $status
201
     */
202
    protected function downStatus($status)
203
    {
204
        array_unshift($this->status, $status);
205
    }
206
207
    /**
208
     * Removes and return the current status.
209
     *
210
     * @return string|null
211
     */
212
    protected function upStatus()
213
    {
214
        return array_shift($this->status);
215
    }
216
217
    /**
218
     * Prepares the arguments found in functions.
219
     *
220
     * @param string $argument
221
     *
222
     * @return string
223
     */
224
    protected static function prepareArgument($argument)
225
    {
226
        if ($argument && ($argument[0] === '"' || $argument[0] === "'")) {
227
            if ($argument[0] === '"') {
228
                $argument = str_replace('\\"', '"', $argument);
229
            } else {
230
                $argument = str_replace("\\'", "'", $argument);
231
            }
232
233
            return substr($argument, 1, -1);
234
        }
235
    }
236
}
237