| Conditions | 16 |
| Paths | 16 |
| Total Lines | 45 |
| Code Lines | 28 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 27 |
| CRAP Score | 16 |
| 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 |
||
| 15 | 39 | public function normalize($data) |
|
| 16 | { |
||
| 17 | 39 | if (!isset($data)) { |
|
| 18 | 1 | return null; |
|
| 19 | } |
||
| 20 | |||
| 21 | 38 | if (is_array($data)) { |
|
| 22 | 33 | foreach ($data as $key => $value) { |
|
| 23 | 33 | unset($data[$key]); |
|
| 24 | 33 | $data[$this->normalize($key)] = $this->normalize($value); |
|
| 25 | } |
||
| 26 | 38 | } elseif (is_object($data)) { |
|
| 27 | 3 | $new = new \stdClass(); |
|
| 28 | 3 | foreach ($data as $k => $v) { |
|
| 29 | 1 | $key = $this->normalize($k); |
|
| 30 | 1 | $new->{$key} = $this->normalize($v); |
|
| 31 | } |
||
| 32 | 3 | $data = $new; |
|
| 33 | } else { |
||
| 34 | 38 | $data = trim($data); |
|
| 35 | //we need to review this. |
||
| 36 | 38 | if (function_exists('iconv') && function_exists('mb_detect_encoding')) { |
|
| 37 | 38 | $current_encoding = mb_detect_encoding($data); |
|
| 38 | |||
| 39 | 38 | if ($current_encoding != 'UTF-8' && $current_encoding != 'UTF-16') { |
|
| 40 | 38 | $data = iconv($current_encoding, 'UTF-8', $data); |
|
| 41 | } |
||
| 42 | } |
||
| 43 | |||
| 44 | 38 | if (is_numeric($data)) { |
|
| 45 | 35 | $int = intval($data); |
|
| 46 | 35 | $float = floatval($data); |
|
| 47 | 35 | $re = "~^-?[0-9]+(\.[0-9]+)?$~xD"; |
|
| 48 | //@TODO this will not accept all float values, this validates /against/ syntax |
||
| 49 | |||
| 50 | 35 | if (($int === (int)trim($data, '-')) && strlen((string)(int)$data) === strlen($data)) { |
|
| 51 | 34 | $data = (int) $data; |
|
| 52 | 33 | } elseif ($int !== $float && preg_match($re, $data) === 1 && strlen($data) === strlen($float)) { |
|
| 53 | 33 | $data = $float; |
|
| 54 | } |
||
| 55 | } |
||
| 56 | } |
||
| 57 | |||
| 58 | 38 | return $data; |
|
| 59 | } |
||
| 60 | } |
||
| 61 |