| Conditions | 18 |
| Paths | 210 |
| Total Lines | 137 |
| Code Lines | 73 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | 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 |
||
| 132 | private function writeDiffHunks($output, array $diff) |
||
| 133 | { |
||
| 134 | // detect "No newline at end of file" and insert into `$diff` if needed |
||
| 135 | |||
| 136 | $upperLimit = \count($diff); |
||
| 137 | |||
| 138 | if (0 === $diff[$upperLimit - 1][1]) { |
||
| 139 | $lc = \substr($diff[$upperLimit - 1][0], -1); |
||
| 140 | if ("\n" !== $lc) { |
||
| 141 | \array_splice($diff, $upperLimit, 0, [["\n\\ No newline at end of file\n", Differ::NO_LINE_END_EOF_WARNING]]); |
||
| 142 | } |
||
| 143 | } else { |
||
| 144 | // search back for the last `+` and `-` line, |
||
| 145 | // check if has trailing linebreak, else add under it warning under it |
||
| 146 | $toFind = [1 => true, 2 => true]; |
||
| 147 | for ($i = $upperLimit - 1; $i >= 0; --$i) { |
||
| 148 | if (isset($toFind[$diff[$i][1]])) { |
||
| 149 | unset($toFind[$diff[$i][1]]); |
||
| 150 | $lc = \substr($diff[$i][0], -1); |
||
| 151 | if ("\n" !== $lc) { |
||
| 152 | \array_splice($diff, $i + 1, 0, [["\n\\ No newline at end of file\n", Differ::NO_LINE_END_EOF_WARNING]]); |
||
| 153 | } |
||
| 154 | |||
| 155 | if (!\count($toFind)) { |
||
| 156 | break; |
||
| 157 | } |
||
| 158 | } |
||
| 159 | } |
||
| 160 | } |
||
| 161 | |||
| 162 | // write hunks to output buffer |
||
| 163 | |||
| 164 | $cutOff = \max($this->commonLineThreshold, $this->contextLines); |
||
| 165 | $hunkCapture = false; |
||
| 166 | $sameCount = $toRange = $fromRange = 0; |
||
| 167 | $toStart = $fromStart = 1; |
||
| 168 | |||
| 169 | foreach ($diff as $i => $entry) { |
||
| 170 | if (0 === $entry[1]) { // same |
||
| 171 | if (false === $hunkCapture) { |
||
| 172 | ++$fromStart; |
||
| 173 | ++$toStart; |
||
| 174 | |||
| 175 | continue; |
||
| 176 | } |
||
| 177 | |||
| 178 | ++$sameCount; |
||
| 179 | ++$toRange; |
||
| 180 | ++$fromRange; |
||
| 181 | |||
| 182 | if ($sameCount === $cutOff) { |
||
| 183 | $contextStartOffset = ($hunkCapture - $this->contextLines) < 0 |
||
| 184 | ? $hunkCapture |
||
| 185 | : $this->contextLines |
||
| 186 | ; |
||
| 187 | |||
| 188 | // note: $contextEndOffset = $this->contextLines; |
||
| 189 | // |
||
| 190 | // because we never go beyond the end of the diff. |
||
| 191 | // with the cutoff/contextlines here the follow is never true; |
||
| 192 | // |
||
| 193 | // if ($i - $cutOff + $this->contextLines + 1 > \count($diff)) { |
||
| 194 | // $contextEndOffset = count($diff) - 1; |
||
| 195 | // } |
||
| 196 | // |
||
| 197 | // ; that would be true for a trailing incomplete hunk case which is dealt with after this loop |
||
| 198 | |||
| 199 | $this->writeHunk( |
||
| 200 | $diff, |
||
| 201 | $hunkCapture - $contextStartOffset, |
||
| 202 | $i - $cutOff + $this->contextLines + 1, |
||
| 203 | $fromStart - $contextStartOffset, |
||
| 204 | $fromRange - $cutOff + $contextStartOffset + $this->contextLines, |
||
| 205 | $toStart - $contextStartOffset, |
||
| 206 | $toRange - $cutOff + $contextStartOffset + $this->contextLines, |
||
| 207 | $output |
||
| 208 | ); |
||
| 209 | |||
| 210 | $fromStart += $fromRange; |
||
| 211 | $toStart += $toRange; |
||
| 212 | |||
| 213 | $hunkCapture = false; |
||
| 214 | $sameCount = $toRange = $fromRange = 0; |
||
| 215 | } |
||
| 216 | |||
| 217 | continue; |
||
| 218 | } |
||
| 219 | |||
| 220 | $sameCount = 0; |
||
| 221 | |||
| 222 | if ($entry[1] === Differ::NO_LINE_END_EOF_WARNING) { |
||
| 223 | continue; |
||
| 224 | } |
||
| 225 | |||
| 226 | $this->changed = true; |
||
| 227 | |||
| 228 | if (false === $hunkCapture) { |
||
| 229 | $hunkCapture = $i; |
||
| 230 | } |
||
| 231 | |||
| 232 | if (Differ::ADDED === $entry[1]) { // added |
||
| 233 | ++$toRange; |
||
| 234 | } |
||
| 235 | |||
| 236 | if (Differ::REMOVED === $entry[1]) { // removed |
||
| 237 | ++$fromRange; |
||
| 238 | } |
||
| 239 | } |
||
| 240 | |||
| 241 | if (false === $hunkCapture) { |
||
| 242 | return; |
||
| 243 | } |
||
| 244 | |||
| 245 | // we end here when cutoff (commonLineThreshold) was not reached, but we where capturing a hunk, |
||
| 246 | // do not render hunk till end automatically because the number of context lines might be less than the commonLineThreshold |
||
| 247 | |||
| 248 | $contextStartOffset = $hunkCapture - $this->contextLines < 0 |
||
| 249 | ? $hunkCapture |
||
| 250 | : $this->contextLines |
||
| 251 | ; |
||
| 252 | |||
| 253 | // prevent trying to write out more common lines than there are in the diff _and_ |
||
| 254 | // do not write more than configured through the context lines |
||
| 255 | $contextEndOffset = \min($sameCount, $this->contextLines); |
||
| 256 | |||
| 257 | $fromRange -= $sameCount; |
||
| 258 | $toRange -= $sameCount; |
||
| 259 | |||
| 260 | $this->writeHunk( |
||
| 261 | $diff, |
||
| 262 | $hunkCapture - $contextStartOffset, |
||
| 263 | $i - $sameCount + $contextEndOffset + 1, |
||
| 264 | $fromStart - $contextStartOffset, |
||
| 265 | $fromRange + $contextStartOffset + $contextEndOffset, |
||
| 266 | $toStart - $contextStartOffset, |
||
| 267 | $toRange + $contextStartOffset + $contextEndOffset, |
||
| 268 | $output |
||
| 269 | ); |
||
| 316 |