| Conditions | 12 |
| Paths | 10 |
| Total Lines | 37 |
| Code Lines | 26 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 40 | public function __construct(?string $value) |
||
| 41 | { |
||
| 42 | if (null === $value || !preg_match('#^\s*(==|!=|[<>]=?)?\s*([0-9\.]+)\s*([kmg]i?)?\s*$#i', $value, $matches)) { |
||
| 43 | throw new InvalidArgumentException(sprintf('Don\'t understand "%s" as a number test.', $value ?? 'null')); |
||
| 44 | } |
||
| 45 | |||
| 46 | $target = $matches[2]; |
||
| 47 | |||
| 48 | if ( ! is_numeric($target)) { |
||
| 49 | throw new InvalidArgumentException(sprintf('Invalid number "%s"', $target)); |
||
| 50 | } |
||
| 51 | |||
| 52 | if (isset($matches[3])) { |
||
| 53 | // magnitude |
||
| 54 | switch (strtolower($matches[3])) { |
||
| 55 | case 'k': |
||
| 56 | $target *= 1000; |
||
| 57 | break; |
||
| 58 | case 'ki': |
||
| 59 | $target *= 1024; |
||
| 60 | break; |
||
| 61 | case 'm': |
||
| 62 | $target *= 1000000; |
||
| 63 | break; |
||
| 64 | case 'mi': |
||
| 65 | $target *= 1024 * 1024; |
||
| 66 | break; |
||
| 67 | case 'g': |
||
| 68 | $target *= 1000000000; |
||
| 69 | break; |
||
| 70 | case 'gi': |
||
| 71 | $target *= 1024 * 1024 * 1024; |
||
| 72 | break; |
||
| 73 | } |
||
| 74 | } |
||
| 75 | |||
| 76 | parent::__construct($target, $matches[1] ?: '=='); |
||
| 77 | } |
||
| 78 | } |