| Total Complexity | 49 |
| Total Lines | 316 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 1 | Features | 0 |
Complex classes like YamlIndentDataFactory often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use YamlIndentDataFactory, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class YamlIndentDataFactory |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * @param string[] $fileLines |
||
| 12 | * @param int $key |
||
| 13 | * @param int $countOfIndents |
||
| 14 | * @param string $fileLine current checked line in loop |
||
| 15 | * @param bool $isCommentLine |
||
| 16 | * @return string |
||
| 17 | * |
||
| 18 | * @SuppressWarnings("CyclomaticComplexity") |
||
| 19 | * @SuppressWarnings("ExcessiveMethodLength") |
||
| 20 | */ |
||
| 21 | public function getRightFileLines(array $fileLines, $key, $countOfIndents, $fileLine, $isCommentLine = false) |
||
| 22 | { |
||
| 23 | if (YamlService::isLineComment($fileLines[$key])) { |
||
| 24 | $key++; |
||
| 25 | return $this->getRightFileLines($fileLines, $key, $countOfIndents, $fileLine, true); |
||
| 26 | } |
||
| 27 | |||
| 28 | $line = $fileLines[$key]; |
||
| 29 | $trimmedLine = trim($line); |
||
| 30 | $countOfRowIndents = strlen($line) - strlen(ltrim($line)); |
||
| 31 | $explodedLine = explode(':', $line); |
||
| 32 | |||
| 33 | // empty line |
||
| 34 | if ($trimmedLine === '') { |
||
| 35 | $fileRows = array_keys($fileLines); |
||
| 36 | $lastFileRow = end($fileRows); |
||
| 37 | /* set comment line indents by next non-empty line, e.g |
||
| 38 | (empty line) |
||
| 39 | # comment line |
||
| 40 | (empty line) |
||
| 41 | foo: bar |
||
| 42 | */ |
||
| 43 | if ($isCommentLine && $lastFileRow !== $key) { |
||
| 44 | $key++; |
||
| 45 | return $this->getRightFileLines($fileLines, $key, $countOfIndents, $fileLine, true); |
||
| 46 | } |
||
| 47 | |||
| 48 | $correctIndents = $this->getCorrectIndents(0); |
||
| 49 | $trimmedFileLine = trim($fileLine); |
||
| 50 | |||
| 51 | return $correctIndents . $trimmedFileLine; |
||
| 52 | } |
||
| 53 | |||
| 54 | // the highest parent |
||
| 55 | if ($countOfRowIndents === 0) { |
||
| 56 | // line is directive |
||
| 57 | if (YamlService::hasLineThreeDashesOnStartOfLine($trimmedLine)) { |
||
| 58 | $correctIndents = $this->getCorrectIndents($countOfRowIndents); |
||
| 59 | $trimmedFileLine = trim($fileLine); |
||
| 60 | |||
| 61 | return $correctIndents . $trimmedFileLine; |
||
| 62 | } |
||
| 63 | |||
| 64 | // parent start as array, e.g. "- foo: bar" |
||
| 65 | // skip comment line because we want result after this condition |
||
| 66 | if ($isCommentLine === false && YamlService::isLineStartOfArrayWithKeyAndValue($trimmedLine)) { |
||
| 67 | return $this->getCorrectLineForArrayWithKeyAndValue($line, $fileLines, $key, $countOfIndents, $fileLine, $isCommentLine); |
||
| 68 | } |
||
| 69 | |||
| 70 | $correctIndents = $this->getCorrectIndents($countOfRowIndents); |
||
| 71 | $trimmedFileLine = trim($fileLine); |
||
| 72 | |||
| 73 | return $correctIndents . $trimmedFileLine; |
||
| 74 | } |
||
| 75 | |||
| 76 | // line start of array, e.g. "- foo: bar" or "- foo" or "- { foo: bar }" |
||
| 77 | if (YamlService::isLineStartOfArrayWithKeyAndValue($trimmedLine)) { |
||
| 78 | return $this->getCorrectLineForArrayWithKeyAndValue($line, $fileLines, $key, $countOfIndents, $fileLine, $isCommentLine); |
||
| 79 | } |
||
| 80 | |||
| 81 | // children of array, description over name of function |
||
| 82 | if ($this->belongLineToArray($fileLines, $key)) { |
||
| 83 | $countOfParents = $this->getCountOfParentsForLine($fileLines, $key); |
||
| 84 | $correctIndents = $this->getCorrectIndents($countOfParents * $countOfIndents); |
||
| 85 | $trimmedFileLine = trim($fileLine); |
||
| 86 | |||
| 87 | return $correctIndents . $trimmedFileLine; |
||
| 88 | } |
||
| 89 | |||
| 90 | // line without ':', e.g. array or string |
||
| 91 | if (array_key_exists(1, $explodedLine) === false) { |
||
| 92 | // is multidimensional array? |
||
| 93 | if ($trimmedLine === '-') { |
||
| 94 | $countOfParents = $this->getCountOfParentsForLine($fileLines, $key); |
||
| 95 | |||
| 96 | $correctIndents = $this->getCorrectIndents($countOfParents * $countOfIndents); |
||
| 97 | $trimmedFileLine = trim($fileLine); |
||
| 98 | |||
| 99 | return $correctIndents . $trimmedFileLine; |
||
| 100 | } |
||
| 101 | |||
| 102 | // is array or string? |
||
| 103 | $countOfParents = $this->getCountOfParentsForLine($fileLines, $key); |
||
| 104 | $correctIndents = $this->getCorrectIndents($countOfParents * $countOfIndents); |
||
| 105 | $trimmedFileLine = trim($fileLine); |
||
| 106 | |||
| 107 | return $correctIndents . $trimmedFileLine; |
||
| 108 | } |
||
| 109 | |||
| 110 | $lineValue = $explodedLine[1]; |
||
| 111 | $trimmedLineValue = trim($lineValue); |
||
| 112 | |||
| 113 | // parent, not comment line |
||
| 114 | if ($isCommentLine === false && ($trimmedLineValue === '' || YamlService::isValueReuseVariable($trimmedLineValue))) { |
||
| 115 | $nextLine = $fileLines[$key + 1]; |
||
| 116 | $countOfNextRowIndents = strlen($nextLine) - strlen(ltrim($nextLine)); |
||
| 117 | if ($countOfNextRowIndents > $countOfRowIndents) { |
||
| 118 | $countOfParents = $this->getCountOfParentsForLine($fileLines, $key); |
||
| 119 | |||
| 120 | $correctIndents = $this->getCorrectIndents($countOfParents * $countOfIndents); |
||
| 121 | $trimmedFileLine = trim($fileLine); |
||
| 122 | |||
| 123 | return $correctIndents . $trimmedFileLine; |
||
| 124 | } |
||
| 125 | } |
||
| 126 | |||
| 127 | $countOfParents = $this->getCountOfParentsForLine($fileLines, $key); |
||
| 128 | $correctIndents = $this->getCorrectIndents($countOfParents * $countOfIndents); |
||
| 129 | $trimmedFileLine = trim($fileLine); |
||
| 130 | |||
| 131 | return $correctIndents . $trimmedFileLine; |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @param int $countOfIndents |
||
| 136 | * @return string |
||
| 137 | */ |
||
| 138 | private function getCorrectIndents($countOfIndents) |
||
| 139 | { |
||
| 140 | return str_repeat(' ', $countOfIndents); |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Belong line to children of array, e.g. |
||
| 145 | * - foo: bar |
||
| 146 | * baz: qux |
||
| 147 | * quux: quuz |
||
| 148 | * etc.: etc. |
||
| 149 | * |
||
| 150 | * @param string[] $fileLines |
||
| 151 | * @param int $key |
||
| 152 | * @return bool |
||
| 153 | */ |
||
| 154 | private function belongLineToArray(array $fileLines, $key) |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * line start of array, e.g. "- foo: bar" or "- foo" or "- { foo: bar }" |
||
| 184 | * |
||
| 185 | * @param string $line |
||
| 186 | * @param string[] $fileLines |
||
| 187 | * @param int $key |
||
| 188 | * @param int $countOfIndents |
||
| 189 | * @param string $fileLine current checked line in loop |
||
| 190 | * @param bool $isCommentLine |
||
| 191 | * @return string |
||
| 192 | */ |
||
| 193 | private function getCorrectLineForArrayWithKeyAndValue($line, array $fileLines, $key, $countOfIndents, $fileLine, $isCommentLine) |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Go back until deepest parent and count them |
||
| 232 | * |
||
| 233 | * @param string[] $fileLines |
||
| 234 | * @param int $key |
||
| 235 | * @return int |
||
| 236 | * |
||
| 237 | * @SuppressWarnings("CyclomaticComplexity") |
||
| 238 | */ |
||
| 239 | private function getCountOfParentsForLine(array $fileLines, $key) |
||
| 324 | } |
||
| 325 | } |
||
| 326 |