-
-
Notifications
You must be signed in to change notification settings - Fork 687
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Symfony 5.2] Add BinaryFileResponseCreateToNewInstanceRector (#4846)
- Loading branch information
Showing
5 changed files
with
181 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
rules/symfony/src/Rector/StaticCall/BinaryFileResponseCreateToNewInstanceRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...FileResponseCreateToNewInstanceRector/BinaryFileResponseCreateToNewInstanceRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...aticCall/BinaryFileResponseCreateToNewInstanceRector/Fixture/create_with_argument.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} | ||
|
||
?> |
31 changes: 31 additions & 0 deletions
31
...Call/BinaryFileResponseCreateToNewInstanceRector/Fixture/create_without_arguments.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
?> |