Skip to content

Commit

Permalink
More valid rector rules to change conf->global->xxx
Browse files Browse the repository at this point in the history
  • Loading branch information
eldy committed Jul 3, 2024
1 parent bd3b7a1 commit fa4247a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 21 deletions.
13 changes: 10 additions & 3 deletions dev/tools/rector/rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,15 @@
__DIR__ . '/../../../scripts/',
__DIR__ . '/../../../test/phpunit/',
]);

$rectorConfig->skip([
'**/includes/**',
'**/custom/**',
'**/vendor/**',
'**/rector/**', // Disable this line to test the "test.php" file.
__DIR__ . '/../../../htdocs/custom/',
__DIR__ . '/../../../htdocs/install/doctemplates/*'
//'test.php',
]);
$rectorConfig->parallel(240);

Expand All @@ -70,9 +72,9 @@

//$rectorConfig->rule(ReplaceEachAssignmentWithKeyCurrentRector::class);


$rectorConfig->rule(Rector\CodeQuality\Rector\FuncCall\FloatvalToTypeCastRector::class);
$rectorConfig->rule(Rector\CodeQuality\Rector\FuncCall\BoolvalToTypeCastRector::class);
$rectorConfig->rule(Rector\CodeQuality\Rector\NotEqual\CommonNotEqualRector::class);
//Not yet ready: $rectorconfig->rule(Rector\CodeQuality\Rector\If_\CompleteMissingIfElseBracketRector::class);
$rectorConfig->rule(Rector\CodeQuality\Rector\For_\ForRepeatedCountToOwnVariableRector::class);

Expand All @@ -82,11 +84,16 @@
$rectorConfig->rule(Dolibarr\Rector\Renaming\UserRightsToFunction::class);
//$rectorConfig->rule(Dolibarr\Rector\Renaming\UsePositiveExit::class);


// This fix <> into != but it breaks other rules, so added at end.
$rectorConfig->rule(Rector\CodeQuality\Rector\NotEqual\CommonNotEqualRector::class);


// Add all predefined rules to migrate to up to php 71.
// Warning this break tab spacing of arrays on several lines
/*$rectorConfig->sets([
LevelSetList::UP_TO_PHP_70
]);*/
LevelSetList::UP_TO_PHP_70
]);*/
// Add predefined rules for a given version only
//$rectorConfig->import(SetList::PHP_70);
//$rectorConfig->import(SetList::PHP_71);
Expand Down
41 changes: 23 additions & 18 deletions dev/tools/rector/src/Renaming/GlobalToFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ public function getRuleDefinition(): RuleDefinition
[new CodeSample(
'$conf->global->CONSTANT',
'getDolGlobalInt(\'CONSTANT\')'
)]
);
)]
);
}

/**
Expand Down Expand Up @@ -133,7 +133,7 @@ public function refactor(Node $node)
$node->dim = new FuncCall(
new Name('getDolGlobalString'),
[new Arg($constName)]
);
);
}
return $node;
}
Expand Down Expand Up @@ -208,7 +208,7 @@ public function refactor(Node $node)
$leftConcat = new FuncCall(
new Name('getDolGlobalString'),
[new Arg($constName)]
);
);
$rightConcat = $node->right;
}
if ($this->isGlobalVar($node->right)) {
Expand All @@ -219,7 +219,7 @@ public function refactor(Node $node)
$rightConcat = new FuncCall(
new Name('getDolGlobalString'),
[new Arg($constName)]
);
);
$leftConcat = $node->left;
}
if (!isset($leftConcat, $rightConcat)) {
Expand All @@ -238,6 +238,7 @@ public function refactor(Node $node)
$node = $nodes->getFirstExpr();
}


// Now process all comparison like:
// $conf->global->... Operator Value

Expand All @@ -264,11 +265,14 @@ public function refactor(Node $node)
$typeofcomparison = 'NotIdentical';
//var_dump($node->left);
}

if (empty($typeofcomparison)) {
return;
}

if (!$this->isGlobalVar($node->left)) {
$isconfglobal = $this->isGlobalVar($node->left);
if (!$isconfglobal) {
// The left side is not conf->global->xxx, so we leave
return;
}

Expand All @@ -282,7 +286,8 @@ public function refactor(Node $node)
$funcName = 'getDolGlobalString';
break;
default:
return;
$funcName = 'getDolGlobalString';
break;
}

$constName = $this->getConstName($node->left);
Expand All @@ -295,9 +300,9 @@ public function refactor(Node $node)
new FuncCall(
new Name($funcName),
[new Arg($constName)]
),
),
$node->right
);
);
}
if ($typeofcomparison == 'NotEqual') {
return new NotEqual(
Expand All @@ -313,36 +318,36 @@ public function refactor(Node $node)
new FuncCall(
new Name($funcName),
[new Arg($constName)]
),
),
$node->right
);
);
}
if ($typeofcomparison == 'GreaterOrEqual') {
return new GreaterOrEqual(
new FuncCall(
new Name($funcName),
[new Arg($constName)]
),
),
$node->right
);
);
}
if ($typeofcomparison == 'Smaller') {
return new Smaller(
new FuncCall(
new Name($funcName),
[new Arg($constName)]
),
),
$node->right
);
);
}
if ($typeofcomparison == 'SmallerOrEqual') {
return new SmallerOrEqual(
new FuncCall(
new Name($funcName),
[new Arg($constName)]
),
),
$node->right
);
);
}
if ($typeofcomparison == 'NotIdentical') {
return new NotIdentical(
Expand Down Expand Up @@ -382,7 +387,7 @@ function (Node $node): bool {
}
return \true;
}
);
);
}

/**
Expand Down

0 comments on commit fa4247a

Please sign in to comment.