Conditions | 14 |
Paths | 16 |
Total Lines | 48 |
Code Lines | 24 |
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 |
||
29 | public static function filter( |
||
30 | $value, |
||
31 | bool $allowNull = false, |
||
32 | float $minValue = null, |
||
33 | float $maxValue = null, |
||
34 | bool $castInts = false |
||
35 | ) { |
||
36 | if ($allowNull === true && $value === null) { |
||
|
|||
37 | return null; |
||
38 | } |
||
39 | |||
40 | $valueFloat = null; |
||
41 | if (is_float($value)) { |
||
42 | $valueFloat = $value; |
||
43 | } elseif (is_int($value) && $castInts) { |
||
44 | $valueFloat = (float)$value; |
||
45 | } elseif (is_string($value)) { |
||
46 | $value = trim($value); |
||
47 | |||
48 | if (!is_numeric($value)) { |
||
49 | throw new FilterException("{$value} does not pass is_numeric"); |
||
50 | } |
||
51 | |||
52 | $value = strtolower($value); |
||
53 | |||
54 | //This is the only case (that we know of) where is_numeric does not return correctly cast-able float |
||
55 | if (strpos($value, 'x') !== false) { |
||
56 | throw new FilterException("{$value} is hex format"); |
||
57 | } |
||
58 | |||
59 | $valueFloat = (float)$value; |
||
60 | } else { |
||
61 | throw new FilterException('"' . var_export($value, true) . '" $value is not a string'); |
||
62 | } |
||
63 | |||
64 | if (is_infinite($valueFloat)) { |
||
65 | throw new FilterException("{$value} overflow"); |
||
66 | } |
||
67 | |||
68 | if ($minValue !== null && $valueFloat < $minValue) { |
||
69 | throw new FilterException("{$valueFloat} is less than {$minValue}"); |
||
70 | } |
||
71 | |||
72 | if ($maxValue !== null && $valueFloat > $maxValue) { |
||
73 | throw new FilterException("{$valueFloat} is greater than {$maxValue}"); |
||
74 | } |
||
75 | |||
76 | return $valueFloat; |
||
77 | } |
||
79 |