diff --git a/src/Analyser/RuleErrorTransformer.php b/src/Analyser/RuleErrorTransformer.php index 5c406d28c4..ebbc7679e9 100644 --- a/src/Analyser/RuleErrorTransformer.php +++ b/src/Analyser/RuleErrorTransformer.php @@ -29,6 +29,7 @@ use SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder; use function get_class; use function sha1; +use function str_contains; use function str_repeat; #[AutowiredService] @@ -137,7 +138,12 @@ public function transform( $newStmts = $traverser->traverse($newStmts); if ($visitor->isFound()) { - $printer = new PhpPrinter(['indent' => str_repeat($indentDetector->indentCharacter, $indentDetector->indentSize)]); + if (str_contains($indentDetector->indentCharacter, "\t")) { + $indent = "\t"; + } else { + $indent = str_repeat($indentDetector->indentCharacter, $indentDetector->indentSize); + } + $printer = new PhpPrinter(['indent' => $indent]); $newCode = $printer->printFormatPreserving($newStmts, $fileNodes, $oldTokens); if ($oldCode !== $newCode) { diff --git a/tests/PHPStan/Rules/Methods/OverridingMethodRuleTest.php b/tests/PHPStan/Rules/Methods/OverridingMethodRuleTest.php index e55c5011fd..cb2171e112 100644 --- a/tests/PHPStan/Rules/Methods/OverridingMethodRuleTest.php +++ b/tests/PHPStan/Rules/Methods/OverridingMethodRuleTest.php @@ -820,4 +820,12 @@ public function testFixOverride(): void $this->fix(__DIR__ . '/data/fix-override-attribute.php', __DIR__ . '/data/fix-override-attribute.php.fixed'); } + #[RequiresPhp('>= 8.3')] + public function testFixWithTabs(): void + { + $this->phpVersionId = PHP_VERSION_ID; + $this->checkMissingOverrideMethodAttribute = true; + $this->fix(__DIR__ . '/data/fix-with-tabs.php', __DIR__ . '/data/fix-with-tabs.php.fixed'); + } + } diff --git a/tests/PHPStan/Rules/Methods/data/fix-with-tabs.php b/tests/PHPStan/Rules/Methods/data/fix-with-tabs.php new file mode 100644 index 0000000000..1aa2d68f1a --- /dev/null +++ b/tests/PHPStan/Rules/Methods/data/fix-with-tabs.php @@ -0,0 +1,28 @@ + */ + public function foo(): Collection; +} + +interface BarI +{ + +} +class Bar implements BarI {} + +/** @template-coveriant TValue */ +class Collection {} + + +class Baz implements FooInterface +{ + /** @return Collection */ + public function foo(): Collection + { + /** @var Collection */ + return new Collection(); + } +} diff --git a/tests/PHPStan/Rules/Methods/data/fix-with-tabs.php.fixed b/tests/PHPStan/Rules/Methods/data/fix-with-tabs.php.fixed new file mode 100644 index 0000000000..1d46bb3a2b --- /dev/null +++ b/tests/PHPStan/Rules/Methods/data/fix-with-tabs.php.fixed @@ -0,0 +1,29 @@ + */ + public function foo(): Collection; +} + +interface BarI +{ + +} +class Bar implements BarI {} + +/** @template-coveriant TValue */ +class Collection {} + + +class Baz implements FooInterface +{ + /** @return Collection */ + #[\Override] + public function foo(): Collection + { + /** @var Collection */ + return new Collection(); + } +}