Total Complexity | 67 |
Total Lines | 231 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like PluginIf 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.
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 PluginIf, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
42 | class PluginIf extends BlockPlugin implements ICompilableBlock, IElseable |
||
43 | { |
||
44 | /** |
||
45 | * @param array $rest |
||
46 | */ |
||
47 | public function init(array $rest) |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * @param array $params |
||
53 | * @param array $tokens |
||
54 | * @param Compiler $compiler |
||
55 | * |
||
56 | * @return array |
||
57 | * @throws CompilationException |
||
58 | */ |
||
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 | case 'and': |
||
74 | if ($tokens[$k] === Compiler::T_UNQUOTED_STRING) { |
||
75 | $p[] = '&&'; |
||
76 | } else { |
||
77 | $p[] = $v; |
||
78 | } |
||
79 | break; |
||
80 | case 'or': |
||
81 | if ($tokens[$k] === Compiler::T_UNQUOTED_STRING) { |
||
82 | $p[] = '||'; |
||
83 | } else { |
||
84 | $p[] = $v; |
||
85 | } |
||
86 | break; |
||
87 | case 'xor': |
||
88 | if ($tokens[$k] === Compiler::T_UNQUOTED_STRING) { |
||
89 | $p[] = '^'; |
||
90 | } else { |
||
91 | $p[] = $v; |
||
92 | } |
||
93 | break; |
||
94 | case 'eq': |
||
95 | if ($tokens[$k] === Compiler::T_UNQUOTED_STRING) { |
||
96 | $p[] = '=='; |
||
97 | } else { |
||
98 | $p[] = $v; |
||
99 | } |
||
100 | break; |
||
101 | case 'ne': |
||
102 | case 'neq': |
||
103 | if ($tokens[$k] === Compiler::T_UNQUOTED_STRING) { |
||
104 | $p[] = '!='; |
||
105 | } else { |
||
106 | $p[] = $v; |
||
107 | } |
||
108 | break; |
||
109 | case 'gte': |
||
110 | case 'ge': |
||
111 | if ($tokens[$k] === Compiler::T_UNQUOTED_STRING) { |
||
112 | $p[] = '>='; |
||
113 | } else { |
||
114 | $p[] = $v; |
||
115 | } |
||
116 | break; |
||
117 | case 'lte': |
||
118 | case 'le': |
||
119 | if ($tokens[$k] === Compiler::T_UNQUOTED_STRING) { |
||
120 | $p[] = '<='; |
||
121 | } else { |
||
122 | $p[] = $v; |
||
123 | } |
||
124 | break; |
||
125 | case 'gt': |
||
126 | if ($tokens[$k] === Compiler::T_UNQUOTED_STRING) { |
||
127 | $p[] = '>'; |
||
128 | } else { |
||
129 | $p[] = $v; |
||
130 | } |
||
131 | break; |
||
132 | case 'lt': |
||
133 | if ($tokens[$k] === Compiler::T_UNQUOTED_STRING) { |
||
134 | $p[] = '<'; |
||
135 | } else { |
||
136 | $p[] = $v; |
||
137 | } |
||
138 | break; |
||
139 | case 'mod': |
||
140 | if ($tokens[$k] === Compiler::T_UNQUOTED_STRING) { |
||
141 | $p[] = '%'; |
||
142 | } else { |
||
143 | $p[] = $v; |
||
144 | } |
||
145 | break; |
||
146 | 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 | 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 | 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 | |||
237 | /** |
||
238 | * @param Compiler $compiler |
||
239 | * @param array $params |
||
240 | * @param string $prepend |
||
241 | * @param string $append |
||
242 | * @param string $type |
||
243 | * |
||
244 | * @return string |
||
245 | */ |
||
246 | public static function preProcessing(Compiler $compiler, array $params, $prepend, $append, $type) |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * @param Compiler $compiler |
||
253 | * @param array $params |
||
254 | * @param string $prepend |
||
255 | * @param string $append |
||
256 | * @param string $content |
||
257 | * |
||
258 | * @return string |
||
259 | */ |
||
260 | public static function postProcessing(Compiler $compiler, array $params, $prepend, $append, $content) |
||
275 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.