Skip to content

Commit b593360

Browse files
committed
[pipe] add custom printer to newline pipe always next line
1 parent 6da898b commit b593360

3 files changed

Lines changed: 25 additions & 4 deletions

File tree

rules-tests/Php85/Rector/StmtsAwareInterface/NestedToPipeOperatorRector/Fixture/basic.php.inc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,17 @@ $value = "hello world";
66
$result1 = function3($value);
77
$result2 = function2($result1);
88
$result = function1($result2);
9+
910
?>
1011
-----
1112
<?php
1213

1314
namespace Rector\Tests\Php85\Rector\StmtsAwareInterface\NestedToPipeOperatorRector\Fixture;
1415

1516
$value = "hello world";
16-
$result = $value |> function3(...) |> function2(...) |> function1(...);
17-
?>
17+
$result = $value
18+
|> function3(...)
19+
|> function2(...)
20+
|> function1(...);
21+
22+
?>

rules-tests/Php85/Rector/StmtsAwareInterface/NestedToPipeOperatorRector/Fixture/nested_func.php.inc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
namespace Rector\Tests\Php85\Rector\StmtsAwareInterface\NestedToPipeOperatorRector\Fixture;
44

55
$result = trim(strtoupper("Hello World"));
6+
67
?>
78
-----
89
<?php
910

1011
namespace Rector\Tests\Php85\Rector\StmtsAwareInterface\NestedToPipeOperatorRector\Fixture;
1112

12-
$result = strtoupper("Hello World") |> trim(...);
13-
?>
13+
$result = strtoupper("Hello World")
14+
|> trim(...);
15+
16+
?>

src/PhpParser/Printer/BetterStandardPrinter.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use PhpParser\Node\Expr\ArrowFunction;
1616
use PhpParser\Node\Expr\Assign;
1717
use PhpParser\Node\Expr\BinaryOp;
18+
use PhpParser\Node\Expr\BinaryOp\Pipe;
1819
use PhpParser\Node\Expr\CallLike;
1920
use PhpParser\Node\Expr\Instanceof_;
2021
use PhpParser\Node\Expr\Match_;
@@ -332,6 +333,18 @@ protected function pExpr_Array(Array_ $array): string
332333
return parent::pExpr_Array($array);
333334
}
334335

336+
protected function pExpr_BinaryOp_Pipe(Pipe $node, int $precedence, int $lhsPrecedence): string
337+
{
338+
return $this->pInfixOp(
339+
Pipe::class,
340+
$node->left,
341+
$this->nl . $this->resolveIndentSpaces() . '|> ',
342+
$node->right,
343+
$precedence,
344+
$lhsPrecedence
345+
);
346+
}
347+
335348
/**
336349
* Fixes escaping of regular patterns
337350
*/

0 commit comments

Comments
 (0)