|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Parser; |
| 4 | + |
| 5 | +use Override; |
| 6 | +use PhpParser\Node; |
| 7 | +use PhpParser\NodeVisitorAbstract; |
| 8 | +use function count; |
| 9 | + |
| 10 | +final class ReversePipeTransformerVisitor extends NodeVisitorAbstract |
| 11 | +{ |
| 12 | + |
| 13 | + #[Override] |
| 14 | + public function enterNode(Node $node): ?Node |
| 15 | + { |
| 16 | + if ($node instanceof Node\Expr\FuncCall && !$node->isFirstClassCallable()) { |
| 17 | + $attributes = $node->getAttributes(); |
| 18 | + $origPipeAttributes = $attributes[PipeTransformerVisitor::ORIGINAL_PIPE_ATTRIBUTE_NAME] ?? []; |
| 19 | + if ($origPipeAttributes !== [] && count($node->getArgs()) === 1) { |
| 20 | + unset($attributes[PipeTransformerVisitor::ORIGINAL_PIPE_ATTRIBUTE_NAME]); |
| 21 | + return new Node\Expr\BinaryOp\Pipe( |
| 22 | + $node->getArgs()[0]->value, |
| 23 | + new Node\Expr\FuncCall($node->name, [new Node\VariadicPlaceholder()], attributes: $attributes), |
| 24 | + attributes: $origPipeAttributes, |
| 25 | + ); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + if ($node instanceof Node\Expr\MethodCall && !$node->isFirstClassCallable()) { |
| 30 | + $attributes = $node->getAttributes(); |
| 31 | + $origPipeAttributes = $attributes[PipeTransformerVisitor::ORIGINAL_PIPE_ATTRIBUTE_NAME] ?? []; |
| 32 | + if ($origPipeAttributes !== [] && count($node->getArgs()) === 1) { |
| 33 | + unset($attributes[PipeTransformerVisitor::ORIGINAL_PIPE_ATTRIBUTE_NAME]); |
| 34 | + return new Node\Expr\BinaryOp\Pipe( |
| 35 | + $node->getArgs()[0]->value, |
| 36 | + new Node\Expr\MethodCall($node->var, $node->name, [new Node\VariadicPlaceholder()], attributes: $attributes), |
| 37 | + attributes: $origPipeAttributes, |
| 38 | + ); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + if ($node instanceof Node\Expr\StaticCall && !$node->isFirstClassCallable()) { |
| 43 | + $attributes = $node->getAttributes(); |
| 44 | + $origPipeAttributes = $attributes[PipeTransformerVisitor::ORIGINAL_PIPE_ATTRIBUTE_NAME] ?? []; |
| 45 | + if ($origPipeAttributes !== [] && count($node->getArgs()) === 1) { |
| 46 | + unset($attributes[PipeTransformerVisitor::ORIGINAL_PIPE_ATTRIBUTE_NAME]); |
| 47 | + return new Node\Expr\BinaryOp\Pipe( |
| 48 | + $node->getArgs()[0]->value, |
| 49 | + new Node\Expr\StaticCall($node->class, $node->name, [new Node\VariadicPlaceholder()], attributes: $attributes), |
| 50 | + attributes: $origPipeAttributes, |
| 51 | + ); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + return null; |
| 56 | + } |
| 57 | + |
| 58 | +} |
0 commit comments