mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-19 14:27:14 +01:00
[Symfony] Add ChangeXmlToYamlFileLoaderInExtensionRector
This commit is contained in:
parent
395ced5f57
commit
156c31ee6d
@ -1,4 +1,4 @@
|
||||
# All 515 Rectors Overview
|
||||
# All 518 Rectors Overview
|
||||
|
||||
- [Projects](#projects)
|
||||
- [General](#general)
|
||||
@ -13,6 +13,7 @@
|
||||
- [CodeQuality](#codequality) (54)
|
||||
- [CodingStyle](#codingstyle) (34)
|
||||
- [DeadCode](#deadcode) (40)
|
||||
- [Decomplex](#decomplex) (1)
|
||||
- [Decouple](#decouple) (1)
|
||||
- [Doctrine](#doctrine) (16)
|
||||
- [DoctrineCodeQuality](#doctrinecodequality) (2)
|
||||
@ -24,6 +25,7 @@
|
||||
- [Laravel](#laravel) (6)
|
||||
- [Legacy](#legacy) (2)
|
||||
- [MagicDisclosure](#magicdisclosure) (5)
|
||||
- [MockeryToProphecy](#mockerytoprophecy) (1)
|
||||
- [MockistaToMockery](#mockistatomockery) (2)
|
||||
- [MysqlToMysqli](#mysqltomysqli) (4)
|
||||
- [Naming](#naming) (2)
|
||||
@ -62,7 +64,7 @@
|
||||
- [SOLID](#solid) (12)
|
||||
- [Sensio](#sensio) (3)
|
||||
- [StrictCodeQuality](#strictcodequality) (1)
|
||||
- [Symfony](#symfony) (29)
|
||||
- [Symfony](#symfony) (30)
|
||||
- [SymfonyCodeQuality](#symfonycodequality) (1)
|
||||
- [SymfonyPHPUnit](#symfonyphpunit) (1)
|
||||
- [Twig](#twig) (1)
|
||||
@ -3240,6 +3242,31 @@ Change ternary of bool : false to && bool
|
||||
|
||||
<br><br>
|
||||
|
||||
## Decomplex
|
||||
|
||||
### `UseMessageVariableForSprintfInSymfonyStyleRector`
|
||||
|
||||
- class: [`Rector\Decomplex\Rector\MethodCall\UseMessageVariableForSprintfInSymfonyStyleRector`](/../master/rules/decomplex/src/Rector/MethodCall/UseMessageVariableForSprintfInSymfonyStyleRector.php)
|
||||
- [test fixtures](/../master/rules/decomplex/tests/Rector/MethodCall/UseMessageVariableForSprintfInSymfonyStyleRector/Fixture)
|
||||
|
||||
Decouple $message property from `sprintf()` calls in `$this->smyfonyStyle->method()`
|
||||
|
||||
```diff
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
|
||||
final class SomeClass
|
||||
{
|
||||
public function run(SymfonyStyle $symfonyStyle)
|
||||
{
|
||||
- $symfonyStyle->info(sprintf('Hi %s', 'Tom'));
|
||||
+ $message = sprintf('Hi %s', 'Tom');
|
||||
+ $symfonyStyle->info($message);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<br><br>
|
||||
|
||||
## Decouple
|
||||
|
||||
### `DecoupleClassMethodToOwnClassRector`
|
||||
@ -4689,6 +4716,25 @@ services:
|
||||
|
||||
<br><br>
|
||||
|
||||
## MockeryToProphecy
|
||||
|
||||
### `MockeryCreateMockToProphizeRector`
|
||||
|
||||
- class: [`Rector\MockeryToProphecy\Rector\MethodCall\MockeryCreateMockToProphizeRector`](/../master/rules/mockery-to-prophecy/src/Rector/MethodCall/MockeryCreateMockToProphizeRector.php)
|
||||
|
||||
Changes mockery mock creation to Prophesize
|
||||
|
||||
```diff
|
||||
-$mock = \Mockery::mock(\'MyClass\');
|
||||
+ $mock = $this->prophesize(\'MyClass\');
|
||||
+
|
||||
$service = new Service();
|
||||
-$service->injectDependency($mock);
|
||||
+$service->injectDependency($mock->reveal());
|
||||
```
|
||||
|
||||
<br><br>
|
||||
|
||||
## MockistaToMockery
|
||||
|
||||
### `MockeryTearDownRector`
|
||||
@ -10539,6 +10585,36 @@ Change "cascade_validation" option to specific node attribute
|
||||
|
||||
<br><br>
|
||||
|
||||
### `ChangeXmlToYamlFileLoaderInExtensionRector`
|
||||
|
||||
- class: [`Rector\Symfony\Rector\Class_\ChangeXmlToYamlFileLoaderInExtensionRector`](/../master/rules/symfony/src/Rector/Class_/ChangeXmlToYamlFileLoaderInExtensionRector.php)
|
||||
- [test fixtures](/../master/rules/symfony/tests/Rector/Class_/ChangeXmlToYamlFileLoaderInExtensionRector/Fixture)
|
||||
|
||||
Change XML loader to YAML in Bundle Extension
|
||||
|
||||
```diff
|
||||
use Symfony\Component\Config\FileLocator;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
-use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
|
||||
+use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
|
||||
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
|
||||
|
||||
final class SomeExtension extends Extension
|
||||
{
|
||||
public function load(array $configs, ContainerBuilder $container)
|
||||
{
|
||||
- $loader = new XmlFileLoader($container, new FileLocator());
|
||||
- $loader->load(__DIR__ . '/../Resources/config/controller.xml');
|
||||
- $loader->load(__DIR__ . '/../Resources/config/events.xml');
|
||||
+ $loader = new YamlFileLoader($container, new FileLocator());
|
||||
+ $loader->load(__DIR__ . '/../Resources/config/controller.yaml');
|
||||
+ $loader->load(__DIR__ . '/../Resources/config/events.yaml');
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<br><br>
|
||||
|
||||
### `ConsoleExceptionToErrorEventConstantRector`
|
||||
|
||||
- class: [`Rector\Symfony\Rector\Console\ConsoleExceptionToErrorEventConstantRector`](/../master/rules/symfony/src/Rector/Console/ConsoleExceptionToErrorEventConstantRector.php)
|
||||
|
@ -0,0 +1,141 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Symfony\Rector\Class_;
|
||||
|
||||
use Nette\Utils\Strings;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use PhpParser\Node\Expr\New_;
|
||||
use PhpParser\Node\Expr\Variable;
|
||||
use PhpParser\Node\Name\FullyQualified;
|
||||
use PhpParser\Node\Scalar\String_;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\Core\RectorDefinition\CodeSample;
|
||||
use Rector\Core\RectorDefinition\RectorDefinition;
|
||||
|
||||
/**
|
||||
* @see \Rector\Symfony\Tests\Rector\Class_\ChangeXmlToYamlFileLoaderInExtensionRector\ChangeXmlToYamlFileLoaderInExtensionRectorTest
|
||||
*/
|
||||
final class ChangeXmlToYamlFileLoaderInExtensionRector extends AbstractRector
|
||||
{
|
||||
public function getDefinition(): RectorDefinition
|
||||
{
|
||||
return new RectorDefinition('Change XML loader to YAML in Bundle Extension', [
|
||||
new CodeSample(
|
||||
<<<'PHP'
|
||||
use Symfony\Component\Config\FileLocator;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
|
||||
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
|
||||
|
||||
final class SomeExtension extends Extension
|
||||
{
|
||||
public function load(array $configs, ContainerBuilder $container)
|
||||
{
|
||||
$loader = new XmlFileLoader($container, new FileLocator());
|
||||
$loader->load(__DIR__ . '/../Resources/config/controller.xml');
|
||||
$loader->load(__DIR__ . '/../Resources/config/events.xml');
|
||||
}
|
||||
}
|
||||
PHP
|
||||
,
|
||||
<<<'PHP'
|
||||
use Symfony\Component\Config\FileLocator;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
|
||||
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
|
||||
|
||||
final class SomeExtension extends Extension
|
||||
{
|
||||
public function load(array $configs, ContainerBuilder $container)
|
||||
{
|
||||
$loader = new YamlFileLoader($container, new FileLocator());
|
||||
$loader->load(__DIR__ . '/../Resources/config/controller.yaml');
|
||||
$loader->load(__DIR__ . '/../Resources/config/events.yaml');
|
||||
}
|
||||
}
|
||||
PHP
|
||||
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [Class_::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Class_ $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
if (! $this->isObjectType($node, 'Symfony\Component\HttpKernel\DependencyInjection\Extension')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$loadClassMethod = $node->getMethod('load');
|
||||
if ($loadClassMethod === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$this->traverseNodesWithCallable((array) $loadClassMethod->stmts, function (Node $node) {
|
||||
if ($node instanceof New_) {
|
||||
if (! $this->isName($node->class, 'Symfony\Component\DependencyInjection\Loader\XmlFileLoader')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$node->class = new FullyQualified('Symfony\Component\DependencyInjection\Loader\YamlFileLoader');
|
||||
return $node;
|
||||
}
|
||||
|
||||
return $this->refactorLoadMethodCall($node);
|
||||
});
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
||||
private function refactorLoadMethodCall(Node $node): ?Node
|
||||
{
|
||||
if (! $node instanceof MethodCall) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! $node->var instanceof Variable) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! $this->isObjectType($node->var, 'Symfony\Component\DependencyInjection\Loader\XmlFileLoader')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! $this->isName($node->name, 'load')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$this->replaceXmlWithYamlSuffix($node);
|
||||
return $node;
|
||||
}
|
||||
|
||||
private function replaceXmlWithYamlSuffix(MethodCall $methodCall): void
|
||||
{
|
||||
// replace XML to YAML suffix in string parts
|
||||
$fileArgument = $methodCall->args[0]->value;
|
||||
|
||||
$this->traverseNodesWithCallable([$fileArgument], function (Node $node): ?Node {
|
||||
if (! $node instanceof String_) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$node->value = Strings::replace($node->value, '#\.xml$#', '.yaml');
|
||||
|
||||
return $node;
|
||||
});
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Symfony\Tests\Rector\Class_\ChangeXmlToYamlFileLoaderInExtensionRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
use Rector\Symfony\Rector\Class_\ChangeXmlToYamlFileLoaderInExtensionRector;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
|
||||
final class ChangeXmlToYamlFileLoaderInExtensionRectorTest 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 ChangeXmlToYamlFileLoaderInExtensionRector::class;
|
||||
}
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\Symfony\Tests\Rector\Class_\ChangeXmlToYamlFileLoaderInExtensionRector\Fixture;
|
||||
|
||||
use Symfony\Component\Config\FileLocator;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
|
||||
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
|
||||
|
||||
final class SomeExtension extends Extension
|
||||
{
|
||||
public function load(array $configs, ContainerBuilder $container)
|
||||
{
|
||||
$loader = new XmlFileLoader($container, new FileLocator());
|
||||
$loader->load(__DIR__ . '/../Resources/config/controller.xml');
|
||||
$loader->load(__DIR__ . '/../Resources/config/events.xml');
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Symfony\Tests\Rector\Class_\ChangeXmlToYamlFileLoaderInExtensionRector\Fixture;
|
||||
|
||||
use Symfony\Component\Config\FileLocator;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
|
||||
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
|
||||
|
||||
final class SomeExtension extends Extension
|
||||
{
|
||||
public function load(array $configs, ContainerBuilder $container)
|
||||
{
|
||||
$loader = new \Symfony\Component\DependencyInjection\Loader\YamlFileLoader($container, new FileLocator());
|
||||
$loader->load(__DIR__ . '/../Resources/config/controller.yaml');
|
||||
$loader->load(__DIR__ . '/../Resources/config/events.yaml');
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
Loading…
x
Reference in New Issue
Block a user