Skip to content

Commit 256fa8b

Browse files
committed
Add consistent multiline method chain fixer with Pest expectation support
1 parent 3bd8cae commit 256fa8b

2 files changed

Lines changed: 53 additions & 3 deletions

File tree

‎src/Fixer/MultilineMethodChainingFixer.php‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public function isCandidate(Tokens $tokens): bool
5050
protected function applyFix(SplFileInfo $file, Tokens $tokens): void
5151
{
5252
$lineEnding = $this->whitespacesConfig->getLineEnding();
53+
$isTestFile = str_ends_with($file->getFilename(), 'Test.php');
5354

5455
for ($index = 1, $count = count($tokens); $index < $count; $index++) {
5556
if (!$tokens[$index]->isObjectOperator() || $this->startsOnNewLine($tokens, $index)) {
@@ -58,7 +59,11 @@ protected function applyFix(SplFileInfo $file, Tokens $tokens): void
5859

5960
$previousOperatorIndex = $this->previousChainObjectOperator($tokens, $index);
6061

61-
if ($previousOperatorIndex === null || !$this->startsOnNewLine($tokens, $previousOperatorIndex)) {
62+
if (
63+
$previousOperatorIndex === null
64+
|| !$this->startsOnNewLine($tokens, $previousOperatorIndex)
65+
|| $isTestFile && $this->isAndExpectationSegment($tokens, $previousOperatorIndex)
66+
) {
6267
continue;
6368
}
6469

@@ -76,6 +81,15 @@ protected function applyFix(SplFileInfo $file, Tokens $tokens): void
7681
}
7782
}
7883

84+
private function isAndExpectationSegment(Tokens $tokens, int $operatorIndex): bool
85+
{
86+
$methodNameIndex = $tokens->getNextMeaningfulToken($operatorIndex);
87+
88+
return $methodNameIndex !== null
89+
&& $tokens[$methodNameIndex]->isGivenKind(T_STRING)
90+
&& strtolower($tokens[$methodNameIndex]->getContent()) === 'and';
91+
}
92+
7993
private function previousChainObjectOperator(Tokens $tokens, int $index): ?int
8094
{
8195
$segmentEndIndex = $tokens->getPrevMeaningfulToken($index);

‎tests/Unit/MultilineMethodChainingFixerTest.php‎

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,46 @@ public function testNestedArgumentChainsDoNotMakeTheOuterChainMultiline(): void
9090
$this->assertSame($source, $this->fix($source));
9191
}
9292

93-
private function fix(string $source): string
93+
public function testItKeepsPestAndExpectationSegmentsTogetherInTestFiles(): void
94+
{
95+
$source = <<<'PHP'
96+
<?php
97+
98+
expect($first)->toBeTrue()
99+
->and($second)->toBeFalse()
100+
->and($third)->toBeNull();
101+
102+
PHP;
103+
104+
$this->assertSame($source, $this->fix($source));
105+
}
106+
107+
public function testItStillMovesSegmentsAfterAndMethodsOutsideTestFiles(): void
108+
{
109+
$source = <<<'PHP'
110+
<?php
111+
112+
$result = $query->first()
113+
->and($other)->finish();
114+
115+
PHP;
116+
117+
$expected = <<<'PHP'
118+
<?php
119+
120+
$result = $query->first()
121+
->and($other)
122+
->finish();
123+
124+
PHP;
125+
126+
$this->assertSame($expected, $this->fix($source, 'Source.php'));
127+
}
128+
129+
private function fix(string $source, string $filename = __FILE__): string
94130
{
95131
$tokens = Tokens::fromCode($source);
96-
$file = new SplFileInfo(__FILE__);
132+
$file = new SplFileInfo($filename);
97133

98134
(new MultilineMethodChainingFixer)->fix($file, $tokens);
99135
(new MethodChainingIndentationFixer)->fix($file, $tokens);

0 commit comments

Comments
 (0)