Total Complexity | 51 |
Total Lines | 310 |
Duplicated Lines | 0 % |
Changes | 6 | ||
Bugs | 3 | 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 |
||
7 | class YamlIndentDataFactory |
||
8 | { |
||
9 | /** |
||
10 | * @param string[] $fileLines |
||
11 | * @param int $key |
||
12 | * @param int $countOfIndents |
||
13 | * @param string $fileLine current checked line in loop |
||
14 | * @param bool $isCommentLine |
||
15 | * @return string |
||
16 | * |
||
17 | * @SuppressWarnings("CyclomaticComplexity") |
||
18 | * @SuppressWarnings("ExcessiveMethodLength") |
||
19 | */ |
||
20 | public function getRightFileLines(array $fileLines, $key, $countOfIndents, $fileLine, $isCommentLine = false) |
||
21 | { |
||
22 | if (YamlService::isLineComment($fileLines[$key])) { |
||
23 | $key++; |
||
24 | return $this->getRightFileLines($fileLines, $key, $countOfIndents, $fileLine, true); |
||
25 | } |
||
26 | |||
27 | $line = $fileLines[$key]; |
||
28 | $trimmedLine = trim($line); |
||
29 | $countOfRowIndents = YamlService::rowIndentsOf($line); |
||
30 | $explodedLine = explode(':', $line); |
||
31 | |||
32 | // empty line |
||
33 | if ($trimmedLine === '') { |
||
34 | $fileRows = array_keys($fileLines); |
||
35 | $lastFileRow = end($fileRows); |
||
36 | /* set comment line indents by next non-empty line, e.g |
||
37 | (empty line) |
||
38 | # comment line |
||
39 | (empty line) |
||
40 | foo: bar |
||
41 | */ |
||
42 | if ($isCommentLine && $lastFileRow !== $key) { |
||
43 | $key++; |
||
44 | return $this->getRightFileLines($fileLines, $key, $countOfIndents, $fileLine, true); |
||
45 | } |
||
46 | |||
47 | $correctIndents = $this->getCorrectIndents(0); |
||
48 | $trimmedFileLine = trim($fileLine); |
||
49 | |||
50 | return $correctIndents . $trimmedFileLine; |
||
51 | } |
||
52 | |||
53 | // the highest parent |
||
54 | if ($countOfRowIndents === 0) { |
||
55 | // line is directive |
||
56 | if (YamlService::hasLineThreeDashesOnStartOfLine($trimmedLine)) { |
||
57 | $correctIndents = $this->getCorrectIndents($countOfRowIndents); |
||
58 | $trimmedFileLine = trim($fileLine); |
||
59 | |||
60 | return $correctIndents . $trimmedFileLine; |
||
61 | } |
||
62 | |||
63 | // parent start as array, e.g. "- foo: bar" |
||
64 | // skip comment line because we want result after this condition |
||
65 | if ($isCommentLine === false && YamlService::isLineStartOfArrayWithKeyAndValue($trimmedLine)) { |
||
66 | return $this->getCorrectLineForArrayWithKeyAndValue($line, $fileLines, $key, $countOfIndents, $fileLine, $isCommentLine); |
||
67 | } |
||
68 | |||
69 | $correctIndents = $this->getCorrectIndents($countOfRowIndents); |
||
70 | $trimmedFileLine = trim($fileLine); |
||
71 | |||
72 | return $correctIndents . $trimmedFileLine; |
||
73 | } |
||
74 | |||
75 | // line start of array, e.g. "- foo: bar" or "- foo" or "- { foo: bar }" |
||
76 | if (YamlService::isLineStartOfArrayWithKeyAndValue($trimmedLine)) { |
||
77 | return $this->getCorrectLineForArrayWithKeyAndValue($line, $fileLines, $key, $countOfIndents, $fileLine, $isCommentLine); |
||
78 | } |
||
79 | |||
80 | // children of array, description over name of function |
||
81 | if ($this->belongLineToArray($fileLines, $key)) { |
||
82 | $countOfParents = $this->getCountOfParentsForLine($fileLines, $key); |
||
83 | $correctIndents = $this->getCorrectIndents($countOfParents * $countOfIndents); |
||
84 | $trimmedFileLine = trim($fileLine); |
||
85 | |||
86 | return $correctIndents . $trimmedFileLine; |
||
87 | } |
||
88 | |||
89 | // line without ':', e.g. array or string |
||
90 | if (array_key_exists(1, $explodedLine) === false) { |
||
91 | // is multidimensional array? |
||
92 | if ($trimmedLine === '-') { |
||
93 | $countOfParents = $this->getCountOfParentsForLine($fileLines, $key); |
||
94 | |||
95 | $correctIndents = $this->getCorrectIndents($countOfParents * $countOfIndents); |
||
96 | $trimmedFileLine = trim($fileLine); |
||
97 | |||
98 | return $correctIndents . $trimmedFileLine; |
||
99 | } |
||
100 | |||
101 | // is array or string? |
||
102 | $countOfParents = $this->getCountOfParentsForLine($fileLines, $key); |
||
103 | $correctIndents = $this->getCorrectIndents($countOfParents * $countOfIndents); |
||
104 | $trimmedFileLine = trim($fileLine); |
||
105 | |||
106 | return $correctIndents . $trimmedFileLine; |
||
107 | } |
||
108 | |||
109 | $lineValue = $explodedLine[1]; |
||
110 | $trimmedLineValue = trim($lineValue); |
||
111 | |||
112 | // parent, not comment line |
||
113 | if ($isCommentLine === false && ($trimmedLineValue === '' || YamlService::isValueReuseVariable($trimmedLineValue))) { |
||
114 | // fix situation when key is without value and is not parent, e.g.: " foo:" |
||
115 | $nextLine = array_key_exists($key + 1, $fileLines) ? $fileLines[$key + 1] : ''; |
||
116 | if (YamlService::rowIndentsOf($nextLine) > $countOfRowIndents) { |
||
117 | $countOfParents = $this->getCountOfParentsForLine($fileLines, $key); |
||
118 | |||
119 | $correctIndents = $this->getCorrectIndents($countOfParents * $countOfIndents); |
||
120 | $trimmedFileLine = trim($fileLine); |
||
121 | |||
122 | return $correctIndents . $trimmedFileLine; |
||
123 | } |
||
124 | } |
||
125 | |||
126 | $countOfParents = $this->getCountOfParentsForLine($fileLines, $key); |
||
127 | $correctIndents = $this->getCorrectIndents($countOfParents * $countOfIndents); |
||
128 | $trimmedFileLine = trim($fileLine); |
||
129 | |||
130 | return $correctIndents . $trimmedFileLine; |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * @param int $countOfIndents |
||
135 | * @return string |
||
136 | */ |
||
137 | private function getCorrectIndents($countOfIndents) |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * Belong line to children of array, e.g. |
||
144 | * - foo: bar |
||
145 | * baz: qux |
||
146 | * quux: quuz |
||
147 | * etc.: etc. |
||
148 | * |
||
149 | * @param string[] $fileLines |
||
150 | * @param int $key |
||
151 | * @return bool |
||
152 | */ |
||
153 | private function belongLineToArray(array $fileLines, $key) |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * line start of array, e.g. "- foo: bar" or "- foo" or "- { foo: bar }" |
||
179 | * |
||
180 | * @param string $line |
||
181 | * @param string[] $fileLines |
||
182 | * @param int $key |
||
183 | * @param int $countOfIndents |
||
184 | * @param string $fileLine current checked line in loop |
||
185 | * @param bool $isCommentLine |
||
186 | * @return string |
||
187 | */ |
||
188 | private function getCorrectLineForArrayWithKeyAndValue($line, array $fileLines, $key, $countOfIndents, $fileLine, $isCommentLine) |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * Go back until deepest parent and count them |
||
227 | * |
||
228 | * @param string[] $fileLines |
||
229 | * @param int $key |
||
230 | * @return int |
||
231 | * |
||
232 | * @SuppressWarnings("CyclomaticComplexity") |
||
233 | * @SuppressWarnings("ExcessiveMethodLength") |
||
234 | */ |
||
235 | private function getCountOfParentsForLine(array $fileLines, $key) |
||
317 | } |
||
318 | } |
||
319 |