[Symfony 4.2] Add Cookie to create() deprecations

This commit is contained in:
Tomas Votruba 2019-03-04 14:26:26 +01:00
parent b7eae771e8
commit b49af77c9b
6 changed files with 170 additions and 1 deletions

View File

@ -1,4 +1,8 @@
services:
# https://github.com/symfony/symfony/pull/28447
Rector\Rector\New_\NewToStaticCallRector:
Symfony\Component\HttpFoundation\Cookie: ['Symfony\Component\HttpFoundation\Cookie', 'create']
Rector\Rector\Class_\ClassReplacerRector:
# https://github.com/symfony/symfony/commit/a7e319d9e1316e2e18843f8ce15b67a8693e5bf9
Symfony\Bundle\FrameworkBundle\Controller\Controller: 'Symfony\Bundle\FrameworkBundle\Controller\AbstractController'
@ -93,4 +97,3 @@ services:
__construct:
0: ~
1: ~

View File

@ -0,0 +1,84 @@
<?php declare(strict_types=1);
namespace Rector\Rector\New_;
use PhpParser\Node;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Name\FullyQualified;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\ConfiguredCodeSample;
use Rector\RectorDefinition\RectorDefinition;
final class NewToStaticCallRector extends AbstractRector
{
/**
* @var string[]
*/
private $typeToStaticCalls = [];
/**
* @param string[] $typeToStaticCalls
*/
public function __construct(array $typeToStaticCalls = [])
{
$this->typeToStaticCalls = $typeToStaticCalls;
}
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Change new Object to static call', [
new ConfiguredCodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
new Cookie($name);
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
Cookie::create($name);
}
}
CODE_SAMPLE
,
[
'Cookie' => [['Cookie', 'create']],
]
),
]);
}
/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [New_::class];
}
/**
* @param New_ $node
*/
public function refactor(Node $node): ?Node
{
foreach ($this->typeToStaticCalls as $type => $staticCall) {
if (! $this->isType($node->class, $type)) {
continue;
}
$className = new FullyQualified($staticCall[0]);
return new StaticCall($className, $staticCall[1], $node->args);
}
return $node;
}
}

View File

@ -0,0 +1,31 @@
<?php
namespace Rector\Tests\Rector\New_\NewToStaticCallRector\Fixture;
use Rector\Tests\Rector\New_\NewToStaticCallRector\Source\FromNewClass;
class SomeClass
{
public function run()
{
new FromNewClass($name);
}
}
?>
-----
<?php
namespace Rector\Tests\Rector\New_\NewToStaticCallRector\Fixture;
use Rector\Tests\Rector\New_\NewToStaticCallRector\Source\FromNewClass;
class SomeClass
{
public function run()
{
\Rector\Tests\Rector\New_\NewToStaticCallRector\Source\IntoStaticClass::run($name);
}
}
?>

View File

@ -0,0 +1,31 @@
<?php declare(strict_types=1);
namespace Rector\Tests\Rector\New_\NewToStaticCallRector;
use Rector\Rector\New_\NewToStaticCallRector;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Rector\Tests\Rector\New_\NewToStaticCallRector\Source\FromNewClass;
use Rector\Tests\Rector\New_\NewToStaticCallRector\Source\IntoStaticClass;
final class NewToStaticCallRectorTest extends AbstractRectorTestCase
{
public function test(): void
{
$this->doTestFiles([__DIR__ . '/Fixture/fixture.php.inc']);
}
protected function getRectorClass(): string
{
return NewToStaticCallRector::class;
}
/**
* @return mixed[]|null
*/
protected function getRectorConfiguration(): ?array
{
return [
FromNewClass::class => [IntoStaticClass::class, 'run'],
];
}
}

View File

@ -0,0 +1,8 @@
<?php declare(strict_types=1);
namespace Rector\Tests\Rector\New_\NewToStaticCallRector\Source;
final class FromNewClass
{
}

View File

@ -0,0 +1,12 @@
<?php declare(strict_types=1);
namespace Rector\Tests\Rector\New_\NewToStaticCallRector\Source;
final class IntoStaticClass
{
public static function run()
{
}
}