Skip to content

Commit

Permalink
bug: Fix brace location after multiline function signature (#6475)
Browse files Browse the repository at this point in the history
CurlyBracesPositionFixer - Fix for union types
  • Loading branch information
jrmajor committed Jul 12, 2022
1 parent c91c354 commit b957583
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Fixer/Basic/CurlyBracesPositionFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
$previousTokenIndex = $openBraceIndex;
do {
$previousTokenIndex = $tokens->getPrevMeaningfulToken($previousTokenIndex);
} while ($tokens[$previousTokenIndex]->isGivenKind([CT::T_TYPE_COLON, CT::T_NULLABLE_TYPE, T_STRING, T_NS_SEPARATOR, CT::T_ARRAY_TYPEHINT]));
} while ($tokens[$previousTokenIndex]->isGivenKind([CT::T_TYPE_COLON, CT::T_NULLABLE_TYPE, T_STRING, T_NS_SEPARATOR, CT::T_ARRAY_TYPEHINT, CT::T_TYPE_ALTERNATION, CT::T_TYPE_INTERSECTION]));

if ($tokens[$previousTokenIndex]->equals(')')) {
if ($tokens[--$previousTokenIndex]->isComment()) {
Expand Down
51 changes: 51 additions & 0 deletions tests/Fixer/Basic/CurlyBracesPositionFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -645,4 +645,55 @@ function foo(
}',
];
}

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

public function provideFix80Cases(): iterable
{
yield 'function (multiline + union return)' => [
'<?php
function sum(
int|float $first,
int|float $second,
): int|float {
}',
];

yield 'function (multiline + union return with whitespace)' => [
'<?php
function sum(
int|float $first,
int|float $second,
): int | float {
}',
];
}

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

public function provideFix81Cases(): iterable
{
yield 'function (multiline + intersection return)' => [
'<?php
function foo(
mixed $bar,
mixed $baz,
): Foo&Bar {
}',
];
}
}

0 comments on commit b957583

Please sign in to comment.