Skip to content

Commit 3aebc81

Browse files
[DowngradePhp73] Handle heredoc new line stripped on multi rules (#257)
* [DowngradePhp73] Handle heredoc new line stripped on multi rules * fix * [ci-review] Rector Rectify * add fixture multi space * fix --------- Co-authored-by: GitHub Action <[email protected]>
1 parent 0ed4563 commit 3aebc81

File tree

7 files changed

+202
-8
lines changed

7 files changed

+202
-8
lines changed

rules-tests/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector/Fixture/fixture.php.inc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@ class Fixture
2626
{
2727
public function run()
2828
{
29-
compact('posts', 'units');
30-
$this->setData('posts', 'units');
29+
compact('posts','units');
30+
$this->setData('posts','units');
3131

32-
self::run('posts', 'units');
32+
self::run('posts','units');
3333

34-
self::run('posts', 'units');
34+
self::run('posts',
35+
'units');
3536
}
3637
}
3738

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Rector\Tests\DowngradePhp73\Rector\FuncCall\DowngradeTrailingCommasInFunctionCallsRector\Fixture;
4+
5+
// @see https://3v4l.org/POahP#v7.2.17
6+
class MultiSpace
7+
{
8+
public function run()
9+
{
10+
self::run('posts','units' , );
11+
}
12+
}
13+
14+
?>
15+
-----
16+
<?php
17+
18+
namespace Rector\Tests\DowngradePhp73\Rector\FuncCall\DowngradeTrailingCommasInFunctionCallsRector\Fixture;
19+
20+
// @see https://3v4l.org/POahP#v7.2.17
21+
class MultiSpace
22+
{
23+
public function run()
24+
{
25+
self::run('posts','units' );
26+
}
27+
}
28+
29+
?>

rules-tests/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector/Fixture/new.php.inc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ class Foo
2222
{
2323
public function doFoo()
2424
{
25-
new self('foo', 'bar');
25+
new self(
26+
'foo',
27+
'bar'
28+
);
2629
}
2730
}
2831
?>

rules/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use PhpParser\Node\Expr\New_;
1111
use PhpParser\Node\Expr\StaticCall;
1212
use Rector\DowngradePhp73\Tokenizer\FollowedByCommaAnalyzer;
13-
use Rector\NodeTypeResolver\Node\AttributeKey;
1413
use Rector\Rector\AbstractRector;
1514
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
1615
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@@ -90,8 +89,17 @@ public function refactor(Node $node): ?Node
9089
return null;
9190
}
9291

93-
// remove comma
94-
$node->setAttribute(AttributeKey::ORIGINAL_NODE, null);
92+
$tokens = $this->file->getOldTokens();
93+
$iteration = 1;
94+
95+
while (isset($tokens[$args[$lastArgKey]->getEndTokenPos() + $iteration])) {
96+
if (trim($tokens[$args[$lastArgKey]->getEndTokenPos() + $iteration]->text) === ',') {
97+
$tokens[$args[$lastArgKey]->getEndTokenPos() + $iteration]->text = '';
98+
break;
99+
}
100+
101+
++$iteration;
102+
}
95103

96104
return $node;
97105
}
Lines changed: 28 additions & 0 deletions
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\Issues\DowngradeHeredoc;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class DowngradeHeredocTest 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: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
3+
namespace Rector\Tests\Issues\DowngradeNullJson\DowngradeHeredoc;
4+
5+
class Fixture
6+
{
7+
public function getDefinition(): FixerDefinitionInterface
8+
{
9+
return new FixerDefinition(
10+
'Sorts attributes using the configured sort algorithm.',
11+
[
12+
new VersionSpecificCodeSample(
13+
<<<'EOL'
14+
<?php
15+
16+
#[Foo]
17+
#[Bar(3)]
18+
#[Qux(new Bar(5))]
19+
#[Corge(a: 'test')]
20+
class Sample1 {}
21+
22+
#[
23+
Foo,
24+
Bar(3),
25+
Qux(new Bar(5)),
26+
Corge(a: 'test'),
27+
]
28+
class Sample2 {}
29+
30+
EOL,
31+
new VersionSpecification(8_00_00),
32+
),
33+
new VersionSpecificCodeSample(
34+
<<<'EOL'
35+
<?php
36+
37+
use A\B\Foo;
38+
use A\B\Bar as BarAlias;
39+
use A\B as AB;
40+
41+
#[Foo]
42+
#[BarAlias(3)]
43+
#[AB\Qux(new Bar(5))]
44+
#[\A\B\Corge(a: 'test')]
45+
class Sample1 {}
46+
47+
EOL,
48+
new VersionSpecification(8_00_00),
49+
['sort_algorithm' => self::ORDER_CUSTOM, 'order' => ['A\B\Qux', 'A\B\Bar', 'A\B\Corge']],
50+
),
51+
],
52+
);
53+
}
54+
}
55+
56+
?>
57+
-----
58+
<?php
59+
60+
namespace Rector\Tests\Issues\DowngradeNullJson\DowngradeHeredoc;
61+
62+
class Fixture
63+
{
64+
public function getDefinition(): FixerDefinitionInterface
65+
{
66+
return new FixerDefinition(
67+
'Sorts attributes using the configured sort algorithm.',
68+
[
69+
new VersionSpecificCodeSample(
70+
<<<'EOL'
71+
<?php
72+
73+
#[Foo]
74+
#[Bar(3)]
75+
#[Qux(new Bar(5))]
76+
#[Corge(a: 'test')]
77+
class Sample1 {}
78+
79+
#[
80+
Foo,
81+
Bar(3),
82+
Qux(new Bar(5)),
83+
Corge(a: 'test'),
84+
]
85+
class Sample2 {}
86+
87+
EOL
88+
,
89+
new VersionSpecification(80000)
90+
),
91+
new VersionSpecificCodeSample(
92+
<<<'EOL'
93+
<?php
94+
95+
use A\B\Foo;
96+
use A\B\Bar as BarAlias;
97+
use A\B as AB;
98+
99+
#[Foo]
100+
#[BarAlias(3)]
101+
#[AB\Qux(new Bar(5))]
102+
#[\A\B\Corge(a: 'test')]
103+
class Sample1 {}
104+
105+
EOL
106+
,
107+
new VersionSpecification(80000),
108+
['sort_algorithm' => self::ORDER_CUSTOM, 'order' => ['A\B\Qux', 'A\B\Bar', 'A\B\Corge']]
109+
),
110+
]
111+
);
112+
}
113+
}
114+
115+
?>
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\Set\ValueObject\DowngradeLevelSetList;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->sets([DowngradeLevelSetList::DOWN_TO_PHP_72]);
10+
};

0 commit comments

Comments
 (0)