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