Conditions | 12 |
Paths | 8 |
Total Lines | 53 |
Lines | 0 |
Ratio | 0 % |
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 |
||
16 | public static function debug_object($obj) |
||
17 | { |
||
18 | $html = ' |
||
19 | <h2>'.$obj->ClassName.'</h2><ul>'; |
||
20 | $fields = Config::inst()->get($obj->ClassName, 'db', Config::INHERITED); |
||
21 | |||
22 | //db |
||
23 | if (count($fields)) { |
||
24 | foreach ($fields as $key => $type) { |
||
|
|||
25 | $value = self::cleanup_value($type, $obj->$key); |
||
26 | $html .= "<li><b>$key ($type):</b> ".$value.'</li>'; |
||
27 | } |
||
28 | } |
||
29 | |||
30 | //casted variables |
||
31 | $fields = Config::inst()->get($obj->ClassName, 'casting', Config::FIRST_SET); |
||
32 | if (count($fields)) { |
||
33 | foreach ($fields as $key => $type) { |
||
34 | $method = $key; |
||
35 | if ($obj->hasMethod($method)) { |
||
36 | $value = $obj->$method(); |
||
37 | } else { |
||
38 | $method = 'get'.$key; |
||
39 | if ($obj->hasMethod($method)) { |
||
40 | $value = $obj->$method(); |
||
41 | } else { |
||
42 | $value = $obj->$key; |
||
43 | } |
||
44 | } |
||
45 | $value = self::cleanup_value($type, $value); |
||
46 | $html .= "<li><b>$key ($type):</b> ".$value.'</li>'; |
||
47 | } |
||
48 | } |
||
49 | |||
50 | //has_one |
||
51 | $fields = Config::inst()->get($obj->ClassName, 'has_one', Config::FIRST_SET); |
||
52 | if (count($fields)) { |
||
53 | foreach ($fields as $key => $type) { |
||
54 | $value = ''; |
||
55 | $field = $key.'ID'; |
||
56 | if ($object = $obj->$key()) { |
||
57 | if ($object && $object->exists()) { |
||
58 | $value = ', '.$object->getTitle(); |
||
59 | } |
||
60 | } |
||
61 | $html .= "<li><b>$key ($type):</b> ".$obj->$field.$value.' </li>'; |
||
62 | } |
||
63 | } |
||
64 | //to do: has_many and many_many |
||
65 | $html .= '</ul>'; |
||
66 | |||
67 | return $html; |
||
68 | } |
||
69 | |||
87 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.