| Conditions | 10 |
| Paths | 30 |
| Total Lines | 35 |
| Code Lines | 24 |
| Lines | 11 |
| Ratio | 31.43 % |
| 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 |
||
| 67 | protected function _format($object) |
||
| 68 | { |
||
| 69 | $value = null; |
||
| 70 | if (is_array($object)) { |
||
| 71 | if (isset($object[$this->field])) { |
||
| 72 | $value = $object[$this->field]; |
||
| 73 | } |
||
| 74 | } elseif (is_object($object)) { |
||
| 75 | $funcPrefix = array( |
||
| 76 | 'get', |
||
| 77 | 'is', |
||
| 78 | 'has', |
||
| 79 | ); |
||
| 80 | foreach ($funcPrefix as $prefix) { |
||
| 81 | $methodName = $prefix.$this->field; |
||
| 82 | if (method_exists($object, $methodName)) { |
||
| 83 | $value = $object->$methodName(); |
||
| 84 | break; |
||
| 85 | } |
||
| 86 | } |
||
| 87 | } |
||
| 88 | View Code Duplication | if (is_object($value)) { |
|
| 89 | if ($value instanceof \DateTime) { |
||
| 90 | return $value->format(\DateTime::ISO8601); |
||
| 91 | } |
||
| 92 | |||
| 93 | return 'object: '.get_class($value); |
||
| 94 | } elseif (is_scalar($value)) { |
||
| 95 | return $value; |
||
| 96 | } elseif (is_array($value)) { |
||
| 97 | return 'array: '.print_r($value, true); |
||
| 98 | } |
||
| 99 | |||
| 100 | return $value; |
||
| 101 | } |
||
| 102 | |||
| 127 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: