| Conditions | 14 |
| Paths | 18 |
| Total Lines | 34 |
| Code Lines | 22 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | 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 |
||
| 34 | protected function cast($key, $value) |
||
| 35 | { |
||
| 36 | $casts = $this->getCasts(); |
||
| 37 | |||
| 38 | if (!$this->hasCast($key)) { |
||
| 39 | return $value; |
||
| 40 | } |
||
| 41 | |||
| 42 | switch ($casts[$key]) { |
||
| 43 | case 'array': |
||
| 44 | return is_array($value) ? $value : [$value]; |
||
| 45 | case 'bool': |
||
| 46 | case 'boolean': |
||
| 47 | return BooleanModule::makeBoolean($value); |
||
| 48 | case 'int': |
||
| 49 | case 'integer': |
||
| 50 | // Prevent integer overflow |
||
| 51 | return $value >= PHP_INT_MAX || $value <= PHP_INT_MIN ? (string) $value : (int) $value; |
||
| 52 | case 'string': |
||
| 53 | if ($value === true) { |
||
| 54 | return 'true'; |
||
| 55 | } |
||
| 56 | |||
| 57 | if ($value === false) { |
||
| 58 | return 'false'; |
||
| 59 | } |
||
| 60 | |||
| 61 | if (is_array($value)) { |
||
| 62 | return json_encode($value); |
||
| 63 | } |
||
| 64 | |||
| 65 | return (string) $value; |
||
| 66 | default: |
||
| 67 | return $value; |
||
| 68 | } |
||
| 91 |