Clean code

This commit is contained in:
ldestailleur
2025-09-18 14:52:39 +02:00
parent a441b412cc
commit f665a0301e
7 changed files with 80 additions and 70 deletions

View File

@@ -63,7 +63,7 @@ class GlobalToFunction extends AbstractRector
'Change $conf->global to getDolGlobal in context (1) conf->global Operator Value or (2) function(conf->global...)',
[new CodeSample(
'$conf->global->CONSTANT',
'getDolGlobalInt(\'CONSTANT\')'
'getDolGlobalString|Int(\'CONSTANT\')'
)]
);
}
@@ -228,14 +228,36 @@ class GlobalToFunction extends AbstractRector
return new Concat($leftConcat, $rightConcat);
}
// If a && b and a or b is conf->global, we replace a or/and b
if ($node instanceof BooleanAnd) {
$nodes = $this->resolveTwoNodeMatch($node);
if (!isset($nodes)) {
return;
// Transformation sur le nœud gauche
$changedone = 0;
if ($this->isGlobalVar($node->left)) {
$constName = $this->getConstName($node->left);
if (empty($constName)) {
return;
}
$node->left = new FuncCall(
new Name('getDolGlobalString'),
[new Arg($constName)]
);
$changedone++;
}
if ($this->isGlobalVar($node->right)) {
$constName = $this->getConstName($node->right);
if (empty($constName)) {
return;
}
$node->right = new FuncCall(
new Name('getDolGlobalString'),
[new Arg($constName)]
);
$changedone++;
}
/** @var Equal $node */
$node = $nodes->getFirstExpr();
if ($changedone) {
return $node;
}
}
@@ -270,9 +292,27 @@ class GlobalToFunction extends AbstractRector
return;
}
// Now we process a comparison
$isconfglobal = $this->isGlobalVar($node->left);
if (!$isconfglobal) {
// The left side is not conf->global->xxx, so we leave
$isconfglobal = $this->isGlobalVar($node->right);
if (!$isconfglobal) {
// The left side and right side is not conf->global->xxx, so we leave
return;
}
// Right side is conf->global->xxx, but we can't know if we must use getDolGlobalInt or String,
// so we use getDolGlobalString if comparison is ==
if ($typeofcomparison == 'Equal') {
$constName = $this->getConstName($node->right);
if (empty($constName)) {
return;
}
$node->right = new FuncCall(
new Name('getDolGlobalString'),
[new Arg($constName)]
);
return $node;
}
return;
}
@@ -360,36 +400,6 @@ class GlobalToFunction extends AbstractRector
}
}
/**
* Get nodes with check empty
*
* @param BooleanAnd $booleanAnd A BooleandAnd
* @return TwoNodeMatch|null
*/
private function resolveTwoNodeMatch(BooleanAnd $booleanAnd): ?TwoNodeMatch
{
return $this->binaryOpManipulator->matchFirstAndSecondConditionNode(
$booleanAnd,
// Function to check if we are in the case $conf->global->... == $value
function (Node $node): bool {
if (!$node instanceof Equal) {
return \false;
}
return $this->isGlobalVar($node->left);
},
// !empty(...) || isset(...)
function (Node $node): bool {
if ($node instanceof BooleanNot && $node->expr instanceof Empty_) {
return $this->isGlobalVar($node->expr->expr);
}
if (!$node instanceof Isset_) {
return $this->isGlobalVar($node);
}
return \true;
}
);
}
/**
* Check if node is a global access with format conf->global->XXX
*