| Conditions | 8 |
| Paths | 1 |
| Total Lines | 87 |
| Code Lines | 44 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| Bugs | 1 | 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 declare(strict_types=1); |
||
| 75 | public static function v1($address = null): string |
||
| 76 | { |
||
| 77 | static $matches = [[null], [null]]; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Get the hardware address as a 48-bit positive integer. |
||
| 81 | * |
||
| 82 | * @param string $node [optional] |
||
| 83 | * |
||
| 84 | * @return null|string |
||
| 85 | * @throws InvalidArgumentException |
||
| 86 | */ |
||
| 87 | $node = function($node = null) use (&$matches) { |
||
| 88 | if (null === $node) { |
||
| 89 | if (empty($matches[1][0])) { |
||
| 90 | // Get MAC address (Linux server LAN) |
||
| 91 | $info = `ifconfig 2>&1` ?: ''; |
||
| 92 | |||
| 93 | // Cache the info in $matches |
||
| 94 | preg_match_all('~[^:]([a-f0-9]{2}([:-])[a-f0-9]{2}(\2[a-f0-9]{2}){4})[^:]~i', |
||
| 95 | $info, |
||
| 96 | $matches, |
||
| 97 | PREG_PATTERN_ORDER |
||
| 98 | ); |
||
| 99 | } |
||
| 100 | |||
| 101 | $node = $matches[1][0] ?? null; |
||
| 102 | |||
| 103 | // Cannot identify host, fallback as in http://tools.ietf.org/html/rfc4122#section-4.5 |
||
| 104 | if (empty($node)) { |
||
| 105 | // @codeCoverageIgnoreStart |
||
| 106 | $node = sprintf('%06x%06x', mt_rand(0, 1 << 24), mt_rand(0, 1 << 24)); |
||
| 107 | // @codeCoverageIgnoreEnd |
||
| 108 | } |
||
| 109 | } |
||
| 110 | |||
| 111 | $node = str_replace([':', '-'], '', $node); |
||
| 112 | |||
| 113 | if (ctype_digit($node)) { |
||
| 114 | $node = sprintf('%012x', $node); |
||
| 115 | } |
||
| 116 | |||
| 117 | if (ctype_xdigit($node) && strlen($node) <= 12) { |
||
| 118 | $node = strtolower(sprintf('%012s', $node)); |
||
| 119 | } else { |
||
| 120 | throw new InvalidArgumentException('UUID invalid node value'); |
||
| 121 | } |
||
| 122 | |||
| 123 | return $node; |
||
| 124 | }; // end $node |
||
| 125 | |||
| 126 | $clockSeq = function() { |
||
| 127 | // Random 14-bit sequence number, http://tools.ietf.org/html/rfc4122#section-4.2.1.1 |
||
| 128 | return mt_rand(0, 1 << 14); |
||
| 129 | }; |
||
| 130 | |||
| 131 | $uuidTime = function() { |
||
| 132 | $time = gettimeofday(); |
||
| 133 | $time = ($time['sec'] * 10000000) + ($time['usec'] * 10) + 0x01b21dd213814000; |
||
| 134 | |||
| 135 | return [ |
||
| 136 | 'low' => sprintf('%08x', $time & 0xffffffff), |
||
| 137 | 'mid' => sprintf('%04x', ($time >> 32) & 0xffff), |
||
| 138 | 'high' => sprintf('%04x', ($time >> 48) & 0x0fff) |
||
| 139 | ]; |
||
| 140 | }; |
||
| 141 | |||
| 142 | $uuidTime = $uuidTime(); |
||
| 143 | $clockSeq = $clockSeq(); |
||
| 144 | |||
| 145 | // Set to version 1 |
||
| 146 | $version = hexdec($uuidTime['high']) & 0x0fff; |
||
| 147 | $version &= ~(0xf000); |
||
| 148 | $version |= 1 << 12; |
||
| 149 | |||
| 150 | // RFC 4122 |
||
| 151 | $variant = ($clockSeq >> 8) & 0x3f; |
||
| 152 | $variant &= ~(0xc0); |
||
| 153 | $variant |= 0x80; |
||
| 154 | |||
| 155 | return sprintf('%08s-%04s-%04x-%02x%02x-%012s', |
||
| 156 | $uuidTime['low'], |
||
| 157 | $uuidTime['mid'], |
||
| 158 | $version, |
||
| 159 | $variant, |
||
| 160 | $clockSeq & 0xff, |
||
| 161 | $node($address) |
||
| 162 | ); |
||
| 275 |