| Conditions | 12 |
| Paths | 10 |
| Total Lines | 40 |
| Code Lines | 30 |
| Lines | 0 |
| Ratio | 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 |
||
| 64 | protected function normalizeValue($value) { |
||
| 65 | if ($value instanceof Node) { |
||
| 66 | return $value; |
||
| 67 | } elseif (is_null($value)) { |
||
| 68 | return new Expr\ConstFetch( |
||
| 69 | new Name('null') |
||
| 70 | ); |
||
| 71 | } elseif (is_bool($value)) { |
||
| 72 | return new Expr\ConstFetch( |
||
| 73 | new Name($value ? 'true' : 'false') |
||
| 74 | ); |
||
| 75 | } elseif (is_int($value)) { |
||
| 76 | return new Scalar\LNumber($value); |
||
| 77 | } elseif (is_float($value)) { |
||
| 78 | return new Scalar\DNumber($value); |
||
| 79 | } elseif (is_string($value)) { |
||
| 80 | return new Scalar\String_($value); |
||
| 81 | } elseif (is_array($value)) { |
||
| 82 | $items = array(); |
||
| 83 | $lastKey = -1; |
||
| 84 | foreach ($value as $itemKey => $itemValue) { |
||
| 85 | // for consecutive, numeric keys don't generate keys |
||
| 86 | if (null !== $lastKey && ++$lastKey === $itemKey) { |
||
| 87 | $items[] = new Expr\ArrayItem( |
||
| 88 | $this->normalizeValue($itemValue) |
||
| 89 | ); |
||
| 90 | } else { |
||
| 91 | $lastKey = null; |
||
| 92 | $items[] = new Expr\ArrayItem( |
||
| 93 | $this->normalizeValue($itemValue), |
||
| 94 | $this->normalizeValue($itemKey) |
||
| 95 | ); |
||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | return new Expr\Array_($items); |
||
| 100 | } else { |
||
| 101 | throw new \LogicException('Invalid value'); |
||
| 102 | } |
||
| 103 | } |
||
| 104 | |||
| 132 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: