|
@@ -141,6 +141,7 @@ if ($foo) {
|
|
|
T_TRAIT,
|
|
|
T_EXTENDS,
|
|
|
T_IMPLEMENTS,
|
|
|
+ T_CONST,
|
|
|
];
|
|
|
$controlStructurePossibiblyWithoutBracesTokens = [
|
|
|
T_IF,
|
|
@@ -171,20 +172,6 @@ if ($foo) {
|
|
|
$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',
|
|
@@ -192,6 +179,7 @@ if ($foo) {
|
|
|
* end_index: int,
|
|
|
* end_index_inclusive: bool,
|
|
|
* initial_indent: string,
|
|
|
+ * new_indent?: string,
|
|
|
* is_indented_block: bool,
|
|
|
* }> $scopes
|
|
|
*/
|
|
@@ -293,7 +281,27 @@ if ($foo) {
|
|
|
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;
|
|
|
|
|
@@ -347,30 +355,13 @@ if ($foo) {
|
|
|
'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) {
|
|
@@ -391,7 +382,7 @@ if ($foo) {
|
|
|
'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;
|
|
@@ -568,6 +559,7 @@ if ($foo) {
|
|
|
'end_index_inclusive' => false,
|
|
|
'initial_indent' => $previousLineInitialIndent,
|
|
|
'new_indent' => $previousLineNewIndent,
|
|
|
+ 'is_indented_block' => true,
|
|
|
];
|
|
|
}
|
|
|
}
|
|
@@ -709,6 +701,37 @@ if ($foo) {
|
|
|
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.
|