[CakePHPToSymfony] Add CakePHPTemplateHToTwigRector

This commit is contained in:
TomasVotruba 2020-01-20 00:23:47 +01:00
parent 80c84536ac
commit 89bfb7982e
6 changed files with 57 additions and 35 deletions

View File

@ -1,4 +1,4 @@
# All 440 Rectors Overview
# All 441 Rectors Overview
- [Projects](#projects)
- [General](#general)
@ -422,6 +422,19 @@ Migrate CakePHP 2.4 Controller to Symfony 5
<br>
### `CakePHPTemplateHToTwigRector`
- class: `Rector\CakePHPToSymfony\Rector\Echo_\CakePHPTemplateHToTwigRector`
Migrate CakePHP 2.4 h() function calls to Twig
```diff
-<h3><?php echo h($value); ?></h3>
+<h3>{{ value|escape }}</h3>
```
<br>
### `CakePHPTemplateLinkToTwigRector`
- class: `Rector\CakePHPToSymfony\Rector\Echo_\CakePHPTemplateLinkToTwigRector`

View File

@ -5,23 +5,24 @@ declare(strict_types=1);
namespace Rector\CakePHPToSymfony\Rector\Echo_;
use PhpParser\Node;
use Rector\Rector\AbstractRector;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Echo_;
use PhpParser\Node\Stmt\InlineHTML;
use Rector\CakePHPToSymfony\Rector\AbstractCakePHPRector;
use Rector\Exception\NotImplementedException;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
/**
* @see \Rector\CakePHPToSymfony\Tests\Rector\Echo_\CakePHPTemplateHToTwigRector\CakePHPTemplateHToTwigRectorTest
*/
final class CakePHPTemplateHToTwigRector extends AbstractRector
final class CakePHPTemplateHToTwigRector extends AbstractCakePHPRector
{
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Migrate CakePHP 2.4 h() function calls to Twig', [
new CodeSample(
'3><?php echo h($value); ?></h3>',
'3>{{ value|escape }}</h3>'
)
new CodeSample('<h3><?php echo h($value); ?></h3>', '<h3>{{ value|escape }}</h3>'),
]);
}
@ -30,16 +31,36 @@ final class CakePHPTemplateHToTwigRector extends AbstractRector
*/
public function getNodeTypes(): array
{
return [\PhpParser\Node\Stmt\Echo_::class];
return [Echo_::class];
}
/**
* @param \PhpParser\Node\Stmt\Echo_ $node
* @param Echo_ $node
*/
public function refactor(Node $node): ?Node
{
// change the node
if (! isset($node->exprs[0])) {
return null;
}
return $node;
$singleEchoedExpr = $node->exprs[0];
if (! $singleEchoedExpr instanceof FuncCall) {
return null;
}
if (! $this->isName($singleEchoedExpr, 'h')) {
return null;
}
$funcArg = $singleEchoedExpr->args[0]->value;
if ($funcArg instanceof Variable) {
$templateVariable = $this->getName($funcArg);
} else {
throw new NotImplementedException();
}
$htmlContent = sprintf('{{ %s|escape }}', $templateVariable);
return new InlineHTML($htmlContent);
}
}

View File

@ -4,6 +4,8 @@ declare(strict_types=1);
namespace Rector\CakePHPToSymfony\Tests\Rector\Echo_\CakePHPTemplateHToTwigRector;
use Iterator;
use Rector\CakePHPToSymfony\Rector\Echo_\CakePHPTemplateHToTwigRector;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
final class CakePHPTemplateHToTwigRectorTest extends AbstractRectorTestCase
@ -13,16 +15,16 @@ final class CakePHPTemplateHToTwigRectorTest extends AbstractRectorTestCase
*/
public function test(string $file): void
{
$this->doTestFile($file);
$this->doTestFileWithoutAutoload($file);
}
public function provideDataForTest(): \Iterator
public function provideDataForTest(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}
protected function getRectorClass(): string
{
return \Rector\CakePHPToSymfony\Rector\Echo_\CakePHPTemplateHToTwigRector::class;
return CakePHPTemplateHToTwigRector::class;
}
}

View File

@ -1,15 +0,0 @@
<?php
namespace Rector\CakePHPToSymfony\Tests\Rector\Echo_\CakePHPTemplateHToTwigRector\Fixture;
3><?php echo h($value); ?></h3>
?>
-----
<?php
namespace Rector\CakePHPToSymfony\Tests\Rector\Echo_\CakePHPTemplateHToTwigRector\Fixture;
3>{{ value|escape }}</h3>
?>

View File

@ -0,0 +1,3 @@
<h3><?php echo h($value); ?></h3>
-----
<h3>{{ value|escape }}</h3>

View File

@ -48,12 +48,10 @@ final class TemplateFinder
private function createFixtureSmartFileInfo(bool $isPhpSnippet): SmartFileInfo
{
if ($isPhpSnippet) {
$fixtureFileInfo = new SmartFileInfo(self::TEMPLATES_FIXTURE_DIRECTORY . '/fixture.php.inc');
return new SmartFileInfo(self::TEMPLATES_FIXTURE_DIRECTORY . '/fixture.php.inc');
}
// is html snippet
} else {
$fixtureFileInfo = new SmartFileInfo(self::TEMPLATES_FIXTURE_DIRECTORY . '/html_fixture.php.inc');
}
return $fixtureFileInfo;
return new SmartFileInfo(self::TEMPLATES_FIXTURE_DIRECTORY . '/html_fixture.php.inc');
}
}