-
-
Notifications
You must be signed in to change notification settings - Fork 336
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Core] [Printer] Ensure empty php tag init on original node not re-p…
…rinted (#3284) * [e2e] Ensure empty php tag init on original node not re-printed * [e2e] Ensure empty php tag init on original node not re-printed * re-print HTML next node is not needed, only prev is needed * add test for next stmt is html * add before HTML * add test for before node * Revert re-print HTML next node is not needed, only prev is needed This reverts commit 5d753e3. * failing fixture add node before node * Fixed 🎉 * Fix * [ci-review] Rector Rectify * [ci-review] Rector Rectify Co-authored-by: GitHub Action <action@github.com>
- Loading branch information
1 parent
1868b93
commit 410e939
Showing
11 changed files
with
189 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php | ||
|
||
?> | ||
<div></div> | ||
<?php |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
tests/Issues/AddNodeAfterNodeStmt/Fixture/before_html.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<div>some text</div> | ||
<?php | ||
if ($a === 1) { | ||
} | ||
?> | ||
----- | ||
<div>some text</div> | ||
<?php | ||
if ($a === 1) { | ||
} | ||
echo 'this is new stmt after if'; | ||
?> |
12 changes: 12 additions & 0 deletions
12
tests/Issues/AddNodeAfterNodeStmt/Fixture/next_html.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
if ($a === 1) { | ||
} | ||
?> | ||
<div>some text</div> | ||
----- | ||
<?php | ||
if ($a === 1) { | ||
} | ||
echo 'this is new stmt after if'; | ||
?> | ||
<div>some text</div> |
32 changes: 32 additions & 0 deletions
32
tests/Issues/AddNodeBeforeNodeStmt/AddNodeBeforeNodeStmtTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Core\Tests\Issues\AddNodeBeforeNodeStmt; | ||
|
||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class AddNodeBeforeNodeStmtTest extends AbstractRectorTestCase | ||
{ | ||
/** | ||
* @dataProvider provideData() | ||
*/ | ||
public function test(string $filePath): void | ||
{ | ||
$this->doTestFile($filePath); | ||
} | ||
|
||
/** | ||
* @return Iterator<array<string>> | ||
*/ | ||
public function provideData(): Iterator | ||
{ | ||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
tests/Issues/AddNodeBeforeNodeStmt/Fixture/before_html.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<div>some text</div> | ||
<?php | ||
if ($a === 1) { | ||
} | ||
?> | ||
----- | ||
<div>some text</div> | ||
<?php | ||
echo 'this is new stmt before if'; | ||
if ($a === 1) { | ||
} |
32 changes: 32 additions & 0 deletions
32
tests/Issues/AddNodeBeforeNodeStmt/Fixture/fixture.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Rector\Core\Tests\Issues\AddNodeAfterNodeStmt\Fixture; | ||
|
||
final class Fixture | ||
{ | ||
public function run() | ||
{ | ||
if ($a === 1) { | ||
} | ||
echo 'existing next stmt after if'; | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Core\Tests\Issues\AddNodeAfterNodeStmt\Fixture; | ||
|
||
final class Fixture | ||
{ | ||
public function run() | ||
{ | ||
echo 'this is new stmt before if'; | ||
if ($a === 1) { | ||
} | ||
echo 'existing next stmt after if'; | ||
} | ||
} | ||
|
||
?> |
12 changes: 12 additions & 0 deletions
12
tests/Issues/AddNodeBeforeNodeStmt/Fixture/next_html.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
if ($a === 1) { | ||
} | ||
?> | ||
<div>some text next</div> | ||
----- | ||
<?php | ||
echo 'this is new stmt before if'; | ||
if ($a === 1) { | ||
} | ||
?> | ||
<div>some text next</div> |
41 changes: 41 additions & 0 deletions
41
tests/Issues/AddNodeBeforeNodeStmt/Source/AddBeforeStmtRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Core\Tests\Issues\AddNodeBeforeNodeStmt\Source; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Scalar\String_; | ||
use PhpParser\Node\Stmt\Echo_; | ||
use PhpParser\Node\Stmt\If_; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Rector\PostRector\Collector\NodesToAddCollector; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
class AddBeforeStmtRector extends AbstractRector | ||
{ | ||
public function __construct(private readonly NodesToAddCollector $nodesToAddCollector) | ||
{ | ||
} | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('uff', []); | ||
} | ||
|
||
public function getNodeTypes(): array | ||
{ | ||
return [ | ||
If_::class, | ||
]; | ||
} | ||
|
||
public function refactor(Node $node) | ||
{ | ||
$this->nodesToAddCollector->addNodeBeforeNode( | ||
new Echo_([new String_("this is new stmt before if")]), | ||
$node | ||
); | ||
return $node; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
tests/Issues/AddNodeBeforeNodeStmt/config/configured_rule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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\Core\Tests\Issues\AddNodeBeforeNodeStmt\Source\AddBeforeStmtRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(AddBeforeStmtRector::class); | ||
}; |