| Conditions | 14 |
| Paths | 16 |
| Total Lines | 49 |
| Code Lines | 30 |
| 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 |
||
| 27 | public static function filter( |
||
| 28 | $value, |
||
| 29 | bool $allowNull = false, |
||
| 30 | float $minValue = null, |
||
| 31 | float $maxValue = null, |
||
| 32 | bool $castInts = false |
||
| 33 | ) { |
||
| 34 | if ($allowNull === true && $value === null) { |
||
| 35 | return null; |
||
| 36 | } |
||
| 37 | |||
| 38 | $valueFloat = null; |
||
|
|
|||
| 39 | if (is_float($value)) { |
||
| 40 | $valueFloat = $value; |
||
| 41 | } elseif (is_int($value) && $castInts) { |
||
| 42 | $valueFloat = (float)$value; |
||
| 43 | } elseif (is_string($value)) { |
||
| 44 | $value = trim($value); |
||
| 45 | |||
| 46 | if (!is_numeric($value)) { |
||
| 47 | throw new Exception("{$value} does not pass is_numeric"); |
||
| 48 | } |
||
| 49 | |||
| 50 | $value = strtolower($value); |
||
| 51 | |||
| 52 | //This is the only case (that we know of) where is_numeric does not return correctly cast-able float |
||
| 53 | if (strpos($value, 'x') !== false) { |
||
| 54 | throw new Exception("{$value} is hex format"); |
||
| 55 | } |
||
| 56 | |||
| 57 | $valueFloat = (float)$value; |
||
| 58 | } else { |
||
| 59 | throw new Exception('"' . var_export($value, true) . '" $value is not a string'); |
||
| 60 | } |
||
| 61 | |||
| 62 | if (is_infinite($valueFloat)) { |
||
| 63 | throw new Exception("{$value} overflow"); |
||
| 64 | } |
||
| 65 | |||
| 66 | if ($minValue !== null && $valueFloat < $minValue) { |
||
| 67 | throw new Exception("{$valueFloat} is less than {$minValue}"); |
||
| 68 | } |
||
| 69 | |||
| 70 | if ($maxValue !== null && $valueFloat > $maxValue) { |
||
| 71 | throw new Exception("{$valueFloat} is greater than {$maxValue}"); |
||
| 72 | } |
||
| 73 | |||
| 74 | return $valueFloat; |
||
| 75 | } |
||
| 76 | } |
||
| 77 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.