Conditions | 63 |
Paths | 185 |
Total Lines | 176 |
Lines | 101 |
Ratio | 57.39 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
59 | public static function replaceKeywords(array $params, array $tokens, Compiler $compiler) |
||
60 | { |
||
61 | $p = array(); |
||
62 | |||
63 | foreach($params as $k => $v) { |
||
64 | $v = (string)$v; |
||
65 | if (substr($v, 0, 1) === '"' || substr($v, 0, 1) === '\'') { |
||
66 | $vmod = strtolower(substr($v, 1, - 1)); |
||
67 | } else { |
||
68 | $vmod = strtolower($v); |
||
69 | } |
||
70 | switch ($vmod) { |
||
71 | |||
72 | View Code Duplication | case 'and': |
|
73 | if ($tokens[$k] === Compiler::T_UNQUOTED_STRING) { |
||
74 | $p[] = '&&'; |
||
75 | } else { |
||
76 | $p[] = $v; |
||
77 | } |
||
78 | break; |
||
79 | View Code Duplication | case 'or': |
|
80 | if ($tokens[$k] === Compiler::T_UNQUOTED_STRING) { |
||
81 | $p[] = '||'; |
||
82 | } else { |
||
83 | $p[] = $v; |
||
84 | } |
||
85 | break; |
||
86 | View Code Duplication | case 'xor': |
|
87 | if ($tokens[$k] === Compiler::T_UNQUOTED_STRING) { |
||
88 | $p[] = '^'; |
||
89 | } else { |
||
90 | $p[] = $v; |
||
91 | } |
||
92 | break; |
||
93 | View Code Duplication | case 'eq': |
|
94 | if ($tokens[$k] === Compiler::T_UNQUOTED_STRING) { |
||
95 | $p[] = '=='; |
||
96 | } else { |
||
97 | $p[] = $v; |
||
98 | } |
||
99 | break; |
||
100 | case 'ne': |
||
101 | View Code Duplication | case 'neq': |
|
102 | if ($tokens[$k] === Compiler::T_UNQUOTED_STRING) { |
||
103 | $p[] = '!='; |
||
104 | } else { |
||
105 | $p[] = $v; |
||
106 | } |
||
107 | break; |
||
108 | case 'gte': |
||
109 | View Code Duplication | case 'ge': |
|
110 | if ($tokens[$k] === Compiler::T_UNQUOTED_STRING) { |
||
111 | $p[] = '>='; |
||
112 | } else { |
||
113 | $p[] = $v; |
||
114 | } |
||
115 | break; |
||
116 | case 'lte': |
||
117 | View Code Duplication | case 'le': |
|
118 | if ($tokens[$k] === Compiler::T_UNQUOTED_STRING) { |
||
119 | $p[] = '<='; |
||
120 | } else { |
||
121 | $p[] = $v; |
||
122 | } |
||
123 | break; |
||
124 | View Code Duplication | case 'gt': |
|
125 | if ($tokens[$k] === Compiler::T_UNQUOTED_STRING) { |
||
126 | $p[] = '>'; |
||
127 | } else { |
||
128 | $p[] = $v; |
||
129 | } |
||
130 | break; |
||
131 | View Code Duplication | case 'lt': |
|
132 | if ($tokens[$k] === Compiler::T_UNQUOTED_STRING) { |
||
133 | $p[] = '<'; |
||
134 | } else { |
||
135 | $p[] = $v; |
||
136 | } |
||
137 | break; |
||
138 | View Code Duplication | case 'mod': |
|
139 | if ($tokens[$k] === Compiler::T_UNQUOTED_STRING) { |
||
140 | $p[] = '%'; |
||
141 | } else { |
||
142 | $p[] = $v; |
||
143 | } |
||
144 | break; |
||
145 | View Code Duplication | case 'not': |
|
146 | if ($tokens[$k] === Compiler::T_UNQUOTED_STRING) { |
||
147 | $p[] = '!'; |
||
148 | } else { |
||
149 | $p[] = $v; |
||
150 | } |
||
151 | break; |
||
152 | case '<>': |
||
153 | $p[] = '!='; |
||
154 | break; |
||
155 | case '==': |
||
156 | case '!=': |
||
157 | case '>=': |
||
158 | case '<=': |
||
159 | case '>': |
||
160 | case '<': |
||
161 | case '===': |
||
162 | case '!==': |
||
163 | case '%': |
||
164 | case '!': |
||
165 | case '^': |
||
166 | $p[] = $vmod; |
||
167 | break; |
||
168 | case 'is': |
||
169 | if ($tokens[$k] !== Compiler::T_UNQUOTED_STRING) { |
||
170 | $p[] = $v; |
||
171 | break; |
||
172 | } |
||
173 | if (isset($params[$k + 1]) && strtolower(trim($params[$k + 1], '"\'')) === 'not' && $tokens[$k + 1] === Compiler::T_UNQUOTED_STRING) { |
||
174 | $negate = true; |
||
175 | next($params); |
||
176 | } else { |
||
177 | $negate = false; |
||
178 | } |
||
179 | $ptr = 1 + (int)$negate; |
||
180 | if ($tokens[$k + $ptr] !== Compiler::T_UNQUOTED_STRING) { |
||
181 | break; |
||
182 | } |
||
183 | if (!isset($params[$k + $ptr])) { |
||
184 | $params[$k + $ptr] = ''; |
||
185 | } else { |
||
186 | $params[$k + $ptr] = trim($params[$k + $ptr], '"\''); |
||
187 | } |
||
188 | switch ($params[$k + $ptr]) { |
||
189 | |||
190 | case 'div': |
||
191 | if (isset($params[$k + $ptr + 1]) && strtolower(trim($params[$k + $ptr + 1], '"\'')) === 'by') { |
||
192 | $p[] = ' % ' . $params[$k + $ptr + 2] . ' ' . ($negate ? '!' : '=') . '== 0'; |
||
193 | next($params); |
||
194 | next($params); |
||
195 | next($params); |
||
196 | } else { |
||
197 | throw new CompilationException($compiler, 'If : Syntax error : syntax should be "if $a is [not] div by $b", found ' . $params[$k - 1] . ' is ' . ($negate ? 'not ' : '') . 'div ' . $params[$k + $ptr + 1] . ' ' . $params[$k + $ptr + 2]); |
||
198 | } |
||
199 | break; |
||
200 | View Code Duplication | case 'even': |
|
201 | $a = array_pop($p); |
||
202 | if (isset($params[$k + $ptr + 1]) && strtolower(trim($params[$k + $ptr + 1], '"\'')) === 'by') { |
||
203 | $b = $params[$k + $ptr + 2]; |
||
204 | $p[] = '(' . $a . ' / ' . $b . ') % 2 ' . ($negate ? '!' : '=') . '== 0'; |
||
205 | next($params); |
||
206 | next($params); |
||
207 | } else { |
||
208 | $p[] = $a . ' % 2 ' . ($negate ? '!' : '=') . '== 0'; |
||
209 | } |
||
210 | next($params); |
||
211 | break; |
||
212 | View Code Duplication | case 'odd': |
|
213 | $a = array_pop($p); |
||
214 | if (isset($params[$k + $ptr + 1]) && strtolower(trim($params[$k + $ptr + 1], '"\'')) === 'by') { |
||
215 | $b = $params[$k + $ptr + 2]; |
||
216 | $p[] = '(' . $a . ' / ' . $b . ') % 2 ' . ($negate ? '=' : '!') . '== 0'; |
||
217 | next($params); |
||
218 | next($params); |
||
219 | } else { |
||
220 | $p[] = $a . ' % 2 ' . ($negate ? '=' : '!') . '== 0'; |
||
221 | } |
||
222 | next($params); |
||
223 | break; |
||
224 | default: |
||
225 | throw new CompilationException($compiler, 'If : Syntax error : syntax should be "if $a is [not] (div|even|odd) [by $b]", found ' . $params[$k - 1] . ' is ' . $params[$k + $ptr + 1]); |
||
226 | } |
||
227 | break; |
||
228 | default: |
||
229 | $p[] = $v; |
||
230 | } |
||
231 | } |
||
232 | |||
233 | return $p; |
||
234 | } |
||
235 | |||
274 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.