Conditions | 27 |
Paths | 2448 |
Total Lines | 146 |
Code Lines | 92 |
Lines | 36 |
Ratio | 24.66 % |
Changes | 4 | ||
Bugs | 2 | Features | 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 |
||
195 | public function runTests($test_obj = null) |
||
196 | { |
||
197 | if (!$test_obj) { |
||
198 | $test_obj = $this; |
||
199 | } else { |
||
200 | $this->proper_responses = @$test_obj->proper_responses; |
||
201 | |||
202 | if ($test_obj instanceof AbstractObject) { |
||
203 | $this->add($test_obj); |
||
204 | } |
||
205 | } |
||
206 | |||
207 | $tested = array(); |
||
208 | $data = array(); |
||
209 | foreach (get_class_methods($test_obj) as $method) { |
||
210 | $m = ''; |
||
211 | if (strpos($method, 'test_') === 0) { |
||
212 | $m = substr($method, 5); |
||
213 | } elseif (strpos($method, 'prepare_') === 0) { |
||
214 | $m = substr($method, 8); |
||
215 | } else { |
||
216 | continue; |
||
217 | } |
||
218 | |||
219 | if (isset($_GET['testonly']) && 'test_'.$_GET['testonly'] != $method) { |
||
220 | continue; |
||
221 | } |
||
222 | |||
223 | // Do not retest same function even if it has both prepare and test |
||
224 | if ($tested[$m]) { |
||
225 | continue; |
||
226 | } |
||
227 | $tested[$m] = true; |
||
228 | |||
229 | // Row contains test result data |
||
230 | $row = array('name' => $m, 'id' => $m); |
||
231 | |||
232 | foreach ($this->variances as $key => $vari) { |
||
233 | if (is_numeric($key)) { |
||
234 | $key = $vari; |
||
235 | } |
||
236 | |||
237 | try { |
||
238 | // Input is a result of preparation function |
||
239 | if (method_exists($test_obj, 'prepare_'.$m)) { |
||
240 | $input = $test_obj->{'prepare_'.$m}($vari, $method); |
||
241 | } else { |
||
242 | if (($test_obj instanceof AbstractObject && $test_obj->hasMethod('prepare')) |
||
243 | || method_exists($test_obj, 'prepare') |
||
244 | ) { |
||
245 | $input = $test_obj->prepare($vari, $method); |
||
246 | } else { |
||
247 | $input = null; |
||
248 | } |
||
249 | } |
||
250 | } catch (Exception $e) { |
||
251 | View Code Duplication | if ($e instanceof Exception_SkipTests) { |
|
252 | $this->grid->destroy(); |
||
253 | |||
254 | /** @type View_Error $v */ |
||
255 | $v = $this->add('View_Error'); |
||
256 | $v->set('Skipping all tests: '.$e->getMessage()); |
||
257 | |||
258 | return; |
||
259 | } |
||
260 | throw $e; |
||
261 | } |
||
262 | |||
263 | $this->input = $input; |
||
264 | |||
265 | $test_func = method_exists($test_obj, 'test_'.$m) ? |
||
266 | 'test_'.$m : 'test'; |
||
267 | |||
268 | // Test speed |
||
269 | $me = memory_get_peak_usage(); |
||
270 | $ms = microtime(true); |
||
271 | $this->cnt = 0; |
||
272 | declare (ticks = 1); |
||
273 | register_tick_function(array($this, 'ticker')); |
||
274 | try { |
||
275 | //$result=$test_obj->$test_func($input[0],$input[1],$input[2]); |
||
276 | $result = $this->executeTest($test_obj, $test_func, $input); |
||
277 | } catch (Exception $e) { |
||
278 | View Code Duplication | if ($e instanceof Exception_SkipTests) { |
|
279 | $this->grid->destroy(); |
||
280 | |||
281 | /** @type View_Error $v */ |
||
282 | $v = $this->add('View_Error'); |
||
283 | $v->set('Skipping all tests: '.$e->getMessage()); |
||
284 | } |
||
285 | |||
286 | View Code Duplication | if ($_GET['tester_details'] == $row['name'] && $_GET['vari'] == $vari) { |
|
287 | throw $e; |
||
288 | } |
||
289 | |||
290 | $result = 'Exception: '.($e instanceof BaseException ? $e->getText() : $e->getMessage()); |
||
291 | |||
292 | /** @type P $ll */ |
||
293 | $ll = $this->add('P', $row['name']); |
||
294 | |||
295 | /** @type View $v */ |
||
296 | $v = $ll->add('View'); |
||
297 | $v->setElement('a') |
||
298 | ->setAttr('href', '#') |
||
299 | ->set('More details') |
||
300 | ->js('click')->univ()->frameURL( |
||
301 | 'Exception Details for test '.$row['name'], |
||
302 | $this->app->url(null, array('tester_details' => $row['name'], 'vari' => $vari)) |
||
303 | ); |
||
304 | |||
305 | $result .= $ll->getHTML(); |
||
306 | } |
||
307 | $ms = microtime(true) - $ms; |
||
308 | $me = ($mend = memory_get_peak_usage()) - $me; |
||
309 | unregister_tick_function(array($this, 'ticker')); |
||
310 | |||
311 | $row[$key.'_inf'] = 'Ticks: '.($this->cnt * 1).'<br/>Memory: '.$me; |
||
312 | |||
313 | $result = $this->formatResult($row, $key, $result); |
||
314 | |||
315 | $k = $key.'_'.$row['name']; |
||
316 | View Code Duplication | if ($this->proper_responses[$k] == $result && isset($this->proper_responses[$k])) { |
|
317 | $row[$key.'_res'] = '<font color="green">PASS</font><br/>'.htmlspecialchars($result); |
||
318 | } elseif (isset($this->proper_responses[$k])) { |
||
319 | $row[$key.'_res'] = '<font color="red">'.htmlspecialchars($result).'</font><br/>'. |
||
320 | var_export($this->proper_responses[$k], true); |
||
321 | } |
||
322 | |||
323 | $this->responses[] = '"'.$k.'"'.'=>'.var_export($result, true); |
||
324 | } |
||
325 | |||
326 | $data[] = $row; |
||
327 | } |
||
328 | $this->grid->setSource($data); |
||
329 | View Code Duplication | if (!$_GET['testonly']) { |
|
330 | $f = $this->form; |
||
331 | $ff = $f->addField('Text', 'responses'); |
||
332 | $this->responses = |
||
333 | ' public $proper_responses=array( |
||
334 | '.implode(', |
||
335 | ', $this->responses).' |
||
336 | );'; |
||
337 | $ff->set($this->responses); |
||
338 | $ff->js('click')->select(); |
||
339 | } |
||
340 | } |
||
341 | |||
365 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: