Skip to content

Commit d8abaa2

Browse files
committed
Add multiline method chaining fixer with Pest assertion exceptions
1 parent 256fa8b commit d8abaa2

3 files changed

Lines changed: 19 additions & 3 deletions

File tree

‎README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,5 @@ Once a method chain continues on a new line, each subsequent segment also starts
128128
+ ->filter()
129129
+ ->values();
130130
```
131+
132+
In `*Test.php` files, Pest expectation segments such as `->and(...)->...` and `->not->...` stay together.

‎src/Fixer/MultilineMethodChainingFixer.php‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ protected function applyFix(SplFileInfo $file, Tokens $tokens): void
6262
if (
6363
$previousOperatorIndex === null
6464
|| !$this->startsOnNewLine($tokens, $previousOperatorIndex)
65-
|| $isTestFile && $this->isAndExpectationSegment($tokens, $previousOperatorIndex)
65+
|| $isTestFile && $this->isPestExpectationSegment($tokens, $previousOperatorIndex)
6666
) {
6767
continue;
6868
}
@@ -81,13 +81,13 @@ protected function applyFix(SplFileInfo $file, Tokens $tokens): void
8181
}
8282
}
8383

84-
private function isAndExpectationSegment(Tokens $tokens, int $operatorIndex): bool
84+
private function isPestExpectationSegment(Tokens $tokens, int $operatorIndex): bool
8585
{
8686
$methodNameIndex = $tokens->getNextMeaningfulToken($operatorIndex);
8787

8888
return $methodNameIndex !== null
8989
&& $tokens[$methodNameIndex]->isGivenKind(T_STRING)
90-
&& strtolower($tokens[$methodNameIndex]->getContent()) === 'and';
90+
&& in_array(strtolower($tokens[$methodNameIndex]->getContent()), ['and', 'not'], true);
9191
}
9292

9393
private function previousChainObjectOperator(Tokens $tokens, int $index): ?int

‎tests/Unit/MultilineMethodChainingFixerTest.php‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,20 @@ public function testItKeepsPestAndExpectationSegmentsTogetherInTestFiles(): void
9999
->and($second)->toBeFalse()
100100
->and($third)->toBeNull();
101101

102+
PHP;
103+
104+
$this->assertSame($source, $this->fix($source));
105+
}
106+
107+
public function testItKeepsPestNotExpectationSegmentsTogetherInTestFiles(): void
108+
{
109+
$source = <<<'PHP'
110+
<?php
111+
112+
expect($value)
113+
->toContain('included')
114+
->not->toContain('excluded');
115+
102116
PHP;
103117

104118
$this->assertSame($source, $this->fix($source));

0 commit comments

Comments
 (0)