Skip to content

Commit

Permalink
Add new_line_for_chained_calls_ignore_rest strategy to MultilineWhite…
Browse files Browse the repository at this point in the history
…spaceBeforeSemicolonsFixer
  • Loading branch information
danog committed Mar 19, 2024
1 parent 1eb4e52 commit d95da5e
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
34 changes: 31 additions & 3 deletions src/Fixer/Semicolon/MultilineWhitespaceBeforeSemicolonsFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ final class MultilineWhitespaceBeforeSemicolonsFixer extends AbstractFixer imple
*/
public const STRATEGY_NEW_LINE_FOR_CHAINED_CALLS = 'new_line_for_chained_calls';

/**
* @internal
*/
public const STRATEGY_NEW_LINE_FOR_CHAINED_CALLS_IGNORE_REST = 'new_line_for_chained_calls_ignore_rest';

public function getDefinition(): FixerDefinitionInterface
{
return new FixerDefinition(
Expand All @@ -61,9 +66,25 @@ function foo() {
$object->method1()
->method2()
->method(3);
$someCond = $a === $b ||
$c === $d
;
',
['strategy' => self::STRATEGY_NEW_LINE_FOR_CHAINED_CALLS]
),
new CodeSample(
'<?php
$object->method1()
->method2()
->method(3);
$someCond = $a === $b ||
$c === $d
;
',
['strategy' => self::STRATEGY_NEW_LINE_FOR_CHAINED_CALLS_IGNORE_REST]
),
]
);
}
Expand Down Expand Up @@ -91,7 +112,11 @@ protected function createConfigurationDefinition(): FixerConfigurationResolverIn
'strategy',
'Forbid multi-line whitespace or move the semicolon to the new line for chained calls.'
))
->setAllowedValues([self::STRATEGY_NO_MULTI_LINE, self::STRATEGY_NEW_LINE_FOR_CHAINED_CALLS])
->setAllowedValues([
self::STRATEGY_NO_MULTI_LINE,
self::STRATEGY_NEW_LINE_FOR_CHAINED_CALLS,
self::STRATEGY_NEW_LINE_FOR_CHAINED_CALLS_IGNORE_REST,
])
->setDefault(self::STRATEGY_NO_MULTI_LINE)
->getOption(),
]);
Expand All @@ -110,7 +135,10 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
$previous = $tokens[$previousIndex];

$indent = $this->findWhitespaceBeforeFirstCall($index, $tokens);
if (self::STRATEGY_NEW_LINE_FOR_CHAINED_CALLS === $this->configuration['strategy'] && null !== $indent) {
if ((
self::STRATEGY_NEW_LINE_FOR_CHAINED_CALLS === $this->configuration['strategy']
|| self::STRATEGY_NEW_LINE_FOR_CHAINED_CALLS_IGNORE_REST === $this->configuration['strategy']
) && null !== $indent) {
if ($previous->isWhitespace() && $previous->getContent() === $lineEnding.$indent) {
continue;
}
Expand All @@ -129,7 +157,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void

// insert the new line with indented semicolon
$tokens->insertAt($index++, [$newline, new Token(';')]);
} else {
} elseif (self::STRATEGY_NEW_LINE_FOR_CHAINED_CALLS_IGNORE_REST !== $this->configuration['strategy']) {
if (!$previous->isWhitespace() || !str_contains($previous->getContent(), "\n")) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1146,6 +1146,42 @@ function foo($bar)
];
}

/**
* @dataProvider provideSemicolonForChainedCallsIgnoreRestFixCases
*/
public function testSemicolonForChainedCallsIgnoreRestFix(string $expected, ?string $input = null): void
{
$this->fixer->configure(['strategy' => MultilineWhitespaceBeforeSemicolonsFixer::STRATEGY_NEW_LINE_FOR_CHAINED_CALLS_IGNORE_REST]);
$this->doTest($expected, $input);
}

public static function provideSemicolonForChainedCallsIgnoreRestFixCases(): iterable
{
yield [
'<?php
$this
->method1()
->method2()
;
$cond = $a === 0 ||
$a === 1
;
?>',
'<?php
$this
->method1()
->method2();
$cond = $a === 0 ||
$a === 1
;
?>',
];
}

/**
* @dataProvider provideMessyWhitespacesSemicolonForChainedCallsCases
*/
Expand Down

0 comments on commit d95da5e

Please sign in to comment.