2
0
forked from Wavyzz/dolibarr

Add Rector (automatic refactoring)

Add first rule: access global to function (getDolGlobalInt, getDolGlobalString)
This commit is contained in:
Dev2a
2023-10-08 20:25:46 +02:00
parent 04c2317761
commit dc4e17a353
6 changed files with 285 additions and 0 deletions

View File

@@ -0,0 +1,76 @@
<?php
namespace Dolibarr\Rector\Renaming;
use PhpParser\Node;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\Exception\PoorDocumentationException;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
class GlobalToFunction extends AbstractRector
{
/**
* @throws PoorDocumentationException
*/
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Change $conf->global to getDolGlobal',
[new CodeSample('$conf->global->CONSTANT'
, 'getDolGlobalInt(\'CONSTANT\')'
)]);
}
public function getNodeTypes(): array
{
return [Node\Expr\BinaryOp\Equal::class];
}
/**
* @param \PhpParser\Node $node
* @return \PhpParser\Node\Expr\BinaryOp\Equal|void
*/
public function refactor(Node $node)
{
if (!$node instanceof Node\Expr\BinaryOp\Equal) {
return;
};
if (!$node->left instanceof Node\Expr\PropertyFetch) {
return;
}
if (!$this->isName($node->left->var, 'global')) {
return;
}
$global = $node->left->var;
if (!$global instanceof Node\Expr\PropertyFetch) {
return;
}
if (!$this->isName($global->var, 'conf')) {
return;
}
switch ($node->right->getType()) {
case 'Scalar_LNumber':
$funcName = 'getDolGlobalInt';
break;
case 'Scalar_String':
$funcName = 'getDolGlobalString';
break;
default:
return;
}
$constName = $this->getName($node->left);
if (empty($constName)) {
return;
}
return new Node\Expr\BinaryOp\Equal(
new Node\Expr\FuncCall(
new Node\Name($funcName),
[new Node\Arg(new Node\Scalar\String_($constName))]
),
$node->right
);
}
}