|
20 | 20 | use PHPStan\DependencyInjection\Type\ParameterClosureTypeExtensionProvider; |
21 | 21 | use PHPStan\DependencyInjection\Type\ParameterOutTypeExtensionProvider; |
22 | 22 | use PHPStan\File\FileHelper; |
| 23 | +use PHPStan\File\FileReader; |
| 24 | +use PHPStan\Fixable\Patcher; |
23 | 25 | use PHPStan\Php\PhpVersion; |
24 | 26 | use PHPStan\PhpDoc\PhpDocInheritanceResolver; |
25 | 27 | use PHPStan\PhpDoc\StubPhpDocProvider; |
|
34 | 36 | use PHPStan\Rules\Properties\ReadWritePropertiesExtensionProvider; |
35 | 37 | use PHPStan\Rules\Rule; |
36 | 38 | use PHPStan\Type\FileTypeMapper; |
| 39 | +use SebastianBergmann\Diff\Differ; |
| 40 | +use SebastianBergmann\Diff\Output\DiffOnlyOutputBuilder; |
37 | 41 | use function array_map; |
38 | 42 | use function array_merge; |
39 | 43 | use function count; |
@@ -193,6 +197,25 @@ static function (Error $error) use ($strictlyTypedSprintf): string { |
193 | 197 | $this->assertSame($expectedErrorsString, $actualErrorsString); |
194 | 198 | } |
195 | 199 |
|
| 200 | + public function fix(string $file, string $expectedDiff): void |
| 201 | + { |
| 202 | + [$errors] = $this->gatherAnalyserErrorsWithDelayedErrors([$file]); |
| 203 | + $diffs = []; |
| 204 | + foreach ($errors as $error) { |
| 205 | + if ($error->getFixedErrorDiff() === null) { |
| 206 | + continue; |
| 207 | + } |
| 208 | + $diffs[] = $error->getFixedErrorDiff(); |
| 209 | + } |
| 210 | + |
| 211 | + $patcher = self::getContainer()->getByType(Patcher::class); |
| 212 | + $originalFileContents = FileReader::read($file); |
| 213 | + $newFileContents = $patcher->applyDiffs($file, $diffs); // @phpstan-ignore missingType.checkedException, missingType.checkedException |
| 214 | + |
| 215 | + $differ = new Differ(new DiffOnlyOutputBuilder('')); |
| 216 | + $this->assertSame($expectedDiff, $differ->diff($originalFileContents, $newFileContents)); |
| 217 | + } |
| 218 | + |
196 | 219 | /** |
197 | 220 | * @param string[] $files |
198 | 221 | * @return list<Error> |
|
0 commit comments