From 0b60b27ac2246a87e78730d035e8b5e694947794 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Mon, 23 Jun 2025 10:00:00 +0200 Subject: [PATCH 1/2] Added regression test --- .../Methods/OverridingMethodRuleTest.php | 7 +++++ .../Rules/Methods/data/fix-with-tabs.php | 28 ++++++++++++++++++ .../Methods/data/fix-with-tabs.php.fixed | 29 +++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 tests/PHPStan/Rules/Methods/data/fix-with-tabs.php create mode 100644 tests/PHPStan/Rules/Methods/data/fix-with-tabs.php.fixed diff --git a/tests/PHPStan/Rules/Methods/OverridingMethodRuleTest.php b/tests/PHPStan/Rules/Methods/OverridingMethodRuleTest.php index e55c5011fd..7eafea3747 100644 --- a/tests/PHPStan/Rules/Methods/OverridingMethodRuleTest.php +++ b/tests/PHPStan/Rules/Methods/OverridingMethodRuleTest.php @@ -820,4 +820,11 @@ 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(); + } +} From fa46d089f76e5c8d1cbbf7fea0e6c0e7640925cd Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Mon, 23 Jun 2025 10:00:27 +0200 Subject: [PATCH 2/2] Fix "Option 'indent' must either be all spaces or a single tab" --- src/Analyser/RuleErrorTransformer.php | 8 +++++++- tests/PHPStan/Rules/Methods/OverridingMethodRuleTest.php | 1 + 2 files changed, 8 insertions(+), 1 deletion(-) 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 7eafea3747..cb2171e112 100644 --- a/tests/PHPStan/Rules/Methods/OverridingMethodRuleTest.php +++ b/tests/PHPStan/Rules/Methods/OverridingMethodRuleTest.php @@ -827,4 +827,5 @@ public function testFixWithTabs(): void $this->checkMissingOverrideMethodAttribute = true; $this->fix(__DIR__ . '/data/fix-with-tabs.php', __DIR__ . '/data/fix-with-tabs.php.fixed'); } + }