| Conditions | 14 |
| Paths | 17 |
| Total Lines | 38 |
| Code Lines | 29 |
| 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 |
||
| 7 | protected function validateCastValue($val) |
||
| 8 | { |
||
| 9 | if (in_array($this->format(), ['array', 'object']) && is_string($val)) { |
||
| 10 | try { |
||
| 11 | $val = json_decode($val); |
||
| 12 | } catch (\Exception $e) { |
||
| 13 | throw $this->getValidationException($e->getMessage(), $val); |
||
| 14 | } |
||
| 15 | } |
||
| 16 | switch ($this->format()) { |
||
| 17 | case 'default': |
||
|
|
|||
| 18 | if (!is_string($val)) { |
||
| 19 | throw $this->getValidationException('value must be a string', $val); |
||
| 20 | } else { |
||
| 21 | $val = explode(',', $val); |
||
| 22 | if (count($val) != 2) { |
||
| 23 | throw $this->getValidationException('value must be a string with 2 comma-separated elements', $val); |
||
| 24 | } else { |
||
| 25 | return $this->getNativeGeopoint($val); |
||
| 26 | } |
||
| 27 | } |
||
| 28 | case 'array': |
||
| 29 | if (!is_array($val) || array_keys($val) != [0, 1]) { |
||
| 30 | throw $this->getValidationException('value must be an array with 2 elements', $val); |
||
| 31 | } else { |
||
| 32 | return $this->getNativeGeopoint($val); |
||
| 33 | } |
||
| 34 | case 'object': |
||
| 35 | $val = json_decode(json_encode($val), true); |
||
| 36 | if (!is_array($val) || !array_key_exists('lat', $val) || !array_key_exists('lon', $val)) { |
||
| 37 | throw $this->getValidationException('object must contain lon and lat attributes', $val); |
||
| 38 | } else { |
||
| 39 | return $this->getNativeGeopoint([$val['lon'], $val['lat']]); |
||
| 40 | } |
||
| 41 | default: |
||
| 42 | throw $this->getValidationException('invalid format', $val); |
||
| 43 | } |
||
| 44 | } |
||
| 45 | |||
| 66 |