Conditions | 18 |
Paths | 210 |
Total Lines | 138 |
Code Lines | 72 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 declare(strict_types=1); |
||
81 | private function writeDiffHunks($output, array $diff): void |
||
82 | { |
||
83 | // detect "No newline at end of file" and insert into `$diff` if needed |
||
84 | |||
85 | $upperLimit = \count($diff); |
||
86 | |||
87 | if (0 === $diff[$upperLimit - 1][1]) { |
||
88 | $lc = \substr($diff[$upperLimit - 1][0], -1); |
||
89 | |||
90 | if ("\n" !== $lc) { |
||
91 | \array_splice($diff, $upperLimit, 0, [["\n\\ No newline at end of file\n", Differ::NO_LINE_END_EOF_WARNING]]); |
||
92 | } |
||
93 | } else { |
||
94 | // search back for the last `+` and `-` line, |
||
95 | // check if has trailing linebreak, else add under it warning under it |
||
96 | $toFind = [1 => true, 2 => true]; |
||
97 | |||
98 | for ($i = $upperLimit - 1; $i >= 0; --$i) { |
||
99 | if (isset($toFind[$diff[$i][1]])) { |
||
100 | unset($toFind[$diff[$i][1]]); |
||
101 | $lc = \substr($diff[$i][0], -1); |
||
102 | |||
103 | if ("\n" !== $lc) { |
||
104 | \array_splice($diff, $i + 1, 0, [["\n\\ No newline at end of file\n", Differ::NO_LINE_END_EOF_WARNING]]); |
||
105 | } |
||
106 | |||
107 | if (!\count($toFind)) { |
||
108 | break; |
||
109 | } |
||
110 | } |
||
111 | } |
||
112 | } |
||
113 | |||
114 | // write hunks to output buffer |
||
115 | |||
116 | $cutOff = \max($this->commonLineThreshold, $this->contextLines); |
||
117 | $hunkCapture = false; |
||
118 | $sameCount = $toRange = $fromRange = 0; |
||
119 | $toStart = $fromStart = 1; |
||
120 | |||
121 | foreach ($diff as $i => $entry) { |
||
122 | if (0 === $entry[1]) { // same |
||
123 | if (false === $hunkCapture) { |
||
124 | ++$fromStart; |
||
125 | ++$toStart; |
||
126 | |||
127 | continue; |
||
128 | } |
||
129 | |||
130 | ++$sameCount; |
||
131 | ++$toRange; |
||
132 | ++$fromRange; |
||
133 | |||
134 | if ($sameCount === $cutOff) { |
||
135 | $contextStartOffset = ($hunkCapture - $this->contextLines) < 0 |
||
136 | ? $hunkCapture |
||
137 | : $this->contextLines |
||
138 | ; |
||
139 | |||
140 | // note: $contextEndOffset = $this->contextLines; |
||
141 | // |
||
142 | // because we never go beyond the end of the diff. |
||
143 | // with the cutoff/contextlines here the follow is never true; |
||
144 | // |
||
145 | // if ($i - $cutOff + $this->contextLines + 1 > \count($diff)) { |
||
146 | // $contextEndOffset = count($diff) - 1; |
||
147 | // } |
||
148 | // |
||
149 | // ; that would be true for a trailing incomplete hunk case which is dealt with after this loop |
||
150 | |||
151 | $this->writeHunk( |
||
152 | $diff, |
||
153 | $hunkCapture - $contextStartOffset, |
||
154 | $i - $cutOff + $this->contextLines + 1, |
||
155 | $fromStart - $contextStartOffset, |
||
156 | $fromRange - $cutOff + $contextStartOffset + $this->contextLines, |
||
157 | $toStart - $contextStartOffset, |
||
158 | $toRange - $cutOff + $contextStartOffset + $this->contextLines, |
||
159 | $output |
||
160 | ); |
||
161 | |||
162 | $fromStart += $fromRange; |
||
163 | $toStart += $toRange; |
||
164 | |||
165 | $hunkCapture = false; |
||
166 | $sameCount = $toRange = $fromRange = 0; |
||
167 | } |
||
168 | |||
169 | continue; |
||
170 | } |
||
171 | |||
172 | $sameCount = 0; |
||
173 | |||
174 | if ($entry[1] === Differ::NO_LINE_END_EOF_WARNING) { |
||
175 | continue; |
||
176 | } |
||
177 | |||
178 | if (false === $hunkCapture) { |
||
179 | $hunkCapture = $i; |
||
180 | } |
||
181 | |||
182 | if (Differ::ADDED === $entry[1]) { |
||
183 | ++$toRange; |
||
184 | } |
||
185 | |||
186 | if (Differ::REMOVED === $entry[1]) { |
||
187 | ++$fromRange; |
||
188 | } |
||
189 | } |
||
190 | |||
191 | if (false === $hunkCapture) { |
||
192 | return; |
||
193 | } |
||
194 | |||
195 | // we end here when cutoff (commonLineThreshold) was not reached, but we where capturing a hunk, |
||
196 | // do not render hunk till end automatically because the number of context lines might be less than the commonLineThreshold |
||
197 | |||
198 | $contextStartOffset = $hunkCapture - $this->contextLines < 0 |
||
199 | ? $hunkCapture |
||
200 | : $this->contextLines |
||
201 | ; |
||
202 | |||
203 | // prevent trying to write out more common lines than there are in the diff _and_ |
||
204 | // do not write more than configured through the context lines |
||
205 | $contextEndOffset = \min($sameCount, $this->contextLines); |
||
206 | |||
207 | $fromRange -= $sameCount; |
||
208 | $toRange -= $sameCount; |
||
209 | |||
210 | $this->writeHunk( |
||
211 | $diff, |
||
212 | $hunkCapture - $contextStartOffset, |
||
213 | $i - $sameCount + $contextEndOffset + 1, |
||
214 | $fromStart - $contextStartOffset, |
||
215 | $fromRange + $contextStartOffset + $contextEndOffset, |
||
216 | $toStart - $contextStartOffset, |
||
217 | $toRange + $contextStartOffset + $contextEndOffset, |
||
218 | $output |
||
219 | ); |
||
265 |