Move ChangeGlobalVariablesToPropertiesRector from ClassMethod to Class_ node, to keep the minimal changed scope in active nodes (#1307)

This commit is contained in:
Tomas Votruba 2021-11-25 15:49:59 +03:00 committed by GitHub
parent 62474db941
commit dd7bc52135
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 19 additions and 21 deletions

View File

@ -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

View File

@ -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;

View File

@ -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;

View File

@ -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
{

View File

@ -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
{

View File

@ -1,6 +1,6 @@
<?php
namespace Rector\Tests\Privatization\Rector\ClassMethod\ChangeGlobalVariablesToPropertiesRector\Fixture;
namespace Rector\Tests\Privatization\Rector\Class_\ChangeGlobalVariablesToPropertiesRector\Fixture;
$variable = 'value';

View File

@ -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 {

View File

@ -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) {