Total Complexity | 49 |
Total Lines | 309 |
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 = YamlService::rowIndentsOf($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 | 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 | */ |
||
234 | private function getCountOfParentsForLine(array $fileLines, $key) |
||
317 | } |
||
318 | } |
||
319 |