Skip to content

Commit

Permalink
[CS] Fix dynamic and broken indent detection, allow to configure spac…
Browse files Browse the repository at this point in the history
…ing via RectorConfig::indent() method
  • Loading branch information
TomasVotruba committed Jun 6, 2022
1 parent 40d9102 commit 05d1996
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 38 deletions.
18 changes: 18 additions & 0 deletions SomeClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Foo;

class Bar
{
/**
* A fine comment here.
*
* @throws \RuntimeException
* @throws \InvalidArgumentException
*/
public function foo(): string
{
// This line contains a single \t after the constant and before the comma
var_dump($ch, CURLOPT_TIMEOUT , 60);
}
}
1 change: 1 addition & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@

$rectorConfig->disableImportNames();
$rectorConfig->importShortClasses();
$rectorConfig->indent(' ', 4);

$rectorConfig->fileExtensions(['php']);
$rectorConfig->nestedChainMethodCallLimit(60);
Expand Down
10 changes: 10 additions & 0 deletions packages/Config/RectorConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,4 +230,14 @@ public function cacheClass(string $cacheClass): void
$parameters = $this->parameters();
$parameters->set(Option::CACHE_CLASS, $cacheClass);
}

/**
* @see https://github.com/nikic/PHP-Parser/issues/723#issuecomment-712401963
*/
public function indent(string $character, int $count): void
{
$parameters = $this->parameters();
$parameters->set(Option::INDENT_CHAR, $character);
$parameters->set(Option::INDENT_SIZE, $count);
}
}
12 changes: 12 additions & 0 deletions src/Configuration/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,16 @@ final class Option
* @var string
*/
public const MEMORY_LIMIT = 'memory-limit';

/**
* @deprecated Use @see \Rector\Config\RectorConfig::indent() method
* @var string
*/
public const INDENT_CHAR = 'indent-char';

/**
* @deprecated Use @see \Rector\Config\RectorConfig::indent() method
* @var string
*/
public const INDENT_SIZE = 'indent-size';
}
10 changes: 10 additions & 0 deletions src/Configuration/RectorConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,14 @@ public function getSymfonyContainerXml(): string
{
return $this->parameterProvider->provideStringParameter(Option::SYMFONY_CONTAINER_XML_PATH_PARAMETER);
}

public function getIndentChar(): string
{
return $this->parameterProvider->provideStringParameter(Option::INDENT_CHAR);
}

public function getIndentSize(): int
{
return $this->parameterProvider->provideIntParameter(Option::INDENT_SIZE);
}
}
7 changes: 3 additions & 4 deletions src/PhpParser/Printer/BetterStandardPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
use PhpParser\Node\Stmt\Use_;
use PhpParser\PrettyPrinter\Standard;
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
use Rector\Core\Configuration\RectorConfigProvider;
use Rector\Core\Contract\PhpParser\NodePrinterInterface;
use Rector\Core\PhpParser\Node\CustomNode\FileWithoutNamespace;
use Rector\Core\PhpParser\Printer\Whitespace\IndentCharacterDetector;
use Rector\Core\Util\StringUtils;
use Rector\NodeTypeResolver\Node\AttributeKey;

Expand Down Expand Up @@ -80,8 +80,8 @@ final class BetterStandardPrinter extends Standard implements NodePrinterInterfa
* @param mixed[] $options
*/
public function __construct(
private readonly IndentCharacterDetector $indentCharacterDetector,
private readonly DocBlockUpdater $docBlockUpdater,
private readonly RectorConfigProvider $rectorConfigProvider,
array $options = []
) {
parent::__construct($options);
Expand All @@ -103,8 +103,7 @@ public function printFormatPreserving(array $stmts, array $origStmts, array $ori
{
$newStmts = $this->resolveNewStmts($stmts);

// detect per print
$this->tabOrSpaceIndentCharacter = $this->indentCharacterDetector->detect($origTokens);
$this->tabOrSpaceIndentCharacter = $this->rectorConfigProvider->getIndentChar();

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

Expand Down
34 changes: 0 additions & 34 deletions src/PhpParser/Printer/Whitespace/IndentCharacterDetector.php

This file was deleted.

0 comments on commit 05d1996

Please sign in to comment.