-
-
Notifications
You must be signed in to change notification settings - Fork 672
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b305983
commit ed316d4
Showing
9 changed files
with
271 additions
and
6 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
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
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
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
103 changes: 103 additions & 0 deletions
103
rules/transform/src/Rector/StaticCall/StaticCallToNewRector.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,103 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Transform\Rector\StaticCall; | ||
|
||
use PhpParser\Node; | ||
use Rector\Core\Contract\Rector\ConfigurableRectorInterface; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see https://github.com/symfony/symfony/pull/35308 | ||
* | ||
* @see \Rector\Transform\Tests\Rector\StaticCall\StaticCallToNewRector\StaticCallToNewRectorTest | ||
*/ | ||
final class StaticCallToNewRector extends AbstractRector implements ConfigurableRectorInterface | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
public const METHODS_BY_TYPES = 'methods_by_types'; | ||
|
||
/** | ||
* @var array<string, string> | ||
*/ | ||
private $methodsByTypes = []; | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Change static call to new instance', [ | ||
new ConfiguredCodeSample( | ||
<<<'CODE_SAMPLE' | ||
class SomeClass | ||
{ | ||
public function run() | ||
{ | ||
$dotenv = JsonResponse::create(true); | ||
} | ||
} | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
class SomeClass | ||
{ | ||
public function run() | ||
{ | ||
$dotenv = new JsonResponse(); | ||
} | ||
} | ||
CODE_SAMPLE | ||
, | ||
[ | ||
self::METHODS_BY_TYPES => [ | ||
'JsonResponse' => 'create', | ||
], | ||
] | ||
), | ||
]); | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [Node\Expr\StaticCall::class]; | ||
} | ||
|
||
/** | ||
* @param Node\Expr\StaticCall $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
foreach ($this->methodsByTypes as $type => $method) { | ||
if (! $this->isName($node->class, $type)) { | ||
continue; | ||
} | ||
|
||
if (! $this->isName($node->name, $method)) { | ||
continue; | ||
} | ||
|
||
$class = $this->getName($node->class); | ||
if ($class === null) { | ||
continue; | ||
} | ||
|
||
return new Node\Expr\New_(new Node\Name\FullyQualified($class)); | ||
} | ||
|
||
return $node; | ||
} | ||
|
||
/** | ||
* @param mixed[] $configuration | ||
*/ | ||
public function configure(array $configuration): void | ||
{ | ||
$this->methodsByTypes = $configuration[self::METHODS_BY_TYPES] ?? []; | ||
} | ||
} |
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
31 changes: 31 additions & 0 deletions
31
rules/transform/tests/Rector/StaticCall/StaticCallToNewRector/Fixture/some_class.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\Transform\Tests\Rector\StaticCall\StaticCallToNewRector\Fixture; | ||
|
||
use Rector\Transform\Tests\Rector\StaticCall\StaticCallToNewRector\Source\SomeJsonResponse; | ||
|
||
class SomeClass | ||
{ | ||
public function run() | ||
{ | ||
$dotenv = SomeJsonResponse::create(true); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Transform\Tests\Rector\StaticCall\StaticCallToNewRector\Fixture; | ||
|
||
use Rector\Transform\Tests\Rector\StaticCall\StaticCallToNewRector\Source\SomeJsonResponse; | ||
|
||
class SomeClass | ||
{ | ||
public function run() | ||
{ | ||
$dotenv = new \Rector\Transform\Tests\Rector\StaticCall\StaticCallToNewRector\Source\SomeJsonResponse(); | ||
} | ||
} | ||
|
||
?> |
10 changes: 10 additions & 0 deletions
10
rules/transform/tests/Rector/StaticCall/StaticCallToNewRector/Source/SomeJsonResponse.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,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Transform\Tests\Rector\StaticCall\StaticCallToNewRector\Source; | ||
|
||
final class SomeJsonResponse | ||
{ | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
rules/transform/tests/Rector/StaticCall/StaticCallToNewRector/StaticCallToNewRectorTest.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,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Transform\Tests\Rector\StaticCall\StaticCallToNewRector; | ||
|
||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
use Rector\Transform\Tests\Rector\StaticCall\StaticCallToNewRector\Source\SomeJsonResponse; | ||
|
||
final class StaticCallToNewRectorTest extends AbstractRectorTestCase | ||
{ | ||
/** | ||
* @dataProvider provideData() | ||
*/ | ||
public function test(\Symplify\SmartFileSystem\SmartFileInfo $fileInfo): void | ||
{ | ||
$this->doTestFileInfo($fileInfo); | ||
} | ||
|
||
public function provideData(): \Iterator | ||
{ | ||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
/** | ||
* @return mixed[] | ||
*/ | ||
protected function getRectorsWithConfiguration(): array | ||
{ | ||
return [ | ||
\Rector\Transform\Rector\StaticCall\StaticCallToNewRector::class => | ||
[ | ||
\Rector\Transform\Rector\StaticCall\StaticCallToNewRector::METHODS_BY_TYPES => [ | ||
SomeJsonResponse::class => 'create', | ||
], | ||
], | ||
]; | ||
} | ||
} |