From fa4247a8125d3cd528dfdcbed767619e553632c4 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Wed, 3 Jul 2024 19:35:06 +0200 Subject: [PATCH] More valid rector rules to change conf->global->xxx --- dev/tools/rector/rector.php | 13 ++++-- .../rector/src/Renaming/GlobalToFunction.php | 41 +++++++++++-------- 2 files changed, 33 insertions(+), 21 deletions(-) diff --git a/dev/tools/rector/rector.php b/dev/tools/rector/rector.php index 0d527997b8334..154a01f3cec27 100644 --- a/dev/tools/rector/rector.php +++ b/dev/tools/rector/rector.php @@ -48,6 +48,7 @@ __DIR__ . '/../../../scripts/', __DIR__ . '/../../../test/phpunit/', ]); + $rectorConfig->skip([ '**/includes/**', '**/custom/**', @@ -55,6 +56,7 @@ '**/rector/**', // Disable this line to test the "test.php" file. __DIR__ . '/../../../htdocs/custom/', __DIR__ . '/../../../htdocs/install/doctemplates/*' + //'test.php', ]); $rectorConfig->parallel(240); @@ -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); @@ -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); diff --git a/dev/tools/rector/src/Renaming/GlobalToFunction.php b/dev/tools/rector/src/Renaming/GlobalToFunction.php index 302a2ff4a2a4a..1c98c9671f654 100644 --- a/dev/tools/rector/src/Renaming/GlobalToFunction.php +++ b/dev/tools/rector/src/Renaming/GlobalToFunction.php @@ -64,8 +64,8 @@ public function getRuleDefinition(): RuleDefinition [new CodeSample( '$conf->global->CONSTANT', 'getDolGlobalInt(\'CONSTANT\')' - )] - ); + )] + ); } /** @@ -133,7 +133,7 @@ public function refactor(Node $node) $node->dim = new FuncCall( new Name('getDolGlobalString'), [new Arg($constName)] - ); + ); } return $node; } @@ -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)) { @@ -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)) { @@ -238,6 +238,7 @@ public function refactor(Node $node) $node = $nodes->getFirstExpr(); } + // Now process all comparison like: // $conf->global->... Operator Value @@ -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; } @@ -282,7 +286,8 @@ public function refactor(Node $node) $funcName = 'getDolGlobalString'; break; default: - return; + $funcName = 'getDolGlobalString'; + break; } $constName = $this->getConstName($node->left); @@ -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( @@ -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( @@ -382,7 +387,7 @@ function (Node $node): bool { } return \true; } - ); + ); } /**