Skip to content

Commit

Permalink
fix: Correctly indent multiline constants and properties
Browse files Browse the repository at this point in the history
  • Loading branch information
julienfalque committed Mar 10, 2024
1 parent f256c85 commit b2ea810
Show file tree
Hide file tree
Showing 2 changed files with 326 additions and 34 deletions.
91 changes: 57 additions & 34 deletions src/Fixer/Whitespace/StatementIndentationFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
T_TRAIT,
T_EXTENDS,
T_IMPLEMENTS,
T_CONST,
];
$controlStructurePossibiblyWithoutBracesTokens = [
T_IF,
Expand Down Expand Up @@ -171,27 +172,14 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
$this->extractIndent($this->computeNewLineContent($tokens, 0)),
);

$methodModifierTokens = [
// https://github.com/php/php-langspec/blob/master/spec/19-grammar.md#grammar-visibility-modifier
T_PUBLIC,
T_PROTECTED,
T_PRIVATE,
// https://github.com/php/php-langspec/blob/master/spec/19-grammar.md#grammar-static-modifier
T_STATIC,
// https://github.com/php/php-langspec/blob/master/spec/19-grammar.md#grammar-class-modifier
T_ABSTRACT,
T_FINAL,
];

$methodModifierIndents = [];

/**
* @var list<array{
* type: 'block'|'block_signature'|'statement',
* skip: bool,
* end_index: int,
* end_index_inclusive: bool,
* initial_indent: string,
* new_indent?: string,
* is_indented_block: bool,
* }> $scopes
*/
Expand Down Expand Up @@ -293,7 +281,27 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
continue;
}

if ($token->isGivenKind($blockSignatureFirstTokens)) {
if (
$token->isGivenKind(CT::T_ARRAY_SQUARE_BRACE_OPEN)
|| ($token->equals('(') && $tokens[$tokens->getPrevMeaningfulToken($index)]->isGivenKind(T_ARRAY))
) {
$blockType = $token->equals('(') ? Tokens::BLOCK_TYPE_PARENTHESIS_BRACE : Tokens::BLOCK_TYPE_ARRAY_SQUARE_BRACE;

$scopes[] = [
'type' => 'statement',
'skip' => true,
'end_index' => $tokens->findBlockEnd($blockType, $index),
'end_index_inclusive' => true,
'initial_indent' => $previousLineInitialIndent,
'new_indent' => $previousLineNewIndent,
'is_indented_block' => false,
];

continue;
}

$isPropertyStart = $this->isPropertyStart($tokens, $index);
if ($isPropertyStart || $token->isGivenKind($blockSignatureFirstTokens)) {
$lastWhitespaceIndex = null;
$closingParenthesisIndex = null;

Expand Down Expand Up @@ -347,30 +355,13 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
'end_index' => $endIndex,
'end_index_inclusive' => true,
'initial_indent' => $this->getLineIndentationWithBracesCompatibility($tokens, $index, $lastIndent),
'is_indented_block' => $token->isGivenKind([T_EXTENDS, T_IMPLEMENTS]),
'is_indented_block' => $isPropertyStart || $token->isGivenKind([T_EXTENDS, T_IMPLEMENTS, T_CONST]),
];

continue;
}

if ($token->isGivenKind($methodModifierTokens)) {
$methodModifierIndents[$index] = $lastIndent;

continue;
}

if ($token->isGivenKind(T_FUNCTION)) {
$x = $tokens->getPrevMeaningfulToken($index);
while (
null !== $x
&& $tokens[$x]->isGivenKind($methodModifierTokens)
&& \array_key_exists($x, $methodModifierIndents)
) {
$lastIndent = $methodModifierIndents[$x];
$x = $tokens->getPrevMeaningfulToken($x);
}

$methodModifierIndents = [];
$endIndex = $index + 1;

for ($max = \count($tokens); $endIndex < $max; ++$endIndex) {
Expand All @@ -391,7 +382,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
'end_index' => $endIndex,
'end_index_inclusive' => true,
'initial_indent' => $this->getLineIndentationWithBracesCompatibility($tokens, $index, $lastIndent),
'is_indented_block' => $token->isGivenKind([T_EXTENDS, T_IMPLEMENTS]),
'is_indented_block' => false,
];

continue;
Expand Down Expand Up @@ -568,6 +559,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
'end_index_inclusive' => false,
'initial_indent' => $previousLineInitialIndent,
'new_indent' => $previousLineNewIndent,
'is_indented_block' => true,
];
}
}
Expand Down Expand Up @@ -709,6 +701,37 @@ private function getLineIndentationWithBracesCompatibility(Tokens $tokens, int $
return $regularIndent;
}

/**
* Returns whether the token at given index is the last token in a property
* declaration before the type or the name of that property.
*/
private function isPropertyStart(Tokens $tokens, int $index): bool
{
$propertyKeywords = [T_VAR, T_PUBLIC, T_PROTECTED, T_PRIVATE, T_STATIC];
if (\defined('T_READONLY')) { // @TODO: drop condition when PHP 8.1+ is required
$propertyKeywords[] = T_READONLY;
}

$nextIndex = $tokens->getNextMeaningfulToken($index);
if (
null === $nextIndex
|| $tokens[$nextIndex]->isGivenKind($propertyKeywords)
|| $tokens[$nextIndex]->isGivenKind([T_CONST, T_FUNCTION])
) {
return false;
}

while ($tokens[$index]->isGivenKind($propertyKeywords)) {
if ($tokens[$index]->isGivenKind([T_VAR, T_PUBLIC, T_PROTECTED, T_PRIVATE])) {
return true;
}

$index = $tokens->getPrevMeaningfulToken($index);
}

return false;
}

/**
* Returns whether the token at given index is a comment whose indentation
* can be fixed.
Expand Down

0 comments on commit b2ea810

Please sign in to comment.