Conditions | 23 |
Paths | 1952 |
Total Lines | 122 |
Code Lines | 76 |
Lines | 23 |
Ratio | 18.85 % |
Changes | 4 | ||
Bugs | 1 | 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 |
||
189 | public function runTests($test_obj = null) |
||
190 | { |
||
191 | if (!$test_obj) { |
||
192 | $test_obj = $this; |
||
193 | } |
||
194 | |||
195 | $tested = array(); |
||
196 | $data = array(); |
||
197 | foreach (get_class_methods($test_obj) as $method) { |
||
198 | if (strpos($method, 'test_') === 0) { |
||
199 | $m = substr($method, 5); |
||
200 | } elseif (strpos($method, 'prepare_') === 0) { |
||
201 | $m = substr($method, 8); |
||
202 | } else { |
||
203 | continue; |
||
204 | } |
||
205 | |||
206 | if (isset($_GET['testonly']) && 'test_'.$_GET['testonly'] != $method) { |
||
207 | continue; |
||
208 | } |
||
209 | |||
210 | // Do not retest same function even if it has both prepare and test |
||
211 | if ($tested[$m]) { |
||
212 | continue; |
||
213 | } |
||
214 | $tested[$m] = true; |
||
215 | |||
216 | // Row contains test result data |
||
217 | $row = array('name' => $m, 'id' => $m); |
||
218 | |||
219 | foreach ($this->variances as $key => $vari) { |
||
220 | if (is_numeric($key)) { |
||
221 | $key = $vari; |
||
222 | } |
||
223 | |||
224 | try { |
||
225 | // Input is a result of preparation function |
||
226 | if (method_exists($test_obj, 'prepare_'.$m)) { |
||
227 | $input = $test_obj->{'prepare_'.$m}($vari, $method); |
||
228 | } else { |
||
229 | if ($test_obj->hasMethod('prepare')) { |
||
230 | $input = $test_obj->prepare($vari, $method); |
||
231 | } else { |
||
232 | $input = null; |
||
233 | } |
||
234 | } |
||
235 | } catch (Exception $e) { |
||
236 | View Code Duplication | if ($e instanceof Exception_SkipTests) { |
|
237 | $this->grid->destroy(); |
||
238 | /** @type View_Error $v_error */ |
||
239 | $v_error = $this->add('View_Error'); |
||
240 | $v_error->set('Skipping all tests: '.$e->getMessage()); |
||
241 | |||
242 | return; |
||
243 | } |
||
244 | } |
||
245 | |||
246 | $this->input = $input; |
||
247 | |||
248 | $test_func = method_exists($test_obj, 'test_'.$m) ? |
||
249 | 'test_'.$m : 'test'; |
||
250 | |||
251 | // Test speed |
||
252 | $me = memory_get_peak_usage(); |
||
253 | $ms = microtime(true); |
||
254 | $this->cnt = 0; |
||
255 | declare (ticks = 1); |
||
256 | register_tick_function(array($this, 'ticker')); |
||
257 | try { |
||
258 | //$result=$test_obj->$test_func($input[0],$input[1],$input[2]); |
||
259 | $result = $this->executeTest($test_obj, $test_func, $input); |
||
260 | } catch (Exception $e) { |
||
261 | View Code Duplication | if ($e instanceof Exception_SkipTests) { |
|
262 | $this->grid->destroy(); |
||
263 | /** @type View_Error $v_error */ |
||
264 | $v_error = $this->add('View_Error'); |
||
265 | $v_error->set('Skipping all tests: '.$e->getMessage()); |
||
266 | } |
||
267 | |||
268 | View Code Duplication | if ($_GET['tester_details'] == $row['name'] && $_GET['vari'] == $vari) { |
|
269 | throw $e; |
||
270 | } |
||
271 | |||
272 | $result = 'Exception: '.($e instanceof BaseException ? $e->getText() : $e->getMessage()); |
||
273 | |||
274 | /** @type P $ll */ |
||
275 | $ll = $this->add('P', $row['name']); |
||
276 | /** @type View $v */ |
||
277 | $v = $ll->add('View'); |
||
278 | $v->setElement('a') |
||
279 | ->setAttr('href', '#') |
||
280 | ->set('More details') |
||
281 | ->js('click')->univ()->frameURL( |
||
282 | 'Exception Details for test '.$row['name'], |
||
283 | $this->app->url(null, array('tester_details' => $row['name'], 'vari' => $vari)) |
||
284 | ); |
||
285 | |||
286 | $result .= $ll->getHTML(); |
||
287 | } |
||
288 | $ms = microtime(true) - $ms; |
||
289 | $me = ($mend = memory_get_peak_usage()) - $me; |
||
290 | unregister_tick_function(array($this, 'ticker')); |
||
291 | |||
292 | $row[$key.'_inf'] = 'Ticks: '.($this->cnt * 1).'<br/>Memory: '.$me; |
||
293 | |||
294 | $result = $this->formatResult($row, $key, $result); |
||
295 | |||
296 | $k = $key.'_'.$row['name']; |
||
297 | View Code Duplication | if ($this->proper_responses[$k] == $result && isset($this->proper_responses[$k])) { |
|
298 | $row[$key.'_res'] = '<font color="green">PASS</font><br/>'.htmlspecialchars($result); |
||
299 | } elseif ($this->proper_responses[$k]) { |
||
300 | $row[$key.'_res'] = '<font color="red">'.htmlspecialchars($result).'</font><br/>'. |
||
301 | var_export($this->proper_responses[$k], true); |
||
302 | } |
||
303 | |||
304 | $this->responses[] = '"'.$k.'"'.'=>'.var_export($result, true); |
||
305 | } |
||
306 | |||
307 | $data[] = $row; |
||
308 | } |
||
309 | $this->grid->setSource($data); |
||
310 | } |
||
311 | public function formatResult(&$row, $key, $result) |
||
333 |
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: