| Conditions | 15 |
| Paths | 35 |
| Total Lines | 68 |
| Code Lines | 49 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 49 | public function buildUnicodeData(array $index): array |
||
| 50 | { |
||
| 51 | $source = new SplFileObject(__DIR__ . '/../../data/UnicodeData.txt'); |
||
| 52 | $charCounter = 0; |
||
| 53 | echo "Parsing: "; |
||
| 54 | $ranges = []; |
||
| 55 | $lastCode = null; |
||
| 56 | $lastProp = null; |
||
| 57 | $rangeStart = null; |
||
| 58 | $namedStarts = []; |
||
| 59 | while (!$source->eof()) { |
||
| 60 | $line = $source->fgets(); |
||
| 61 | if (false === $line) { |
||
| 62 | throw new RuntimeException("Error reading line from unicode data file"); |
||
| 63 | } |
||
| 64 | if ('' == $line) { |
||
| 65 | continue; |
||
| 66 | } |
||
| 67 | $splitLine = explode(';', $line); |
||
| 68 | $codeHex = $splitLine[0] ?? null; |
||
| 69 | $name = $splitLine[1] ?? null; |
||
| 70 | $prop = $splitLine[2] ?? null; |
||
| 71 | if (!isset($codeHex, $name, $prop)) { |
||
| 72 | throw new RuntimeException("Invalid line format"); |
||
| 73 | } |
||
| 74 | $code = hexdec($codeHex); |
||
| 75 | $isFirst = 1 === preg_match('#^<(.+), First>$#', $name, $matches); |
||
| 76 | $firstName = $matches[1] ?? null; |
||
| 77 | $isLast = 1 === preg_match('#^<(.+), Last>$#', $name, $matches); |
||
| 78 | $lastName = $matches[1] ?? null; |
||
| 79 | $range = null; |
||
| 80 | if ($isFirst) { |
||
| 81 | $namedStarts[$firstName] = $code; |
||
| 82 | unset($rangeStart); |
||
| 83 | } elseif ($isLast) { |
||
| 84 | if (!isset($namedStarts[$lastName]) || isset($rangeStart) || $lastCode !== $namedStarts[$lastName]) { |
||
| 85 | throw new RuntimeException("Invalid file format"); |
||
| 86 | } |
||
| 87 | /** @var int $lastCode */ |
||
| 88 | $range = new Range($lastCode, $code); |
||
|
|
|||
| 89 | } elseif ($prop !== $lastProp) { |
||
| 90 | /** @var int $rangeStart */ |
||
| 91 | if (isset($rangeStart, $lastCode)) { |
||
| 92 | $range = new Range($rangeStart, $lastCode); |
||
| 93 | } |
||
| 94 | |||
| 95 | $rangeStart = $code; |
||
| 96 | } |
||
| 97 | |||
| 98 | if (isset($range)) { |
||
| 99 | if (!isset($ranges[$lastProp])) { |
||
| 100 | $ranges[$lastProp] = []; |
||
| 101 | } |
||
| 102 | $ranges[$lastProp][] = $range; |
||
| 103 | } |
||
| 104 | |||
| 105 | $lastCode = $code; |
||
| 106 | $lastProp = $prop; |
||
| 107 | |||
| 108 | if ($charCounter % 100 == 0) { |
||
| 109 | echo "."; |
||
| 110 | } |
||
| 111 | $charCounter++; |
||
| 112 | } |
||
| 113 | $source = null; |
||
| 114 | echo " {$charCounter} characters\n"; |
||
| 115 | |||
| 116 | return $this->dumpProps($index, $this->buildRangeSets($ranges)); |
||
| 117 | } |
||
| 254 |