Skip to content

Fix "Option 'indent' must either be all spaces or a single tab" #4070

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: 2.1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/Analyser/RuleErrorTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 8 additions & 0 deletions tests/PHPStan/Rules/Methods/OverridingMethodRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

}
28 changes: 28 additions & 0 deletions tests/PHPStan/Rules/Methods/data/fix-with-tabs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php declare(strict_types = 1);

namespace FixWithTabs;

interface FooInterface {
/** @return Collection<BarI> */
public function foo(): Collection;
}

interface BarI
{

}
class Bar implements BarI {}

/** @template-coveriant TValue */
class Collection {}


class Baz implements FooInterface
{
/** @return Collection<Bar> */
public function foo(): Collection
{
/** @var Collection<Bar> */
return new Collection();
}
}
29 changes: 29 additions & 0 deletions tests/PHPStan/Rules/Methods/data/fix-with-tabs.php.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php declare(strict_types = 1);

namespace FixWithTabs;

interface FooInterface {
/** @return Collection<BarI> */
public function foo(): Collection;
}

interface BarI
{

}
class Bar implements BarI {}

/** @template-coveriant TValue */
class Collection {}


class Baz implements FooInterface
{
/** @return Collection<Bar> */
#[\Override]
public function foo(): Collection
{
/** @var Collection<Bar> */
return new Collection();
}
}
Loading