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