|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace DocbookCS\Fix; |
| 6 | + |
| 7 | +use DocbookCS\Source\File; |
| 8 | + |
| 9 | +final class FixApplier |
| 10 | +{ |
| 11 | + /** |
| 12 | + * @param list<Fix|FixPlan> $fixes |
| 13 | + */ |
| 14 | + public function apply(File $file, array $fixes): FixResult |
| 15 | + { |
| 16 | + if ($fixes === []) { |
| 17 | + return new FixResult($file); |
| 18 | + } |
| 19 | + |
| 20 | + $plans = array_map( |
| 21 | + static fn ($fix): FixPlan => $fix instanceof FixPlan ? $fix : new FixPlan($fix), |
| 22 | + $fixes, |
| 23 | + ); |
| 24 | + |
| 25 | + usort($plans, static fn (FixPlan $a, FixPlan $b): int => $a->firstOffset() <=> $b->firstOffset()); |
| 26 | + |
| 27 | + /** @var list<Fix> $acceptedFixes */ |
| 28 | + $acceptedFixes = []; |
| 29 | + $acceptedPlans = 0; |
| 30 | + $skipped = 0; |
| 31 | + |
| 32 | + $content = $file->content; |
| 33 | + $length = strlen($content); |
| 34 | + |
| 35 | + foreach ($plans as $plan) { |
| 36 | + if (!$this->canApply($file, $length, $plan, $acceptedFixes)) { |
| 37 | + $skipped++; |
| 38 | + continue; |
| 39 | + } |
| 40 | + |
| 41 | + if (!$this->changesContent($content, $plan)) { |
| 42 | + $skipped++; |
| 43 | + continue; |
| 44 | + } |
| 45 | + |
| 46 | + foreach ($plan->fixes as $fix) { |
| 47 | + $this->insertFix($acceptedFixes, $fix); |
| 48 | + } |
| 49 | + |
| 50 | + $acceptedPlans++; |
| 51 | + } |
| 52 | + |
| 53 | + $fixedContent = ''; |
| 54 | + $sourceOffset = 0; |
| 55 | + |
| 56 | + foreach ($acceptedFixes as $fix) { |
| 57 | + $fixedContent .= substr($content, $sourceOffset, $fix->beginOffset - $sourceOffset); |
| 58 | + $fixedContent .= $fix->replacement; |
| 59 | + $sourceOffset = $fix->untilOffset; |
| 60 | + } |
| 61 | + |
| 62 | + $fixedContent .= substr($content, $sourceOffset); |
| 63 | + |
| 64 | + return new FixResult( |
| 65 | + file: $file->withContent($fixedContent), |
| 66 | + applied: $acceptedPlans, |
| 67 | + skipped: $skipped, |
| 68 | + appliedFixes: $acceptedFixes, |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * @param list<Fix> $acceptedFixes |
| 74 | + */ |
| 75 | + private function canApply(File $file, int $contentLength, FixPlan $plan, array $acceptedFixes): bool |
| 76 | + { |
| 77 | + $content = $file->content; |
| 78 | + $first = $plan->fixes[0]; |
| 79 | + $previous = null; |
| 80 | + |
| 81 | + foreach ($plan->fixes as $fix) { |
| 82 | + if (!$this->canAcceptFix($file, $contentLength, $first, $previous, $fix, $acceptedFixes)) { |
| 83 | + return false; |
| 84 | + } |
| 85 | + |
| 86 | + $currentContent = substr($content, $fix->beginOffset, $fix->untilOffset - $fix->beginOffset); |
| 87 | + |
| 88 | + if ($fix->expectedContent !== null && $currentContent !== $fix->expectedContent) { |
| 89 | + return false; |
| 90 | + } |
| 91 | + |
| 92 | + $previous = $fix; |
| 93 | + } |
| 94 | + |
| 95 | + return true; |
| 96 | + } |
| 97 | + |
| 98 | + private function changesContent(string $content, FixPlan $plan): bool |
| 99 | + { |
| 100 | + foreach ($plan->fixes as $fix) { |
| 101 | + $currentContent = substr($content, $fix->beginOffset, $fix->untilOffset - $fix->beginOffset); |
| 102 | + |
| 103 | + if ($currentContent !== $fix->replacement) { |
| 104 | + return true; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + return false; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * @param list<Fix> $acceptedFixes |
| 113 | + */ |
| 114 | + private function conflictsWithAcceptedFix(Fix $fix, array $acceptedFixes): bool |
| 115 | + { |
| 116 | + $index = $this->insertionIndex($acceptedFixes, $fix); |
| 117 | + |
| 118 | + return ($index > 0 && self::overlaps($acceptedFixes[$index - 1], $fix)) |
| 119 | + || ($index < count($acceptedFixes) && self::overlaps($acceptedFixes[$index], $fix)); |
| 120 | + } |
| 121 | + |
| 122 | + /** @param list<Fix> $fixes */ |
| 123 | + private function insertFix(array &$fixes, Fix $fix): void |
| 124 | + { |
| 125 | + $lastIndex = count($fixes) - 1; |
| 126 | + if ($lastIndex < 0 || self::compare($fixes[$lastIndex], $fix) < 0) { |
| 127 | + $fixes[] = $fix; |
| 128 | + return; |
| 129 | + } |
| 130 | + |
| 131 | + array_splice($fixes, $this->insertionIndex($fixes, $fix), 0, [$fix]); |
| 132 | + } |
| 133 | + |
| 134 | + /** @param list<Fix> $fixes */ |
| 135 | + private function insertionIndex(array $fixes, Fix $fix): int |
| 136 | + { |
| 137 | + $low = 0; |
| 138 | + $high = count($fixes); |
| 139 | + |
| 140 | + while ($low < $high) { |
| 141 | + $middle = intdiv($low + $high, 2); |
| 142 | + if (self::compare($fixes[$middle], $fix) < 0) { |
| 143 | + $low = $middle + 1; |
| 144 | + } else { |
| 145 | + $high = $middle; |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + return $low; |
| 150 | + } |
| 151 | + |
| 152 | + private static function compare(Fix $a, Fix $b): int |
| 153 | + { |
| 154 | + return [ |
| 155 | + $a->beginOffset, |
| 156 | + $a->untilOffset, |
| 157 | + ] <=> [ |
| 158 | + $b->beginOffset, |
| 159 | + $b->untilOffset, |
| 160 | + ]; |
| 161 | + } |
| 162 | + |
| 163 | + private static function overlaps(Fix $a, Fix $b): bool |
| 164 | + { |
| 165 | + $aIsInsertion = $a->beginOffset === $a->untilOffset; |
| 166 | + $bIsInsertion = $b->beginOffset === $b->untilOffset; |
| 167 | + |
| 168 | + if ($aIsInsertion && $bIsInsertion) { |
| 169 | + return $a->beginOffset === $b->beginOffset; |
| 170 | + } |
| 171 | + |
| 172 | + if ($aIsInsertion) { |
| 173 | + return $a->beginOffset > $b->beginOffset && $a->beginOffset < $b->untilOffset; |
| 174 | + } |
| 175 | + |
| 176 | + if ($bIsInsertion) { |
| 177 | + return $b->beginOffset > $a->beginOffset && $b->beginOffset < $a->untilOffset; |
| 178 | + } |
| 179 | + |
| 180 | + return $a->beginOffset < $b->untilOffset && $b->beginOffset < $a->untilOffset; |
| 181 | + } |
| 182 | + |
| 183 | + /** @param list<Fix> $acceptedFixes */ |
| 184 | + private function canAcceptFix( |
| 185 | + File $file, |
| 186 | + int $contentLength, |
| 187 | + Fix $first, |
| 188 | + ?Fix $previous, |
| 189 | + Fix $fix, |
| 190 | + array $acceptedFixes |
| 191 | + ): bool { |
| 192 | + return $fix->filePath === $file->path |
| 193 | + && $fix->sniffCode === $first->sniffCode |
| 194 | + && $fix->beginOffset >= 0 |
| 195 | + && $fix->untilOffset >= $fix->beginOffset |
| 196 | + && $fix->untilOffset <= $contentLength |
| 197 | + && ($previous === null || !self::overlaps($previous, $fix)) |
| 198 | + && !$this->conflictsWithAcceptedFix($fix, $acceptedFixes); |
| 199 | + } |
| 200 | +} |
0 commit comments