Skip to content

Commit

Permalink
[Core] Fix crash indentation on indent(\t, 1) config (#3155)
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Dec 5, 2022
1 parent f4ff24c commit 3f1a285
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/PhpParser/Printer/BetterStandardPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ public function __construct(
$this->insertionMap['Stmt_Function->returnType'] = [')', false, ': ', null];
$this->insertionMap['Expr_Closure->returnType'] = [')', false, ': ', null];
$this->insertionMap['Expr_ArrowFunction->returnType'] = [')', false, ': ', null];

$this->tabOrSpaceIndentCharacter = $this->rectorConfigProvider->getIndentChar();
}

/**
Expand All @@ -104,8 +106,6 @@ public function printFormatPreserving(array $stmts, array $origStmts, array $ori
{
$newStmts = $this->resolveNewStmts($stmts);

$this->tabOrSpaceIndentCharacter = $this->rectorConfigProvider->getIndentChar();

$content = parent::printFormatPreserving($newStmts, $origStmts, $origTokens);

// add new line in case of added stmts
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Rector\Core\Tests\Issues\IndentationCrash\FixtureTab;

final class IfElseTabIndentation
{
/**
* @param string[]|string $args
*
* @return void
*/
public function run( $args ) {
if ( is_array( $args ) ) {
foreach ( $args as $data ) {
echo $data;
}
} else {
echo $args;
}
}
}
32 changes: 32 additions & 0 deletions tests/Issues/IndentationCrash/TabIndentationCrashTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Rector\Core\Tests\Issues\IndentationCrash;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class TabIndentationCrashTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

/**
* @return Iterator<array<string>>
*/
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/FixtureTab');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/tab_configured_rule.php';
}
}
11 changes: 11 additions & 0 deletions tests/Issues/IndentationCrash/config/tab_configured_rule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\DeadCode\Rector\If_\SimplifyIfElseWithSameContentRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->indent("\t", 1);
$rectorConfig->rule(SimplifyIfElseWithSameContentRector::class);
};

0 comments on commit 3f1a285

Please sign in to comment.