JsFunctionsScanner   C
last analyzed

Complexity

Total Complexity 55

Size/Duplication

Total Lines 232
Duplicated Lines 13.79 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 32
loc 232
rs 6.8
c 0
b 0
f 0
wmc 55
lcom 1
cbo 1

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
F getFunctions() 32 155 44
A status() 0 10 3
A downStatus() 0 4 1
A upStatus() 0 4 1
B prepareArgument() 0 12 5

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like JsFunctionsScanner often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use JsFunctionsScanner, and based on these observations, apply Extract Interface, too.

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