Skip to content

Commit de03964

Browse files
authored
Introduce NoMultilineReturnFixer (#6)
The `return` keyword and its expression should be on the same line for readability. This fixer replaces multiline returns with a single space between `return` and the expression.
1 parent d77662e commit de03964

2 files changed

Lines changed: 81 additions & 0 deletions

File tree

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Ticketswap\PhpCsFixerConfig\Fixer;
6+
7+
use Override;
8+
use PhpCsFixer\AbstractFixer;
9+
use PhpCsFixer\FixerDefinition\CodeSample;
10+
use PhpCsFixer\FixerDefinition\FixerDefinition;
11+
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
12+
use PhpCsFixer\Tokenizer\Token;
13+
use PhpCsFixer\Tokenizer\Tokens;
14+
use SplFileInfo;
15+
16+
final class NoMultilineReturnFixer extends AbstractFixer
17+
{
18+
#[Override]
19+
public function getDefinition() : FixerDefinitionInterface
20+
{
21+
return new FixerDefinition(
22+
'The `return` keyword and the expression it returns must be on the same line.',
23+
[
24+
new CodeSample(
25+
<<<'EOL'
26+
<?php
27+
28+
function foo()
29+
{
30+
return
31+
$bar->baz();
32+
}
33+
34+
EOL,
35+
),
36+
],
37+
);
38+
}
39+
40+
#[Override]
41+
public function isCandidate(Tokens $tokens) : bool
42+
{
43+
return $tokens->isTokenKindFound(T_RETURN);
44+
}
45+
46+
#[Override]
47+
protected function applyFix(SplFileInfo $file, Tokens $tokens) : void
48+
{
49+
for ($index = $tokens->count() - 1; $index >= 0; --$index) {
50+
if ( ! $tokens[$index]->isGivenKind(T_RETURN)) {
51+
continue;
52+
}
53+
54+
$nextIndex = $index + 1;
55+
56+
if ( ! isset($tokens[$nextIndex])) {
57+
continue;
58+
}
59+
60+
if ( ! $tokens[$nextIndex]->isGivenKind(T_WHITESPACE)) {
61+
continue;
62+
}
63+
64+
$whitespace = $tokens[$nextIndex]->getContent();
65+
66+
if ( ! str_contains($whitespace, "\n")) {
67+
continue;
68+
}
69+
70+
$nextMeaningfulIndex = $tokens->getNextMeaningfulToken($index);
71+
72+
if ($nextMeaningfulIndex === null || $tokens[$nextMeaningfulIndex]->equals(';')) {
73+
continue;
74+
}
75+
76+
$tokens[$nextIndex] = new Token([T_WHITESPACE, ' ']);
77+
}
78+
}
79+
}

src/RuleSet/TicketSwapRuleSet.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use Symplify\CodingStandard\TokenRunner\Whitespace\IndentResolver;
2727
use Symplify\CodingStandard\TokenRunner\Wrapper\FixerWrapper\ArrayWrapperFactory;
2828
use Ticketswap\PhpCsFixerConfig\Fixer\AttributesNewLineFixer;
29+
use Ticketswap\PhpCsFixerConfig\Fixer\NoMultilineReturnFixer;
2930
use Ticketswap\PhpCsFixerConfig\Fixer\PhpdocAboveAttributeFixer;
3031
use Ticketswap\PhpCsFixerConfig\Fixers;
3132
use Ticketswap\PhpCsFixerConfig\NameWrapper;
@@ -57,6 +58,7 @@ public static function create() : RuleSet
5758
new Fixers(
5859
new LineBreakAfterStatementsFixer(),
5960
new NameWrapper(new AttributesNewLineFixer()),
61+
new NameWrapper(new NoMultilineReturnFixer()),
6062
new NameWrapper(new PhpdocAboveAttributeFixer()),
6163
new NameWrapper(new ArrayListItemNewlineFixer($arrayItemNewliner, $arrayAnalyzer, $arrayBlockInfoFinder)),
6264
new NameWrapper(new ArrayOpenerAndCloserNewlineFixer($arrayBlockInfoFinder, $whitespacesFixerConfig, $arrayAnalyzer)),

0 commit comments

Comments
 (0)