Conditions | 17 |
Paths | 13 |
Total Lines | 110 |
Code Lines | 56 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 1 | 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 |
||
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 | } |
||
319 |