Skip to content

Commit

Permalink
bug: Add attributes support to statement_indentation (#6429)
Browse files Browse the repository at this point in the history
Add attributes support to `statement_indentation`
  • Loading branch information
julienfalque authored Jul 10, 2022
1 parent 27d4eee commit 9abfac4
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/Fixer/Whitespace/StatementIndentationFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
$blockSignatureFirstTokens[] = T_MATCH;
}

$blockFirstTokens = ['{', [CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN], [T_EXTENDS], [T_IMPLEMENTS], [CT::T_USE_TRAIT], [CT::T_GROUP_IMPORT_BRACE_OPEN]];
if (\defined('T_ATTRIBUTE')) { // @TODO: drop condition when PHP 8.0+ is required
$blockFirstTokens[] = [T_ATTRIBUTE];
}

$endIndex = \count($tokens) - 1;
if ($tokens[$endIndex]->isWhitespace()) {
--$endIndex;
Expand Down Expand Up @@ -139,7 +144,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
}

if (
$token->equalsAny(['{', [CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN], [T_EXTENDS], [T_IMPLEMENTS], [CT::T_USE_TRAIT], [CT::T_GROUP_IMPORT_BRACE_OPEN]])
$token->equalsAny($blockFirstTokens)
|| ($token->equals('(') && !$tokens[$tokens->getPrevMeaningfulToken($index)]->isGivenKind(T_ARRAY))
|| isset($alternativeBlockStarts[$index])
|| isset($caseBlockStarts[$index])
Expand All @@ -158,11 +163,12 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
$endIndex = $tokens->getNextTokenOfKind($index, [[CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE]]);
} elseif ($token->isGivenKind(CT::T_GROUP_IMPORT_BRACE_OPEN)) {
$endIndex = $tokens->getNextTokenOfKind($index, [[CT::T_GROUP_IMPORT_BRACE_CLOSE]]);
} elseif ($token->equals('{')) {
$endIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $index);
} elseif ($token->equals('(')) {
$endIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $index);
} else {
$endIndex = $tokens->findBlockEnd(
$token->equals('{') ? Tokens::BLOCK_TYPE_CURLY_BRACE : Tokens::BLOCK_TYPE_PARENTHESIS_BRACE,
$index
);
$endIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_ATTRIBUTE, $index);
}

if ('block_signature' === $scopes[$currentScope]['type']) {
Expand Down Expand Up @@ -356,7 +362,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
--$currentScope;
}

if ($token->equalsAny([';', ',', '}', [T_OPEN_TAG], [T_CLOSE_TAG]])) {
if ($token->equalsAny([';', ',', '}', [T_OPEN_TAG], [T_CLOSE_TAG], [CT::T_ATTRIBUTE_CLOSE]])) {
continue;
}

Expand Down
18 changes: 18 additions & 0 deletions tests/Fixer/Basic/BracesFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2589,6 +2589,24 @@ function D() /**
}',
self::$configurationOopPositionSameLine,
],
[
'<?php
class Foo
{
#[Baz]
public function bar()
{
}
}',
'<?php
class Foo
{
#[Baz]
public function bar()
{
}
}',
],
];
}

Expand Down
45 changes: 45 additions & 0 deletions tests/Fixer/Whitespace/ArrayIndentationFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,51 @@ public function provideFixWithTabsCases(): array
]);
}

/**
* @dataProvider provideFixPhp80Cases
* @requires PHP 8.0
*/
public function testFixPhp80(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}

public function provideFixPhp80Cases()
{
yield 'attribute' => [
'<?php
class Foo {
#[SimpleAttribute]
#[ComplexAttribute(
foo: true,
bar: [
1,
2,
3,
]
)]
public function bar()
{
}
}',
'<?php
class Foo {
#[SimpleAttribute]
#[ComplexAttribute(
foo: true,
bar: [
1,
2,
3,
]
)]
public function bar()
{
}
}',
];
}

private function withLongArraySyntaxCases(array $cases): array
{
$longSyntaxCases = [];
Expand Down
39 changes: 39 additions & 0 deletions tests/Fixer/Whitespace/StatementIndentationFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,45 @@ public function provideFixPhp80Cases(): iterable
default => throw new Exception(),
};',
];

yield 'attribute' => [
'<?php
class Foo {
#[SimpleAttribute]
#[
MultilineAttribute
]
#[ComplexAttribute(
foo: true,
bar: [
1,
2,
3,
]
)]
public function bar()
{
}
}',
'<?php
class Foo {
#[SimpleAttribute]
#[
MultilineAttribute
]
#[ComplexAttribute(
foo: true,
bar: [
1,
2,
3,
]
)]
public function bar()
{
}
}',
];
}

/**
Expand Down

0 comments on commit 9abfac4

Please sign in to comment.