| Conditions | 13 |
| Paths | 16 |
| Total Lines | 28 |
| Code Lines | 20 |
| 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 |
||
| 38 | public static function fromString(string $addr): MacAddr8 |
||
| 39 | { |
||
| 40 | $canon = ''; |
||
| 41 | $len = strlen($addr); |
||
| 42 | $sep = null; |
||
| 43 | $digits = 0; |
||
| 44 | for ($i = 0; $i < $len; $i++) { |
||
| 45 | $c = $addr[$i]; |
||
| 46 | if (ctype_xdigit($c)) { |
||
| 47 | if (($digits % 2) == 0 && $digits > 0) { |
||
| 48 | $canon .= ':'; |
||
| 49 | } |
||
| 50 | $canon .= $c; |
||
| 51 | $digits++; |
||
| 52 | } elseif ($sep === null && ($c == ':' || $c == '-' || $c == '.')) { |
||
| 53 | $sep = $c; |
||
| 54 | } elseif ($c !== $sep && !ctype_space($c)) { |
||
| 55 | throw new ParseException('Invalid macaddr8 string', $i); |
||
| 56 | } |
||
| 57 | } |
||
| 58 | |||
| 59 | if ($digits == 12) { |
||
|
|
|||
| 60 | $canon = substr($canon, 0, 8) . ':ff:fe' . substr($canon, 8); |
||
| 61 | } elseif ($digits != 16) { |
||
| 62 | throw new ParseException('Invalid macaddr8 string: only 6 or 8 bytes may be provided'); |
||
| 63 | } |
||
| 64 | |||
| 65 | return new MacAddr8(strtolower($canon)); |
||
| 66 | } |
||
| 124 |