Skip to content

Commit

Permalink
[DowngradePhp73] Handle heredoc new line stripped on multi rules (#257)
Browse files Browse the repository at this point in the history
* [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]>
  • Loading branch information
samsonasik and actions-user authored Dec 12, 2024
1 parent 0ed4563 commit 3aebc81
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ class Fixture
{
public function run()
{
compact('posts', 'units');
$this->setData('posts', 'units');
compact('posts','units');
$this->setData('posts','units');

self::run('posts', 'units');
self::run('posts','units');

self::run('posts', 'units');
self::run('posts',
'units');
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Rector\Tests\DowngradePhp73\Rector\FuncCall\DowngradeTrailingCommasInFunctionCallsRector\Fixture;

// @see https://3v4l.org/POahP#v7.2.17
class MultiSpace
{
public function run()
{
self::run('posts','units' , );
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp73\Rector\FuncCall\DowngradeTrailingCommasInFunctionCallsRector\Fixture;

// @see https://3v4l.org/POahP#v7.2.17
class MultiSpace
{
public function run()
{
self::run('posts','units' );
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ class Foo
{
public function doFoo()
{
new self('foo', 'bar');
new self(
'foo',
'bar'
);
}
}
?>
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\StaticCall;
use Rector\DowngradePhp73\Tokenizer\FollowedByCommaAnalyzer;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand Down Expand Up @@ -90,8 +89,17 @@ public function refactor(Node $node): ?Node
return null;
}

// remove comma
$node->setAttribute(AttributeKey::ORIGINAL_NODE, null);
$tokens = $this->file->getOldTokens();
$iteration = 1;

while (isset($tokens[$args[$lastArgKey]->getEndTokenPos() + $iteration])) {
if (trim($tokens[$args[$lastArgKey]->getEndTokenPos() + $iteration]->text) === ',') {
$tokens[$args[$lastArgKey]->getEndTokenPos() + $iteration]->text = '';
break;
}

++$iteration;
}

return $node;
}
Expand Down
28 changes: 28 additions & 0 deletions tests/Issues/DowngradeHeredoc/DowngradeHeredocTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Issues\DowngradeHeredoc;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class DowngradeHeredocTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
115 changes: 115 additions & 0 deletions tests/Issues/DowngradeHeredoc/Fixture/fixture.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

namespace Rector\Tests\Issues\DowngradeNullJson\DowngradeHeredoc;

class Fixture
{
public function getDefinition(): FixerDefinitionInterface
{
return new FixerDefinition(
'Sorts attributes using the configured sort algorithm.',
[
new VersionSpecificCodeSample(
<<<'EOL'
<?php
#[Foo]
#[Bar(3)]
#[Qux(new Bar(5))]
#[Corge(a: 'test')]
class Sample1 {}
#[
Foo,
Bar(3),
Qux(new Bar(5)),
Corge(a: 'test'),
]
class Sample2 {}

EOL,
new VersionSpecification(8_00_00),
),
new VersionSpecificCodeSample(
<<<'EOL'
<?php
use A\B\Foo;
use A\B\Bar as BarAlias;
use A\B as AB;
#[Foo]
#[BarAlias(3)]
#[AB\Qux(new Bar(5))]
#[\A\B\Corge(a: 'test')]
class Sample1 {}

EOL,
new VersionSpecification(8_00_00),
['sort_algorithm' => self::ORDER_CUSTOM, 'order' => ['A\B\Qux', 'A\B\Bar', 'A\B\Corge']],
),
],
);
}
}

?>
-----
<?php

namespace Rector\Tests\Issues\DowngradeNullJson\DowngradeHeredoc;

class Fixture
{
public function getDefinition(): FixerDefinitionInterface
{
return new FixerDefinition(
'Sorts attributes using the configured sort algorithm.',
[
new VersionSpecificCodeSample(
<<<'EOL'
<?php
#[Foo]
#[Bar(3)]
#[Qux(new Bar(5))]
#[Corge(a: 'test')]
class Sample1 {}
#[
Foo,
Bar(3),
Qux(new Bar(5)),
Corge(a: 'test'),
]
class Sample2 {}

EOL
,
new VersionSpecification(80000)
),
new VersionSpecificCodeSample(
<<<'EOL'
<?php
use A\B\Foo;
use A\B\Bar as BarAlias;
use A\B as AB;
#[Foo]
#[BarAlias(3)]
#[AB\Qux(new Bar(5))]
#[\A\B\Corge(a: 'test')]
class Sample1 {}

EOL
,
new VersionSpecification(80000),
['sort_algorithm' => self::ORDER_CUSTOM, 'order' => ['A\B\Qux', 'A\B\Bar', 'A\B\Corge']]
),
]
);
}
}

?>
10 changes: 10 additions & 0 deletions tests/Issues/DowngradeHeredoc/config/configured_rule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Set\ValueObject\DowngradeLevelSetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->sets([DowngradeLevelSetList::DOWN_TO_PHP_72]);
};

0 comments on commit 3aebc81

Please sign in to comment.