Skip to content

Commit

Permalink
Merge branch 'downgrade-php73-downgrade-flexible-heredocs-syntax'
Browse files Browse the repository at this point in the history
  • Loading branch information
Tommy Quissens committed Sep 22, 2020
2 parents a73fc88 + 96765dd commit c637ecc
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 0 deletions.
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@
"Rector\\NetteCodeQuality\\": "rules/nette-code-quality/src",
"Rector\\DowngradePhp71\\": "rules/downgrade-php71/src",
"Rector\\DowngradePhp72\\": "rules/downgrade-php72/src",
"Rector\\DowngradePhp73\\": "rules/downgrade-php73/src",
"Rector\\DowngradePhp74\\": "rules/downgrade-php74/src",
"Rector\\DowngradePhp80\\": "rules/downgrade-php80/src",
"Rector\\SymfonyPhpConfig\\": "rules/symfony-php-config/src"
Expand Down Expand Up @@ -238,6 +239,7 @@
"Rector\\NetteCodeQuality\\Tests\\": "rules/nette-code-quality/tests",
"Rector\\DowngradePhp71\\Tests\\": "rules/downgrade-php71/tests",
"Rector\\DowngradePhp72\\Tests\\": "rules/downgrade-php72/tests",
"Rector\\DowngradePhp73\\Tests\\": "rules/downgrade-php73/tests",
"Rector\\DowngradePhp74\\Tests\\": "rules/downgrade-php74/tests",
"Rector\\DowngradePhp80\\Tests\\": "rules/downgrade-php80/tests",
"Rector\\SymfonyPhpConfig\\Tests\\": "rules/symfony-php-config/tests",
Expand Down
12 changes: 12 additions & 0 deletions config/set/downgrade-php73.php
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);
};
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;
}
}
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;
}
}
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;
}
}

?>

0 comments on commit c637ecc

Please sign in to comment.