Conditions | 12 |
Paths | 17 |
Total Lines | 34 |
Code Lines | 26 |
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 | return $this->getNativeGeopoint(explode(',', $val)); |
||
22 | } |
||
23 | case 'array': |
||
24 | if (!is_array($val)) { |
||
25 | throw $this->getValidationException('value must be an array', $val); |
||
26 | } else { |
||
27 | return $this->getNativeGeopoint($val); |
||
28 | } |
||
29 | case 'object': |
||
30 | if (!is_object($val)) { |
||
31 | throw $this->getValidationException('value must be an object', $val); |
||
32 | } elseif (!isset($val->lat) || !isset($val->lon)) { |
||
33 | throw $this->getValidationException('object must contain lon and lat attributes', $val); |
||
34 | } else { |
||
35 | return $this->getNativeGeopoint([$val->lon, $val->lat]); |
||
36 | } |
||
37 | default: |
||
38 | throw $this->getValidationException('invalid format', $val); |
||
39 | } |
||
40 | } |
||
41 | |||
66 |