Skip to content

Commit

Permalink
Make indent size and char fetchable (#4762)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Aug 11, 2023
1 parent 1659ca2 commit a29e3af
Showing 1 changed file with 18 additions and 19 deletions.
37 changes: 18 additions & 19 deletions src/PhpParser/Printer/BetterStandardPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,12 @@ final class BetterStandardPrinter extends Standard
*/
private const REPLACE_COLON_WITH_SPACE_REGEX = '#(^.*function .*\(.*\)) : #';

/**
* Use space by default
*/
private string $tabOrSpaceIndentCharacter = ' ';

/**
* @param mixed[] $options
*/
public function __construct(
private readonly DocBlockUpdater $docBlockUpdater,
array $options = []
private readonly DocBlockUpdater $docBlockUpdater
) {
parent::__construct($options);
parent::__construct([
'shortArraySyntax' => true,
]);

// print return type double colon right after the bracket "function(): string"
$this->initializeInsertionMap();
Expand All @@ -94,8 +87,6 @@ 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 = SimpleParameterProvider::provideStringParameter(Option::INDENT_CHAR, ' ');
}

/**
Expand Down Expand Up @@ -184,8 +175,8 @@ protected function pExpr_ArrowFunction(ArrowFunction $arrowFunction): string

$indentSize = SimpleParameterProvider::provideIntParameter(Option::INDENT_SIZE);

$indent = str_repeat($this->tabOrSpaceIndentCharacter, $this->indentLevel) .
str_repeat($this->tabOrSpaceIndentCharacter, $indentSize);
$indent = str_repeat($this->getIndentCharacter(), $this->indentLevel) .
str_repeat($this->getIndentCharacter(), $indentSize);

$text = "\n" . $indent;
foreach ($comments as $key => $comment) {
Expand All @@ -211,7 +202,7 @@ protected function setIndentLevel(int $level): void
{
$level = max($level, 0);
$this->indentLevel = $level;
$this->nl = "\n" . str_repeat($this->tabOrSpaceIndentCharacter, $level);
$this->nl = "\n" . str_repeat($this->getIndentCharacter(), $level);
}

/**
Expand All @@ -222,15 +213,15 @@ protected function indent(): void
$indentSize = SimpleParameterProvider::provideIntParameter(Option::INDENT_SIZE);

$this->indentLevel += $indentSize;
$this->nl .= str_repeat($this->tabOrSpaceIndentCharacter, $indentSize);
$this->nl .= str_repeat($this->getIndentCharacter(), $indentSize);
}

/**
* This allows to use both spaces and tabs vs. original space-only
*/
protected function outdent(): void
{
if ($this->tabOrSpaceIndentCharacter === ' ') {
if ($this->getIndentCharacter() === ' ') {
// - 4 spaces
assert($this->indentLevel >= 4);
$this->indentLevel -= 4;
Expand All @@ -240,7 +231,7 @@ protected function outdent(): void
--$this->indentLevel;
}

$this->nl = "\n" . str_repeat($this->tabOrSpaceIndentCharacter, $this->indentLevel);
$this->nl = "\n" . str_repeat($this->getIndentCharacter(), $this->indentLevel);
}

/**
Expand Down Expand Up @@ -522,6 +513,14 @@ protected function pParam(Param $param): string
. ($param->default instanceof Expr ? ' = ' . $this->p($param->default) : '');
}

/**
* Must be a method to be able to react to changed parameter in tests
*/
private function getIndentCharacter(): string
{
return SimpleParameterProvider::provideStringParameter(Option::INDENT_CHAR, ' ');
}

private function shouldPrintNewRawValue(LNumber|DNumber $lNumber): bool
{
return $lNumber->getAttribute(AttributeKey::REPRINT_RAW_VALUE) === true;
Expand Down

0 comments on commit a29e3af

Please sign in to comment.