diff --git a/config/set/privatization.php b/config/set/privatization.php index 3598adc5525..e35a7ed6bea 100644 --- a/config/set/privatization.php +++ b/config/set/privatization.php @@ -3,7 +3,6 @@ declare(strict_types=1); use Rector\Config\RectorConfig; -use Rector\Privatization\Rector\Class_\ChangeGlobalVariablesToPropertiesRector; use Rector\Privatization\Rector\Class_\FinalizeClassesWithoutChildrenRector; use Rector\Privatization\Rector\ClassMethod\PrivatizeFinalClassMethodRector; use Rector\Privatization\Rector\MethodCall\PrivatizeLocalGetterToPropertyRector; @@ -12,7 +11,6 @@ return static function (RectorConfig $rectorConfig): void { $rectorConfig->rules([ FinalizeClassesWithoutChildrenRector::class, - ChangeGlobalVariablesToPropertiesRector::class, PrivatizeLocalGetterToPropertyRector::class, PrivatizeFinalClassPropertyRector::class, PrivatizeFinalClassMethodRector::class, diff --git a/rules-tests/Privatization/Rector/Class_/ChangeGlobalVariablesToPropertiesRector/ChangeGlobalVariablesToPropertiesRectorTest.php b/rules-tests/Privatization/Rector/Class_/ChangeGlobalVariablesToPropertiesRector/ChangeGlobalVariablesToPropertiesRectorTest.php deleted file mode 100644 index c7aac1c3519..00000000000 --- a/rules-tests/Privatization/Rector/Class_/ChangeGlobalVariablesToPropertiesRector/ChangeGlobalVariablesToPropertiesRectorTest.php +++ /dev/null @@ -1,28 +0,0 @@ -doTestFile($filePath); - } - - public static function provideData(): Iterator - { - return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); - } - - public function provideConfigFilePath(): string - { - return __DIR__ . '/config/configured_rule.php'; - } -} diff --git a/rules-tests/Privatization/Rector/Class_/ChangeGlobalVariablesToPropertiesRector/Fixture/fixture.php.inc b/rules-tests/Privatization/Rector/Class_/ChangeGlobalVariablesToPropertiesRector/Fixture/fixture.php.inc deleted file mode 100644 index f025a8ad988..00000000000 --- a/rules-tests/Privatization/Rector/Class_/ChangeGlobalVariablesToPropertiesRector/Fixture/fixture.php.inc +++ /dev/null @@ -1,40 +0,0 @@ - ------ -variable = 5; - } - - public function run() - { - var_dump($this->variable); - } -} - -?> diff --git a/rules-tests/Privatization/Rector/Class_/ChangeGlobalVariablesToPropertiesRector/Fixture/global_after.php.inc b/rules-tests/Privatization/Rector/Class_/ChangeGlobalVariablesToPropertiesRector/Fixture/global_after.php.inc deleted file mode 100644 index a37af26d5c7..00000000000 --- a/rules-tests/Privatization/Rector/Class_/ChangeGlobalVariablesToPropertiesRector/Fixture/global_after.php.inc +++ /dev/null @@ -1,44 +0,0 @@ - ------ -variable = 5; - return $this->variable; - } - - public function run() - { - var_dump($this->variable); - } -} - -?> diff --git a/rules-tests/Privatization/Rector/Class_/ChangeGlobalVariablesToPropertiesRector/Fixture/skip_read_only_global.php.inc b/rules-tests/Privatization/Rector/Class_/ChangeGlobalVariablesToPropertiesRector/Fixture/skip_read_only_global.php.inc deleted file mode 100644 index 18ebd298f7e..00000000000 --- a/rules-tests/Privatization/Rector/Class_/ChangeGlobalVariablesToPropertiesRector/Fixture/skip_read_only_global.php.inc +++ /dev/null @@ -1,17 +0,0 @@ - diff --git a/rules-tests/Privatization/Rector/Class_/ChangeGlobalVariablesToPropertiesRector/config/configured_rule.php b/rules-tests/Privatization/Rector/Class_/ChangeGlobalVariablesToPropertiesRector/config/configured_rule.php deleted file mode 100644 index 6f1fe807be4..00000000000 --- a/rules-tests/Privatization/Rector/Class_/ChangeGlobalVariablesToPropertiesRector/config/configured_rule.php +++ /dev/null @@ -1,10 +0,0 @@ -rule(ChangeGlobalVariablesToPropertiesRector::class); -}; diff --git a/rules/Privatization/Rector/Class_/ChangeGlobalVariablesToPropertiesRector.php b/rules/Privatization/Rector/Class_/ChangeGlobalVariablesToPropertiesRector.php deleted file mode 100644 index d580d0f5165..00000000000 --- a/rules/Privatization/Rector/Class_/ChangeGlobalVariablesToPropertiesRector.php +++ /dev/null @@ -1,192 +0,0 @@ -variable = 5; - } - - public function run() - { - var_dump($this->variable); - } -} -CODE_SAMPLE - ), - ] - ); - } - - /** - * @return array> - */ - public function getNodeTypes(): array - { - return [Class_::class]; - } - - /** - * @param Class_ $node - */ - public function refactor(Node $node): ?Node - { - foreach ($node->getMethods() as $classMethod) { - $this->collectGlobalVariableNamesAndRefactorToPropertyFetch($node, $classMethod); - } - - if ($this->globalVariableNames === []) { - return null; - } - - // @todo find ideal property position - $globalProperties = []; - foreach ($this->globalVariableNames as $globalVariableName) { - $globalProperties[] = new Property(Class_::MODIFIER_PRIVATE, [ - new PropertyProperty($globalVariableName), - ]); - } - - array_splice($node->stmts, 0, 0, $globalProperties); - - return $node; - } - - private function collectGlobalVariableNamesAndRefactorToPropertyFetch(Class_ $class, ClassMethod $classMethod): void - { - $this->globalVariableNames = []; - - $this->traverseNodesWithCallable($classMethod, function (Node $node) use ($class): int|PropertyFetch|null { - if ($node instanceof Global_) { - $this->refactorGlobal($class, $node); - return NodeTraverser::DONT_TRAVERSE_CHILDREN; - } - - if ($node instanceof Variable) { - return $this->refactorGlobalVariable($node); - } - - return null; - }); - } - - private function refactorGlobal(Class_ $class, Global_ $global): void - { - foreach ($global->vars as $var) { - $varName = $this->getName($var); - if ($varName === null) { - return; - } - - if ($this->isReadOnly($class, $varName)) { - return; - } - - $this->globalVariableNames[] = $varName; - } - - $this->removeNode($global); - } - - private function refactorGlobalVariable(Variable $variable): ?PropertyFetch - { - if (! $this->isNames($variable, $this->globalVariableNames)) { - return null; - } - - // replace with property fetch - $variableName = $this->getName($variable); - if ($variableName === null) { - return null; - } - - return $this->nodeFactory->createPropertyFetch('this', $variableName); - } - - private function isReadOnly(Class_ $class, string $globalVariableName): bool - { - /** @var ClassMethod[] $classMethods */ - $classMethods = $this->betterNodeFinder->findInstanceOf($class, ClassMethod::class); - foreach ($classMethods as $classMethod) { - $isReAssign = (bool) $this->betterNodeFinder->findFirst((array) $classMethod->stmts, function (Node $node) use ( - $globalVariableName - ): bool { - if (! $node instanceof Assign) { - return false; - } - - if ($node->var instanceof Variable) { - return $this->nodeNameResolver->isName($node->var, $globalVariableName); - } - - if ($node->var instanceof PropertyFetch) { - return $this->nodeNameResolver->isName($node->var, $globalVariableName); - } - - return false; - }); - - if ($isReAssign) { - return false; - } - } - - return true; - } -}