mirror of
https://github.com/rectorphp/rector.git
synced 2024-05-31 00:10:51 +00:00
Move ChangeGlobalVariablesToPropertiesRector from ClassMethod to Class_ node, to keep the minimal changed scope in active nodes (#1307)
This commit is contained in:
parent
62474db941
commit
dd7bc52135
|
@ -8883,7 +8883,7 @@ Add unique use imports collected during Rector run
|
|||
|
||||
Change global `$variables` to private properties
|
||||
|
||||
- class: [`Rector\Privatization\Rector\ClassMethod\ChangeGlobalVariablesToPropertiesRector`](../rules/Privatization/Rector/ClassMethod/ChangeGlobalVariablesToPropertiesRector.php)
|
||||
- class: [`Rector\Privatization\Rector\Class_\ChangeGlobalVariablesToPropertiesRector`](../rules/Privatization/Rector/ClassMethod/ChangeGlobalVariablesToPropertiesRector.php)
|
||||
|
||||
```diff
|
||||
class SomeClass
|
||||
|
|
|
@ -2,11 +2,11 @@
|
|||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Rector\Privatization\Rector\Class_\ChangeGlobalVariablesToPropertiesRector;
|
||||
use Rector\Privatization\Rector\Class_\ChangeLocalPropertyToVariableRector;
|
||||
use Rector\Privatization\Rector\Class_\ChangeReadOnlyVariableWithDefaultValueToConstantRector;
|
||||
use Rector\Privatization\Rector\Class_\FinalizeClassesWithoutChildrenRector;
|
||||
use Rector\Privatization\Rector\Class_\RepeatedLiteralToClassConstantRector;
|
||||
use Rector\Privatization\Rector\ClassMethod\ChangeGlobalVariablesToPropertiesRector;
|
||||
use Rector\Privatization\Rector\ClassMethod\PrivatizeFinalClassMethodRector;
|
||||
use Rector\Privatization\Rector\MethodCall\PrivatizeLocalGetterToPropertyRector;
|
||||
use Rector\Privatization\Rector\Property\ChangeReadOnlyPropertyWithDefaultValueToConstantRector;
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Privatization\Rector\ClassMethod\ChangeGlobalVariablesToPropertiesRector;
|
||||
namespace Rector\Tests\Privatization\Rector\Class_\ChangeGlobalVariablesToPropertiesRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Privatization\Rector\ClassMethod\ChangeGlobalVariablesToPropertiesRector\Fixture;
|
||||
namespace Rector\Tests\Privatization\Rector\Class_\ChangeGlobalVariablesToPropertiesRector\Fixture;
|
||||
|
||||
class Fixture
|
||||
{
|
||||
|
@ -21,7 +21,7 @@ class Fixture
|
|||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\Privatization\Rector\ClassMethod\ChangeGlobalVariablesToPropertiesRector\Fixture;
|
||||
namespace Rector\Tests\Privatization\Rector\Class_\ChangeGlobalVariablesToPropertiesRector\Fixture;
|
||||
|
||||
class Fixture
|
||||
{
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Privatization\Rector\ClassMethod\ChangeGlobalVariablesToPropertiesRector\Fixture;
|
||||
namespace Rector\Tests\Privatization\Rector\Class_\ChangeGlobalVariablesToPropertiesRector\Fixture;
|
||||
|
||||
class GlobalAfter
|
||||
{
|
||||
|
@ -23,7 +23,7 @@ class GlobalAfter
|
|||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\Privatization\Rector\ClassMethod\ChangeGlobalVariablesToPropertiesRector\Fixture;
|
||||
namespace Rector\Tests\Privatization\Rector\Class_\ChangeGlobalVariablesToPropertiesRector\Fixture;
|
||||
|
||||
class GlobalAfter
|
||||
{
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Privatization\Rector\ClassMethod\ChangeGlobalVariablesToPropertiesRector\Fixture;
|
||||
namespace Rector\Tests\Privatization\Rector\Class_\ChangeGlobalVariablesToPropertiesRector\Fixture;
|
||||
|
||||
$variable = 'value';
|
||||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Rector\Privatization\Rector\ClassMethod\ChangeGlobalVariablesToPropertiesRector;
|
||||
use Rector\Privatization\Rector\Class_\ChangeGlobalVariablesToPropertiesRector;
|
||||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||
|
||||
return static function (ContainerConfigurator $containerConfigurator): void {
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Privatization\Rector\ClassMethod;
|
||||
namespace Rector\Privatization\Rector\Class_;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\Assign;
|
||||
|
@ -11,6 +11,7 @@ use PhpParser\Node\Expr\Variable;
|
|||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Global_;
|
||||
use PhpParser\NodeTraverser;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\PostRector\Collector\PropertyToAddCollector;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
|
@ -20,7 +21,7 @@ use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
|||
* @see https://3v4l.org/DWC4P
|
||||
*
|
||||
* @changelog https://stackoverflow.com/a/12446305/1348344
|
||||
* @see \Rector\Tests\Privatization\Rector\ClassMethod\ChangeGlobalVariablesToPropertiesRector\ChangeGlobalVariablesToPropertiesRectorTest
|
||||
* @see \Rector\Tests\Privatization\Rector\Class_\ChangeGlobalVariablesToPropertiesRector\ChangeGlobalVariablesToPropertiesRectorTest
|
||||
*/
|
||||
final class ChangeGlobalVariablesToPropertiesRector extends AbstractRector
|
||||
{
|
||||
|
@ -82,27 +83,24 @@ CODE_SAMPLE
|
|||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [ClassMethod::class];
|
||||
return [Class_::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ClassMethod $node
|
||||
* @param Class_ $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
$classLike = $this->betterNodeFinder->findParentType($node, Class_::class);
|
||||
if (! $classLike instanceof Class_) {
|
||||
return null;
|
||||
foreach ($node->getMethods() as $classMethod) {
|
||||
$this->collectGlobalVariableNamesAndRefactorToPropertyFetch($node, $classMethod);
|
||||
}
|
||||
|
||||
$this->collectGlobalVariableNamesAndRefactorToPropertyFetch($classLike, $node);
|
||||
|
||||
if ($this->globalVariableNames === []) {
|
||||
return null;
|
||||
}
|
||||
|
||||
foreach ($this->globalVariableNames as $globalVariableName) {
|
||||
$this->propertyToAddCollector->addPropertyWithoutConstructorToClass($globalVariableName, null, $classLike);
|
||||
$this->propertyToAddCollector->addPropertyWithoutConstructorToClass($globalVariableName, null, $node);
|
||||
}
|
||||
|
||||
return $node;
|
||||
|
@ -112,10 +110,10 @@ CODE_SAMPLE
|
|||
{
|
||||
$this->globalVariableNames = [];
|
||||
|
||||
$this->traverseNodesWithCallable($classMethod, function (Node $node) use ($class): ?PropertyFetch {
|
||||
$this->traverseNodesWithCallable($classMethod, function (Node $node) use ($class) {
|
||||
if ($node instanceof Global_) {
|
||||
$this->refactorGlobal($class, $node);
|
||||
return null;
|
||||
return NodeTraverser::DONT_TRAVERSE_CHILDREN;
|
||||
}
|
||||
|
||||
if ($node instanceof Variable) {
|
Loading…
Reference in New Issue
Block a user