add c alias for create command, fix html input for code samples

This commit is contained in:
TomasVotruba 2020-01-20 00:09:52 +01:00
parent f3ec287a52
commit 08567c2f6a
6 changed files with 101 additions and 2 deletions

View File

@ -11,3 +11,4 @@ services:
# .tpl templates to twig
Rector\CakePHPToSymfony\Rector\Echo_\CakePHPTemplateLinkToTwigRector: null
Rector\CakePHPToSymfony\Rector\Echo_\CakePHPTemplateTranslateToTwigRector: null
Rector\CakePHPToSymfony\Rector\Echo_\CakePHPTemplateHToTwigRector: null

View File

@ -0,0 +1,45 @@
<?php
declare(strict_types=1);
namespace Rector\CakePHPToSymfony\Rector\Echo_;
use PhpParser\Node;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
/**
* @see \Rector\CakePHPToSymfony\Tests\Rector\Echo_\CakePHPTemplateHToTwigRector\CakePHPTemplateHToTwigRectorTest
*/
final class CakePHPTemplateHToTwigRector extends AbstractRector
{
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>'
)
]);
}
/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [\PhpParser\Node\Stmt\Echo_::class];
}
/**
* @param \PhpParser\Node\Stmt\Echo_ $node
*/
public function refactor(Node $node): ?Node
{
// change the node
return $node;
}
}

View File

@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace Rector\CakePHPToSymfony\Tests\Rector\Echo_\CakePHPTemplateHToTwigRector;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
final class CakePHPTemplateHToTwigRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideDataForTest()
*/
public function test(string $file): void
{
$this->doTestFile($file);
}
public function provideDataForTest(): \Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}
protected function getRectorClass(): string
{
return \Rector\CakePHPToSymfony\Rector\Echo_\CakePHPTemplateHToTwigRector::class;
}
}

View File

@ -0,0 +1,15 @@
<?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

@ -96,6 +96,7 @@ final class CreateRectorCommand extends Command
protected function configure(): void
{
$this->setName(CommandNaming::classToName(self::class));
$this->setAliases(['c']);
$this->setDescription('[Dev] Create a new Rector, in a proper location, with new tests');
}

View File

@ -48,8 +48,8 @@ final class ConfigurationFactory
$category,
$this->resolveFullyQualifiedNodeTypes($rectorRecipe['node_types']),
$rectorRecipe['description'],
trim(ltrim($rectorRecipe['code_before'], '<?php')),
trim(ltrim($rectorRecipe['code_after'], '<?php')),
$this->normalizeCode($rectorRecipe['code_before']),
$this->normalizeCode($rectorRecipe['code_after']),
array_filter((array) $rectorRecipe['source']),
$this->resolveSetConfig($rectorRecipe['set'])
);
@ -134,4 +134,13 @@ final class ConfigurationFactory
// in case of forgotten _
return Strings::endsWith($nodeClass, '\\' . $nodeType . '_');
}
private function normalizeCode(string $code): string
{
if (Strings::startsWith($code, '<?php')) {
$code = ltrim($code, '<?php');
}
return trim($code);
}
}