Conditions | 15 |
Paths | 28 |
Total 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 |
||
22 | public function apply($value, $options = []) |
||
23 | { |
||
24 | $type = isset($options[0]) ? $options[0] : null; |
||
25 | |||
26 | switch ($type) { |
||
27 | case 'int': |
||
28 | case 'integer': |
||
29 | return (int) $value; |
||
30 | case 'real': |
||
31 | case 'float': |
||
32 | case 'double': |
||
33 | return (float) $value; |
||
34 | case 'string': |
||
35 | return (string) $value; |
||
36 | case 'bool': |
||
37 | case 'boolean': |
||
38 | return (bool) $value; |
||
39 | case 'object': |
||
40 | return is_array($value) ? (object) $value : json_decode($value, false); |
||
41 | case 'array': |
||
42 | return json_decode($value, true); |
||
43 | case 'collection': |
||
44 | $array = is_array($value) ? $value : json_decode($value, true); |
||
45 | |||
46 | return new Collection($array); |
||
47 | default: |
||
48 | throw new InvalidArgumentException("Wrong Sanitizer casting format: {$type}."); |
||
49 | } |
||
50 | } |
||
51 | } |
||
52 |