Skip to content

Commit 11e80d7

Browse files
authored
Fix "Option 'indent' must either be all spaces or a single tab"
1 parent 1d12f65 commit 11e80d7

File tree

4 files changed

+72
-1
lines changed

4 files changed

+72
-1
lines changed

src/Analyser/RuleErrorTransformer.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder;
3030
use function get_class;
3131
use function sha1;
32+
use function str_contains;
3233
use function str_repeat;
3334

3435
#[AutowiredService]
@@ -137,7 +138,12 @@ public function transform(
137138
$newStmts = $traverser->traverse($newStmts);
138139

139140
if ($visitor->isFound()) {
140-
$printer = new PhpPrinter(['indent' => str_repeat($indentDetector->indentCharacter, $indentDetector->indentSize)]);
141+
if (str_contains($indentDetector->indentCharacter, "\t")) {
142+
$indent = "\t";
143+
} else {
144+
$indent = str_repeat($indentDetector->indentCharacter, $indentDetector->indentSize);
145+
}
146+
$printer = new PhpPrinter(['indent' => $indent]);
141147
$newCode = $printer->printFormatPreserving($newStmts, $fileNodes, $oldTokens);
142148

143149
if ($oldCode !== $newCode) {

tests/PHPStan/Rules/Methods/OverridingMethodRuleTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,4 +820,12 @@ public function testFixOverride(): void
820820
$this->fix(__DIR__ . '/data/fix-override-attribute.php', __DIR__ . '/data/fix-override-attribute.php.fixed');
821821
}
822822

823+
#[RequiresPhp('>= 8.3')]
824+
public function testFixWithTabs(): void
825+
{
826+
$this->phpVersionId = PHP_VERSION_ID;
827+
$this->checkMissingOverrideMethodAttribute = true;
828+
$this->fix(__DIR__ . '/data/fix-with-tabs.php', __DIR__ . '/data/fix-with-tabs.php.fixed');
829+
}
830+
823831
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace FixWithTabs;
4+
5+
interface FooInterface {
6+
/** @return Collection<BarI> */
7+
public function foo(): Collection;
8+
}
9+
10+
interface BarI
11+
{
12+
13+
}
14+
class Bar implements BarI {}
15+
16+
/** @template-coveriant TValue */
17+
class Collection {}
18+
19+
20+
class Baz implements FooInterface
21+
{
22+
/** @return Collection<Bar> */
23+
public function foo(): Collection
24+
{
25+
/** @var Collection<Bar> */
26+
return new Collection();
27+
}
28+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace FixWithTabs;
4+
5+
interface FooInterface {
6+
/** @return Collection<BarI> */
7+
public function foo(): Collection;
8+
}
9+
10+
interface BarI
11+
{
12+
13+
}
14+
class Bar implements BarI {}
15+
16+
/** @template-coveriant TValue */
17+
class Collection {}
18+
19+
20+
class Baz implements FooInterface
21+
{
22+
/** @return Collection<Bar> */
23+
#[\Override]
24+
public function foo(): Collection
25+
{
26+
/** @var Collection<Bar> */
27+
return new Collection();
28+
}
29+
}

0 commit comments

Comments
 (0)