AbstractChunkOutputBuilder.php
1 year ago
DiffOnlyOutputBuilder.php
1 year ago
DiffOutputBuilderInterface.php
1 year ago
StrictUnifiedDiffOutputBuilder.php
1 year ago
UnifiedDiffOutputBuilder.php
1 year ago
AbstractChunkOutputBuilder.php
53 lines
| 1 | <?php declare(strict_types=1); |
| 2 | /* |
| 3 | * This file is part of sebastian/diff. |
| 4 | * |
| 5 | * (c) Sebastian Bergmann <sebastian@phpunit.de> |
| 6 | * |
| 7 | * For the full copyright and license information, please view the LICENSE |
| 8 | * file that was distributed with this source code. |
| 9 | */ |
| 10 | namespace SebastianBergmann\Diff\Output; |
| 11 | |
| 12 | use function count; |
| 13 | |
| 14 | abstract class AbstractChunkOutputBuilder implements DiffOutputBuilderInterface |
| 15 | { |
| 16 | /** |
| 17 | * Takes input of the diff array and returns the common parts. |
| 18 | * Iterates through diff line by line. |
| 19 | */ |
| 20 | protected function getCommonChunks(array $diff, int $lineThreshold = 5): array |
| 21 | { |
| 22 | $diffSize = count($diff); |
| 23 | $capturing = false; |
| 24 | $chunkStart = 0; |
| 25 | $chunkSize = 0; |
| 26 | $commonChunks = []; |
| 27 | |
| 28 | for ($i = 0; $i < $diffSize; ++$i) { |
| 29 | if ($diff[$i][1] === 0 /* OLD */) { |
| 30 | if ($capturing === false) { |
| 31 | $capturing = true; |
| 32 | $chunkStart = $i; |
| 33 | $chunkSize = 0; |
| 34 | } else { |
| 35 | ++$chunkSize; |
| 36 | } |
| 37 | } elseif ($capturing !== false) { |
| 38 | if ($chunkSize >= $lineThreshold) { |
| 39 | $commonChunks[$chunkStart] = $chunkStart + $chunkSize; |
| 40 | } |
| 41 | |
| 42 | $capturing = false; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | if ($capturing !== false && $chunkSize >= $lineThreshold) { |
| 47 | $commonChunks[$chunkStart] = $chunkStart + $chunkSize; |
| 48 | } |
| 49 | |
| 50 | return $commonChunks; |
| 51 | } |
| 52 | } |
| 53 |