Skip to content

Commit c0b138c

Browse files
committed
downgrade pipe
1 parent ffa9fe4 commit c0b138c

File tree

4 files changed

+149
-0
lines changed

4 files changed

+149
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\DowngradePhp85\Rector\StmtsAwareInterface\DowngradePipeOperatorRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class DowngradePipeOperatorRectorTest extends AbstractRectorTestCase
12+
{
13+
#[DataProvider('provideData')]
14+
public function test(string $filePath): void
15+
{
16+
$this->doTestFile($filePath);
17+
}
18+
19+
public static function provideData(): Iterator
20+
{
21+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
22+
}
23+
24+
public function provideConfigFilePath(): string
25+
{
26+
return __DIR__ . '/config/configured_rule.php';
27+
}
28+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\DowngradePhp85\Rector\StmtsAwareInterface\DowngradePipeOperatorRector\Fixture;
6+
7+
$value = "hello world";
8+
$result = $value
9+
|> function3(...)
10+
|> function2(...)
11+
|> function1(...);
12+
13+
?>
14+
-----
15+
<?php
16+
17+
declare(strict_types=1);
18+
19+
namespace Rector\Tests\DowngradePhp85\Rector\StmtsAwareInterface\DowngradePipeOperatorRector\Fixture;
20+
21+
$value = "hello world";
22+
$result1 = function3($value);
23+
$result2 = function2($result1);
24+
$result = function1($result2);
25+
26+
?>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\DowngradePhp85\Rector\StmtsAwareInterface\DowngradePipeOperatorRector;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->rule(DowngradePipeOperatorRector::class);
10+
};
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\DowngradePhp85\Rector\StmtsAwareInterface;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Expr\BinaryOp\Pipe;
9+
use PhpParser\Node\Expr\FuncCall;
10+
use PhpParser\Node\Expr\Variable;
11+
use PhpParser\Node\Expr\Closure;
12+
use PhpParser\Node\Expr\ArrowFunction;
13+
use PhpParser\Node\Stmt\Expression;
14+
use PhpParser\Node\Stmt\Return_;
15+
use PhpParser\NodeTraverser;
16+
use Rector\Rector\AbstractRector;
17+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
18+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
19+
20+
/**
21+
* @see https://wiki.php.net/rfc/pipe-operator-v3
22+
* @see \Rector\Tests\DowngradePhp85\Rector\StmtsAwareInterface\DowngradePipeOperatorRector\DowngradePipeOperatorRectorTest
23+
*/
24+
final class DowngradePipeOperatorRector extends AbstractRector
25+
{
26+
/**
27+
* @var int
28+
*/
29+
private $tempVariableCounter = 0;
30+
31+
public function getRuleDefinition(): RuleDefinition
32+
{
33+
return new RuleDefinition(
34+
'Downgrade pipe operator |> to PHP < 8.1 compatible code',
35+
[
36+
new CodeSample(
37+
<<<'CODE_SAMPLE'
38+
$value = "hello world";
39+
$result = $value
40+
|> function3(...)
41+
|> function2(...)
42+
|> function1(...);
43+
CODE_SAMPLE
44+
,
45+
<<<'CODE_SAMPLE'
46+
$value = "hello world";
47+
$result1 = function3($value);
48+
$result2 = function2($result1);
49+
$result = function1($result2);
50+
CODE_SAMPLE
51+
),
52+
new CodeSample(
53+
<<<'CODE_SAMPLE'
54+
$result = strtoupper("Hello World") |> trim(...);
55+
CODE_SAMPLE
56+
,
57+
<<<'CODE_SAMPLE'
58+
$result = trim(strtoupper("Hello World"));
59+
CODE_SAMPLE
60+
),
61+
]
62+
);
63+
}
64+
65+
public function getNodeTypes(): array
66+
{
67+
return [Pipe::class];
68+
}
69+
70+
public function refactor(Node $node): ?Node
71+
{
72+
if (!$node instanceof Pipe) {
73+
return null;
74+
}
75+
76+
return $this->processPipeOperation($node);
77+
}
78+
79+
private function processPipeOperation(Pipe $pipe): void
80+
{
81+
82+
}
83+
84+
85+
}

0 commit comments

Comments
 (0)