Conditions | 18 |
Paths | 14 |
Total Lines | 111 |
Code Lines | 56 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
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 |
||
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 | } |
||
319 |