2019-11-16 15:36:58 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\Privatization\Rector\Class_;
|
2019-11-16 15:36:58 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr\Assign;
|
|
|
|
use PhpParser\Node\Expr\PropertyFetch;
|
|
|
|
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;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2019-11-16 15:36:58 +00:00
|
|
|
/**
|
2022-05-12 19:17:07 +00:00
|
|
|
* @changelog https://3v4l.org/DWC4P
|
2019-11-16 15:36:58 +00:00
|
|
|
*
|
2021-04-10 18:47:17 +00:00
|
|
|
* @changelog https://stackoverflow.com/a/12446305/1348344
|
2021-11-25 13:07:04 +00:00
|
|
|
* @see \Rector\Tests\Privatization\Rector\Class_\ChangeGlobalVariablesToPropertiesRector\ChangeGlobalVariablesToPropertiesRectorTest
|
2019-11-16 15:36:58 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class ChangeGlobalVariablesToPropertiesRector extends AbstractRector
|
2019-11-16 15:36:58 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
private $globalVariableNames = [];
|
2021-02-21 23:39:53 +00:00
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\PostRector\Collector\PropertyToAddCollector
|
2021-02-21 23:39:53 +00:00
|
|
|
*/
|
|
|
|
private $propertyToAddCollector;
|
2022-06-07 08:22:29 +00:00
|
|
|
public function __construct(PropertyToAddCollector $propertyToAddCollector)
|
2021-02-21 23:39:53 +00:00
|
|
|
{
|
|
|
|
$this->propertyToAddCollector = $propertyToAddCollector;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2019-11-16 15:36:58 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Change global $variables to private properties', [new CodeSample(<<<'CODE_SAMPLE'
|
2019-11-16 15:36:58 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function go()
|
|
|
|
{
|
|
|
|
global $variable;
|
|
|
|
$variable = 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
global $variable;
|
|
|
|
var_dump($variable);
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2019-11-16 15:36:58 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
private $variable;
|
|
|
|
public function go()
|
|
|
|
{
|
|
|
|
$this->variable = 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
var_dump($this->variable);
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2019-11-16 15:36:58 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2019-11-16 15:36:58 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2019-11-16 15:36:58 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [Class_::class];
|
2019-11-16 15:36:58 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param Class_ $node
|
2019-11-16 15:36:58 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2019-11-16 15:36:58 +00:00
|
|
|
{
|
2021-11-25 13:07:04 +00:00
|
|
|
foreach ($node->getMethods() as $classMethod) {
|
|
|
|
$this->collectGlobalVariableNamesAndRefactorToPropertyFetch($node, $classMethod);
|
2019-11-16 15:36:58 +00:00
|
|
|
}
|
2021-01-09 10:50:47 +00:00
|
|
|
if ($this->globalVariableNames === []) {
|
|
|
|
return null;
|
|
|
|
}
|
2019-11-16 15:36:58 +00:00
|
|
|
foreach ($this->globalVariableNames as $globalVariableName) {
|
2021-11-25 13:07:04 +00:00
|
|
|
$this->propertyToAddCollector->addPropertyWithoutConstructorToClass($globalVariableName, null, $node);
|
2019-11-16 15:36:58 +00:00
|
|
|
}
|
|
|
|
return $node;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function collectGlobalVariableNamesAndRefactorToPropertyFetch(Class_ $class, ClassMethod $classMethod) : void
|
2019-11-16 15:36:58 +00:00
|
|
|
{
|
|
|
|
$this->globalVariableNames = [];
|
2022-06-07 08:22:29 +00:00
|
|
|
$this->traverseNodesWithCallable($classMethod, function (Node $node) use($class) {
|
|
|
|
if ($node instanceof Global_) {
|
2021-07-16 09:16:38 +00:00
|
|
|
$this->refactorGlobal($class, $node);
|
2022-06-07 08:22:29 +00:00
|
|
|
return NodeTraverser::DONT_TRAVERSE_CHILDREN;
|
2019-11-16 15:36:58 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($node instanceof Variable) {
|
2019-11-16 15:36:58 +00:00
|
|
|
return $this->refactorGlobalVariable($node);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function refactorGlobal(Class_ $class, Global_ $global) : void
|
2019-11-16 15:36:58 +00:00
|
|
|
{
|
|
|
|
foreach ($global->vars as $var) {
|
|
|
|
$varName = $this->getName($var);
|
|
|
|
if ($varName === null) {
|
|
|
|
return;
|
|
|
|
}
|
2021-07-16 09:16:38 +00:00
|
|
|
if ($this->isReadOnly($class, $varName)) {
|
|
|
|
return;
|
|
|
|
}
|
2019-11-16 15:36:58 +00:00
|
|
|
$this->globalVariableNames[] = $varName;
|
|
|
|
}
|
|
|
|
$this->removeNode($global);
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function refactorGlobalVariable(Variable $variable) : ?PropertyFetch
|
2020-02-01 16:04:38 +00:00
|
|
|
{
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->isNames($variable, $this->globalVariableNames)) {
|
2020-02-01 16:04:38 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
// replace with property fetch
|
|
|
|
$variableName = $this->getName($variable);
|
|
|
|
if ($variableName === null) {
|
|
|
|
return null;
|
|
|
|
}
|
2021-01-30 21:41:25 +00:00
|
|
|
return $this->nodeFactory->createPropertyFetch('this', $variableName);
|
2020-02-01 16:04:38 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function isReadOnly(Class_ $class, string $globalVariableName) : bool
|
2021-07-16 09:16:38 +00:00
|
|
|
{
|
|
|
|
/** @var ClassMethod[] $classMethods */
|
2022-06-07 08:22:29 +00:00
|
|
|
$classMethods = $this->betterNodeFinder->findInstanceOf($class, ClassMethod::class);
|
2021-07-16 09:16:38 +00:00
|
|
|
foreach ($classMethods as $classMethod) {
|
2022-06-07 08:22:29 +00:00
|
|
|
$isReAssign = (bool) $this->betterNodeFinder->findFirst((array) $classMethod->stmts, function (Node $node) use($globalVariableName) : bool {
|
|
|
|
if (!$node instanceof Assign) {
|
2021-07-16 09:16:38 +00:00
|
|
|
return \false;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($node->var instanceof Variable) {
|
2021-07-16 09:16:38 +00:00
|
|
|
return $this->nodeNameResolver->isName($node->var, $globalVariableName);
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($node->var instanceof PropertyFetch) {
|
2021-07-16 09:16:38 +00:00
|
|
|
return $this->nodeNameResolver->isName($node->var, $globalVariableName);
|
|
|
|
}
|
|
|
|
return \false;
|
|
|
|
});
|
|
|
|
if ($isReAssign) {
|
|
|
|
return \false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return \true;
|
|
|
|
}
|
2019-11-16 15:36:58 +00:00
|
|
|
}
|