Skip to content

Commit fb2cc65

Browse files
committed
Fix applying auto-fixes when the code contains pipe operator
1 parent 167f9a3 commit fb2cc65

File tree

5 files changed

+51
-3
lines changed

5 files changed

+51
-3
lines changed

src/Parser/PipeTransformerVisitor.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ final class PipeTransformerVisitor extends NodeVisitorAbstract
1515

1616
public const ORIGINAL_PIPE_ATTRIBUTE_NAME = 'originalPipeAttrs';
1717

18+
public const ORIGINAL_VARIADIC_PLACEHOLDER_ATTRIBUTE_NAME = 'originalVariadicPlaceholderAttrs';
19+
1820
#[Override]
1921
public function enterNode(Node $node): ?Node
2022
{
@@ -27,6 +29,7 @@ public function enterNode(Node $node): ?Node
2729
new Arg($node->left),
2830
], attributes: array_merge($node->right->getAttributes(), [
2931
self::ORIGINAL_PIPE_ATTRIBUTE_NAME => $node->getAttributes(),
32+
self::ORIGINAL_VARIADIC_PLACEHOLDER_ATTRIBUTE_NAME => $node->right->getRawArgs()[0]->getAttributes(),
3033
]));
3134
}
3235

@@ -35,6 +38,7 @@ public function enterNode(Node $node): ?Node
3538
new Arg($node->left),
3639
], attributes: array_merge($node->right->getAttributes(), [
3740
self::ORIGINAL_PIPE_ATTRIBUTE_NAME => $node->getAttributes(),
41+
self::ORIGINAL_VARIADIC_PLACEHOLDER_ATTRIBUTE_NAME => $node->right->getRawArgs()[0]->getAttributes(),
3842
]));
3943
}
4044

@@ -43,6 +47,7 @@ public function enterNode(Node $node): ?Node
4347
new Arg($node->left),
4448
], attributes: array_merge($node->right->getAttributes(), [
4549
self::ORIGINAL_PIPE_ATTRIBUTE_NAME => $node->getAttributes(),
50+
self::ORIGINAL_VARIADIC_PLACEHOLDER_ATTRIBUTE_NAME => $node->right->getRawArgs()[0]->getAttributes(),
4651
]));
4752
}
4853

src/Parser/ReversePipeTransformerVisitor.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@ public function enterNode(Node $node): ?Node
1818
if ($node instanceof Node\Expr\FuncCall && !$node->isFirstClassCallable()) {
1919
$attributes = $node->getAttributes();
2020
$origPipeAttributes = $attributes[PipeTransformerVisitor::ORIGINAL_PIPE_ATTRIBUTE_NAME] ?? [];
21+
$origVariadicPlaceholderAttributes = $attributes[PipeTransformerVisitor::ORIGINAL_VARIADIC_PLACEHOLDER_ATTRIBUTE_NAME] ?? [];
2122
if ($origPipeAttributes !== [] && count($node->getArgs()) === 1) {
2223
$origPipeAttributes[self::ARG_ATTRIBUTES_NAME] = $node->getArgs()[0]->getAttributes();
2324
if ($node->name instanceof Node\Name) {
2425
unset($attributes[PipeTransformerVisitor::ORIGINAL_PIPE_ATTRIBUTE_NAME]);
26+
unset($attributes[PipeTransformerVisitor::ORIGINAL_VARIADIC_PLACEHOLDER_ATTRIBUTE_NAME]);
2527
return new Node\Expr\BinaryOp\Pipe(
2628
$node->getArgs()[0]->value,
27-
new Node\Expr\FuncCall($node->name, [new Node\VariadicPlaceholder()], attributes: $attributes),
29+
new Node\Expr\FuncCall($node->name, [new Node\VariadicPlaceholder($origVariadicPlaceholderAttributes)], attributes: $attributes),
2830
attributes: $origPipeAttributes,
2931
);
3032
}
@@ -40,12 +42,14 @@ public function enterNode(Node $node): ?Node
4042
if ($node instanceof Node\Expr\MethodCall && !$node->isFirstClassCallable()) {
4143
$attributes = $node->getAttributes();
4244
$origPipeAttributes = $attributes[PipeTransformerVisitor::ORIGINAL_PIPE_ATTRIBUTE_NAME] ?? [];
45+
$origVariadicPlaceholderAttributes = $attributes[PipeTransformerVisitor::ORIGINAL_VARIADIC_PLACEHOLDER_ATTRIBUTE_NAME] ?? [];
4346
if ($origPipeAttributes !== [] && count($node->getArgs()) === 1) {
4447
$origPipeAttributes[self::ARG_ATTRIBUTES_NAME] = $node->getArgs()[0]->getAttributes();
4548
unset($attributes[PipeTransformerVisitor::ORIGINAL_PIPE_ATTRIBUTE_NAME]);
49+
unset($attributes[PipeTransformerVisitor::ORIGINAL_VARIADIC_PLACEHOLDER_ATTRIBUTE_NAME]);
4650
return new Node\Expr\BinaryOp\Pipe(
4751
$node->getArgs()[0]->value,
48-
new Node\Expr\MethodCall($node->var, $node->name, [new Node\VariadicPlaceholder()], attributes: $attributes),
52+
new Node\Expr\MethodCall($node->var, $node->name, [new Node\VariadicPlaceholder($origVariadicPlaceholderAttributes)], attributes: $attributes),
4953
attributes: $origPipeAttributes,
5054
);
5155
}
@@ -54,12 +58,14 @@ public function enterNode(Node $node): ?Node
5458
if ($node instanceof Node\Expr\StaticCall && !$node->isFirstClassCallable()) {
5559
$attributes = $node->getAttributes();
5660
$origPipeAttributes = $attributes[PipeTransformerVisitor::ORIGINAL_PIPE_ATTRIBUTE_NAME] ?? [];
61+
$origVariadicPlaceholderAttributes = $attributes[PipeTransformerVisitor::ORIGINAL_VARIADIC_PLACEHOLDER_ATTRIBUTE_NAME] ?? [];
5762
if ($origPipeAttributes !== [] && count($node->getArgs()) === 1) {
5863
$origPipeAttributes[self::ARG_ATTRIBUTES_NAME] = $node->getArgs()[0]->getAttributes();
5964
unset($attributes[PipeTransformerVisitor::ORIGINAL_PIPE_ATTRIBUTE_NAME]);
65+
unset($attributes[PipeTransformerVisitor::ORIGINAL_VARIADIC_PLACEHOLDER_ATTRIBUTE_NAME]);
6066
return new Node\Expr\BinaryOp\Pipe(
6167
$node->getArgs()[0]->value,
62-
new Node\Expr\StaticCall($node->class, $node->name, [new Node\VariadicPlaceholder()], attributes: $attributes),
68+
new Node\Expr\StaticCall($node->class, $node->name, [new Node\VariadicPlaceholder($origVariadicPlaceholderAttributes)], attributes: $attributes),
6369
attributes: $origPipeAttributes,
6470
);
6571
}

tests/PHPStan/Build/FinalClassRuleTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PHPStan\File\FileHelper;
66
use PHPStan\Rules\Rule;
77
use PHPStan\Testing\RuleTestCase;
8+
use PHPUnit\Framework\Attributes\RequiresPhp;
89

910
/**
1011
* @extends RuleTestCase<FinalClassRule>
@@ -32,4 +33,10 @@ public function testFix(): void
3233
$this->fix(__DIR__ . '/data/final-class-rule.php', __DIR__ . '/data/final-class-rule.php.fixed');
3334
}
3435

36+
#[RequiresPhp('>= 8.5')]
37+
public function testFixWithPipe(): void
38+
{
39+
$this->fix(__DIR__ . '/data/final-class-rule-pipe.php', __DIR__ . '/data/final-class-rule-pipe.php.fixed');
40+
}
41+
3542
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php // lint >= 8.5
2+
3+
namespace FinalClassRulePipe;
4+
5+
class Foo
6+
{
7+
8+
public function doFoo(): void
9+
{
10+
$one = '1'
11+
|> strtoupper(...)
12+
|> strtolower(...);
13+
}
14+
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php // lint >= 8.5
2+
3+
namespace FinalClassRulePipe;
4+
5+
final class Foo
6+
{
7+
8+
public function doFoo(): void
9+
{
10+
$one = '1'
11+
|> strtoupper(...)
12+
|> strtolower(...);
13+
}
14+
15+
}

0 commit comments

Comments
 (0)