| Conditions | 16 |
| Paths | 18 |
| Total Lines | 55 |
| 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 |
||
| 36 | public static function filter($value, bool $allowNull = false, int $minValue = null, int $maxValue = PHP_INT_MAX) |
||
| 37 | { |
||
| 38 | if ($allowNull === true && $value === null) { |
||
|
|
|||
| 39 | return null; |
||
| 40 | } |
||
| 41 | |||
| 42 | $valueInt = null; |
||
| 43 | if (is_int($value)) { |
||
| 44 | $valueInt = $value; |
||
| 45 | } elseif (is_string($value)) { |
||
| 46 | $value = trim($value); |
||
| 47 | |||
| 48 | if (strlen($value) === 0) { |
||
| 49 | throw new FilterException('$value string length is zero'); |
||
| 50 | } |
||
| 51 | |||
| 52 | $stringToCheckDigits = $value; |
||
| 53 | |||
| 54 | if ($value[0] === '-' || $value[0] === '+') { |
||
| 55 | $stringToCheckDigits = substr($value, 1); |
||
| 56 | } |
||
| 57 | |||
| 58 | if (!ctype_digit($stringToCheckDigits)) { |
||
| 59 | throw new FilterException( |
||
| 60 | "{$value} does not contain all digits, optionally prepended by a '+' or '-' and optionally " |
||
| 61 | . "surrounded by whitespace" |
||
| 62 | ); |
||
| 63 | } |
||
| 64 | |||
| 65 | $phpIntMin = ~PHP_INT_MAX; |
||
| 66 | |||
| 67 | $casted = (int)$value; |
||
| 68 | |||
| 69 | if ($casted === PHP_INT_MAX && $value !== (string)PHP_INT_MAX) { |
||
| 70 | throw new FilterException("{$value} was greater than a max int of " . PHP_INT_MAX); |
||
| 71 | } |
||
| 72 | |||
| 73 | if ($casted === $phpIntMin && $value !== (string)$phpIntMin) { |
||
| 74 | throw new FilterException("{$value} was less than a min int of {$phpIntMin}"); |
||
| 75 | } |
||
| 76 | |||
| 77 | $valueInt = $casted; |
||
| 78 | } else { |
||
| 79 | throw new FilterException('"' . var_export($value, true) . '" $value is not a string'); |
||
| 80 | } |
||
| 81 | |||
| 82 | if ($minValue !== null && $valueInt < $minValue) { |
||
| 83 | throw new FilterException("{$valueInt} is less than {$minValue}"); |
||
| 84 | } |
||
| 85 | |||
| 86 | if ($valueInt > $maxValue) { |
||
| 87 | throw new FilterException("{$valueInt} is greater than {$maxValue}"); |
||
| 88 | } |
||
| 89 | |||
| 90 | return $valueInt; |
||
| 91 | } |
||
| 93 |