Skip to content

Commit

Permalink
[Symfony 5.2] Add BinaryFileResponseCreateToNewInstanceRector (#4846)
Browse files Browse the repository at this point in the history
  • Loading branch information
simivar authored Dec 11, 2020
1 parent e6ca54b commit 668753b
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 0 deletions.
4 changes: 4 additions & 0 deletions config/set/symfony52.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Rector\Renaming\Rector\MethodCall\RenameMethodRector;
use Rector\Renaming\ValueObject\MethodCallRename;
use Rector\Renaming\ValueObject\RenameClassConstant;
use Rector\Symfony\Rector\StaticCall\BinaryFileResponseCreateToNewInstanceRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symplify\SymfonyPhpConfig\ValueObjectInliner;

Expand All @@ -16,6 +17,9 @@

$services = $containerConfigurator->services();

# https://github.com/symfony/symfony/blob/5.x/UPGRADE-5.2.md#httpfoundation
$services->set(BinaryFileResponseCreateToNewInstanceRector::class);

# https://github.com/symfony/symfony/blob/5.x/UPGRADE-5.2.md#mime
$services->set(RenameMethodRector::class)
->call('configure', [[
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

declare(strict_types=1);

namespace Rector\Symfony\Rector\StaticCall;

use PhpParser\Node;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Name;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see https://github.com/symfony/symfony/blob/5.x/UPGRADE-5.2.md#httpfoundation
* @see \Rector\Symfony\Tests\Rector\StaticCall\BinaryFileResponseCreateToNewInstanceRector\BinaryFileResponseCreateToNewInstanceRectorTest
*/
final class BinaryFileResponseCreateToNewInstanceRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Change deprecated BinaryFileResponse::create() to use __construct() instead', [
new CodeSample(
<<<'CODE_SAMPLE'
use Symfony\Component\HttpFoundation;
class SomeClass
{
public function run()
{
$binaryFile = BinaryFileResponse::create();
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use Symfony\Component\HttpFoundation;
class SomeClass
{
public function run()
{
$binaryFile = new BinaryFileResponse(null);
}
}
CODE_SAMPLE
),
]);
}

/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [StaticCall::class];
}

/**
* @param StaticCall $node
*/
public function refactor(Node $node): ?Node
{
if (! $node->class instanceof Name) {
return null;
}

if (! $this->isName($node->class, 'Symfony\Component\HttpFoundation\BinaryFileResponse')) {
return null;
}

if (! $this->isName($node->name, 'create')) {
return null;
}

$args = $node->args;
if ([] === $args) {
$args[] = $this->createArg($this->createNull());
}

return new New_($node->class, $args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Rector\Symfony\Tests\Rector\StaticCall\BinaryFileResponseCreateToNewInstanceRector;

use Iterator;
use Rector\Symfony\Rector\StaticCall\BinaryFileResponseCreateToNewInstanceRector;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;

final class BinaryFileResponseCreateToNewInstanceRectorTest 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 BinaryFileResponseCreateToNewInstanceRector::class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Rector\Symfony\Tests\Rector\StaticCall\BinaryFileResponseCreateToNewInstanceRector\Fixture;

use Symfony\Component\HttpFoundation\BinaryFileResponse;

class CreateWithArgument
{
public function action()
{
return BinaryFileResponse::create('some_file_path.txt');
}
}

?>
-----
<?php

namespace Rector\Symfony\Tests\Rector\StaticCall\BinaryFileResponseCreateToNewInstanceRector\Fixture;

use Symfony\Component\HttpFoundation\BinaryFileResponse;

class CreateWithArgument
{
public function action()
{
return new BinaryFileResponse('some_file_path.txt');
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Rector\Symfony\Tests\Rector\StaticCall\BinaryFileResponseCreateToNewInstanceRector\Fixture;

use Symfony\Component\HttpFoundation\BinaryFileResponse;

class CreateWithoutArguments
{
public function action()
{
$binaryFileResponse = BinaryFileResponse::create();
}
}

?>
-----
<?php

namespace Rector\Symfony\Tests\Rector\StaticCall\BinaryFileResponseCreateToNewInstanceRector\Fixture;

use Symfony\Component\HttpFoundation\BinaryFileResponse;

class CreateWithoutArguments
{
public function action()
{
$binaryFileResponse = new BinaryFileResponse(null);
}
}

?>

0 comments on commit 668753b

Please sign in to comment.