-
-
Notifications
You must be signed in to change notification settings - Fork 671
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'downgrade-php73-downgrade-flexible-heredocs-syntax'
- Loading branch information
Showing
5 changed files
with
150 additions
and
0 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
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 | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\DowngradePhp73\Rector\FuncCall\DowngradeTrailingCommasInFunctionCalls; | ||
use Rector\DowngradePhp73\Rector\String_\DowngradeFlexibleHeredocSyntax; | ||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; | ||
|
||
return static function (ContainerConfigurator $containerConfigurator): void { | ||
$services = $containerConfigurator->services(); | ||
$services->set(DowngradeFlexibleHeredocSyntax::class); | ||
}; |
70 changes: 70 additions & 0 deletions
70
rules/downgrade-php73/src/Rector/String_/DowngradeFlexibleHeredocSyntax.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,70 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\DowngradePhp73\Rector\String_; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Scalar\String_; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Rector\Core\RectorDefinition\CodeSample; | ||
use Rector\Core\RectorDefinition\RectorDefinition; | ||
use Rector\NodeTypeResolver\Node\AttributeKey; | ||
|
||
/** | ||
* @see https://wiki.php.net/rfc/flexible_heredoc_nowdoc_syntaxes | ||
* @see \Rector\Php73\Tests\Rector\String_\SensitiveHereNowDocRector\SensitiveHereNowDocRectorTest | ||
*/ | ||
final class DowngradeFlexibleHeredocSyntax extends AbstractRector | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private const WRAP_SUFFIX = '_WRAP'; | ||
|
||
public function getDefinition(): RectorDefinition | ||
{ | ||
return new RectorDefinition('Changes heredoc/nowdoc that contains closing word to safe wrapper name', [ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
$query = <<<SQL | ||
SELECT * | ||
FROM `table` | ||
WHERE `column` = true; | ||
SQL; | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
$query = <<<SQL | ||
SELECT * | ||
FROM `table` | ||
WHERE `column` = true; | ||
SQL; | ||
CODE_SAMPLE | ||
), | ||
]); | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [String_::class]; | ||
} | ||
|
||
/** | ||
* @param String_ $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
if (! in_array($node->getAttribute(AttributeKey::KIND), [String_::KIND_HEREDOC, String_::KIND_NOWDOC], true)) { | ||
return null; | ||
} | ||
|
||
$node->setAttribute('docIndentation', ''); | ||
$node->setAttribute(AttributeKey::ORIGINAL_NODE, null); | ||
|
||
return $node; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...ests/Rector/String_/DowngradeFlexibleHeredocSyntax/DowngradeFlexibleHeredocSyntaxTest.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,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Php73\Tests\Rector\String_\SensitiveHereNowDocRector; | ||
|
||
use Iterator; | ||
use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase; | ||
use Rector\DowngradePhp73\Rector\String_\DowngradeFlexibleHeredocSyntax; | ||
use Symplify\SmartFileSystem\SmartFileInfo; | ||
|
||
final class DowngradeFlexibleHeredocSyntaxTest extends AbstractRectorTestCase | ||
{ | ||
/** | ||
* @dataProvider provideData() | ||
*/ | ||
public function test(SmartFileInfo $fileInfo): void | ||
{ | ||
$this->doTestFileInfo($fileInfo); | ||
} | ||
|
||
public function provideData(): Iterator | ||
{ | ||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
protected function getRectorClass(): string | ||
{ | ||
return DowngradeFlexibleHeredocSyntax::class; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...wngrade-php73/tests/Rector/String_/DowngradeFlexibleHeredocSyntax/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,35 @@ | ||
<?php | ||
|
||
namespace Rector\DowngradePhp73\Tests\Rector\FuncCall\DowngradeFlexibleHeredocSyntax\Fixture; | ||
|
||
class FixtureClass | ||
{ | ||
public function run() | ||
{ | ||
$query = <<<SQL | ||
SELECT * | ||
FROM `table` | ||
WHERE `column` = true; | ||
SQL; | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\DowngradePhp73\Tests\Rector\FuncCall\DowngradeFlexibleHeredocSyntax\Fixture; | ||
|
||
class FixtureClass | ||
{ | ||
public function run() | ||
{ | ||
$query = <<<SQL | ||
SELECT * | ||
FROM `table` | ||
WHERE `column` = true; | ||
SQL; | ||
} | ||
} | ||
|
||
?> |