Skip to content

Commit cfbf11c

Browse files
committed
RuleTestCase::fix()
1 parent 52c90e5 commit cfbf11c

File tree

6 files changed

+59
-2
lines changed

6 files changed

+59
-2
lines changed

src/Fixable/FileChangedException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Exception;
66

7-
class FileChangedException extends Exception
7+
final class FileChangedException extends Exception
88
{
99

1010
}

src/Fixable/MergeConflictException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Exception;
66

7-
class MergeConflictException extends Exception
7+
final class MergeConflictException extends Exception
88
{
99

1010
}

src/Fixable/Patcher.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
use PHPStan\File\FileReader;
1313
use ReflectionClass;
1414
use SebastianBergmann\Diff\Differ;
15+
use function array_map;
16+
use function count;
17+
use function implode;
18+
use function sha1;
19+
use const PREG_SPLIT_DELIM_CAPTURE;
20+
use const PREG_SPLIT_NO_EMPTY;
1521

1622
#[AutowiredService]
1723
final class Patcher

src/Testing/RuleTestCase.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
use PHPStan\DependencyInjection\Type\ParameterClosureTypeExtensionProvider;
2121
use PHPStan\DependencyInjection\Type\ParameterOutTypeExtensionProvider;
2222
use PHPStan\File\FileHelper;
23+
use PHPStan\File\FileReader;
24+
use PHPStan\Fixable\Patcher;
2325
use PHPStan\Php\PhpVersion;
2426
use PHPStan\PhpDoc\PhpDocInheritanceResolver;
2527
use PHPStan\PhpDoc\StubPhpDocProvider;
@@ -34,6 +36,8 @@
3436
use PHPStan\Rules\Properties\ReadWritePropertiesExtensionProvider;
3537
use PHPStan\Rules\Rule;
3638
use PHPStan\Type\FileTypeMapper;
39+
use SebastianBergmann\Diff\Differ;
40+
use SebastianBergmann\Diff\Output\DiffOnlyOutputBuilder;
3741
use function array_map;
3842
use function array_merge;
3943
use function count;
@@ -193,6 +197,25 @@ static function (Error $error) use ($strictlyTypedSprintf): string {
193197
$this->assertSame($expectedErrorsString, $actualErrorsString);
194198
}
195199

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+
196219
/**
197220
* @param string[] $files
198221
* @return list<Error>

tests/PHPStan/Build/NamedArgumentsRuleTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,22 @@ public function testRule(): void
3939
]);
4040
}
4141

42+
public function testNoFix(): void
43+
{
44+
$this->fix(__DIR__ . '/data/named-arguments-no-errors.php', '');
45+
}
46+
47+
public function testFix(): void
48+
{
49+
$this->fix(__DIR__ . '/data/named-arguments.php', <<<DIFF
50+
- new Exception('foo', 0, new Exception('previous'));
51+
- new Exception('foo', code: 0, previous: new Exception('previous'));
52+
+ new Exception('foo', previous: new Exception('previous'));
53+
+ new Exception(previous: new Exception('previous'));
54+
- new Exception('', 0, new Exception('previous'));
55+
+ new Exception(previous: new Exception('previous'));
56+
57+
DIFF);
58+
}
59+
4260
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php // lint >= 8.0
2+
3+
namespace NamedArgumentRuleNoErrors;
4+
5+
use Exception;
6+
7+
function (): void {
8+
new Exception('foo', 0);
9+
new Exception('foo', 0, null);
10+
};

0 commit comments

Comments
 (0)