| Conditions | 15 |
| Paths | 16 |
| Total Lines | 46 |
| Code Lines | 25 |
| 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 |
||
| 30 | public static function filter($value, $allowNull = false, $minLength = 1, $maxLength = PHP_INT_MAX) |
||
| 31 | { |
||
| 32 | if ($allowNull !== false && $allowNull !== true) { |
||
| 33 | throw new \InvalidArgumentException('$allowNull was not a boolean value'); |
||
| 34 | } |
||
| 35 | |||
| 36 | if (!is_int($minLength) || $minLength < 0) { |
||
| 37 | throw new \InvalidArgumentException('$minLength was not a positive integer value'); |
||
| 38 | } |
||
| 39 | |||
| 40 | if (!is_int($maxLength) || $maxLength < 0) { |
||
| 41 | throw new \InvalidArgumentException('$maxLength was not a positive integer value'); |
||
| 42 | } |
||
| 43 | |||
| 44 | if ($allowNull === true && $value === null) { |
||
| 45 | return null; |
||
| 46 | } |
||
| 47 | |||
| 48 | if (is_scalar($value)) { |
||
| 49 | $value = "{$value}"; |
||
| 50 | } |
||
| 51 | |||
| 52 | if (is_object($value) && method_exists($value, '__toString')) { |
||
| 53 | $value = (string)$value; |
||
| 54 | } |
||
| 55 | |||
| 56 | if (!is_string($value)) { |
||
| 57 | throw new \Exception("Value '" . var_export($value, true) . "' is not a string"); |
||
| 58 | } |
||
| 59 | |||
| 60 | $valueLength = strlen($value); |
||
| 61 | |||
| 62 | if ($valueLength < $minLength || $valueLength > $maxLength) { |
||
| 63 | throw new \Exception( |
||
| 64 | sprintf( |
||
| 65 | "Value '%s' with length '%d' is less than '%d' or greater than '%d'", |
||
| 66 | $value, |
||
| 67 | $valueLength, |
||
| 68 | $minLength, |
||
| 69 | $maxLength |
||
| 70 | ) |
||
| 71 | ); |
||
| 72 | } |
||
| 73 | |||
| 74 | return $value; |
||
| 75 | } |
||
| 76 | |||
| 104 |