From dc4e17a353bb6b83f53517111c1ec57a9a8f2918 Mon Sep 17 00:00:00 2001 From: Dev2a Date: Sun, 8 Oct 2023 20:25:46 +0200 Subject: [PATCH] Add Rector (automatic refactoring) Add first rule: access global to function (getDolGlobalInt, getDolGlobalString) --- dev/.gitignore | 1 + dev/tools/rector/composer.json | 19 +++ dev/tools/rector/composer.lock | 137 ++++++++++++++++++ dev/tools/rector/readme.md | 22 +++ dev/tools/rector/rector.php | 30 ++++ .../rector/src/Renaming/GlobalToFunction.php | 76 ++++++++++ 6 files changed, 285 insertions(+) create mode 100644 dev/tools/rector/composer.json create mode 100644 dev/tools/rector/composer.lock create mode 100644 dev/tools/rector/readme.md create mode 100644 dev/tools/rector/rector.php create mode 100644 dev/tools/rector/src/Renaming/GlobalToFunction.php diff --git a/dev/.gitignore b/dev/.gitignore index eaecc7e3425..65c707c2dce 100644 --- a/dev/.gitignore +++ b/dev/.gitignore @@ -1 +1,2 @@ /spec +/tools/rector/vendor/ diff --git a/dev/tools/rector/composer.json b/dev/tools/rector/composer.json new file mode 100644 index 00000000000..455dd2ee025 --- /dev/null +++ b/dev/tools/rector/composer.json @@ -0,0 +1,19 @@ +{ + "name": "dolibarr/rector", + "type": "project", + "license": "GplV3", + "authors": [ + { + "name": "Dev2a", + "email": "contact@dev2a.pro" + } + ], + "require-dev": { + "rector/rector": "^0.18.5" + }, + "autoload-dev": { + "psr-4": { + "Dolibarr\\Rector\\": "./src" + } + } +} diff --git a/dev/tools/rector/composer.lock b/dev/tools/rector/composer.lock new file mode 100644 index 00000000000..7350920136d --- /dev/null +++ b/dev/tools/rector/composer.lock @@ -0,0 +1,137 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", + "This file is @generated automatically" + ], + "content-hash": "f2998987cad52db5ab60d5ff0672ce05", + "packages": [], + "packages-dev": [ + { + "name": "phpstan/phpstan", + "version": "1.10.38", + "source": { + "type": "git", + "url": "https://github.com/phpstan/phpstan.git", + "reference": "5302bb402c57f00fb3c2c015bac86e0827e4b691" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/5302bb402c57f00fb3c2c015bac86e0827e4b691", + "reference": "5302bb402c57f00fb3c2c015bac86e0827e4b691", + "shasum": "" + }, + "require": { + "php": "^7.2|^8.0" + }, + "conflict": { + "phpstan/phpstan-shim": "*" + }, + "bin": [ + "phpstan", + "phpstan.phar" + ], + "type": "library", + "autoload": { + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "PHPStan - PHP Static Analysis Tool", + "keywords": [ + "dev", + "static analysis" + ], + "support": { + "docs": "https://phpstan.org/user-guide/getting-started", + "forum": "https://github.com/phpstan/phpstan/discussions", + "issues": "https://github.com/phpstan/phpstan/issues", + "security": "https://github.com/phpstan/phpstan/security/policy", + "source": "https://github.com/phpstan/phpstan-src" + }, + "funding": [ + { + "url": "https://github.com/ondrejmirtes", + "type": "github" + }, + { + "url": "https://github.com/phpstan", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/phpstan/phpstan", + "type": "tidelift" + } + ], + "time": "2023-10-06T14:19:14+00:00" + }, + { + "name": "rector/rector", + "version": "0.18.5", + "source": { + "type": "git", + "url": "https://github.com/rectorphp/rector.git", + "reference": "2a3b82f317e431fc142d21f3303891a4e64c96eb" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/rectorphp/rector/zipball/2a3b82f317e431fc142d21f3303891a4e64c96eb", + "reference": "2a3b82f317e431fc142d21f3303891a4e64c96eb", + "shasum": "" + }, + "require": { + "php": "^7.2|^8.0", + "phpstan/phpstan": "^1.10.35" + }, + "conflict": { + "rector/rector-doctrine": "*", + "rector/rector-downgrade-php": "*", + "rector/rector-phpunit": "*", + "rector/rector-symfony": "*" + }, + "bin": [ + "bin/rector" + ], + "type": "library", + "autoload": { + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "Instant Upgrade and Automated Refactoring of any PHP code", + "keywords": [ + "automation", + "dev", + "migration", + "refactoring" + ], + "support": { + "issues": "https://github.com/rectorphp/rector/issues", + "source": "https://github.com/rectorphp/rector/tree/0.18.5" + }, + "funding": [ + { + "url": "https://github.com/tomasvotruba", + "type": "github" + } + ], + "time": "2023-10-05T11:25:40+00:00" + } + ], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": [], + "platform-dev": [], + "plugin-api-version": "2.2.0" +} diff --git a/dev/tools/rector/readme.md b/dev/tools/rector/readme.md new file mode 100644 index 00000000000..2cbae19aca1 --- /dev/null +++ b/dev/tools/rector/readme.md @@ -0,0 +1,22 @@ +### Refactoring code with [rector](https://getrector.com) + + +#### Installation + +run in this folder + +```shell +composer install +``` + #### Usage + +##### To see change before apply +```shell +./vendor/bin/rector process --dry-run +``` + +##### To apply change + +```shell +./vendor/bin/rector process +``` diff --git a/dev/tools/rector/rector.php b/dev/tools/rector/rector.php new file mode 100644 index 00000000000..4ec907e208d --- /dev/null +++ b/dev/tools/rector/rector.php @@ -0,0 +1,30 @@ +phpVersion(PhpVersion::PHP_71); + $rectorConfig->paths([ + __DIR__ . '/../../../htdocs/', + __DIR__ . '/../../../scripts', + __DIR__ . '/../../../test', + ]); + $rectorConfig->skip([ + __DIR__ . '/../../../htdocs/includes/*', + __DIR__ . '/../../../htdocs/install/doctemplates/*' + ]); + $rectorConfig->parallel(240); + + + // register a single rule + $rectorConfig->rule(Dolibarr\Rector\Renaming\GlobalToFunction::class); + + // define sets of rules + // $rectorConfig->sets([ + // LevelSetList::UP_TO_PHP_71 + // ]); +}; diff --git a/dev/tools/rector/src/Renaming/GlobalToFunction.php b/dev/tools/rector/src/Renaming/GlobalToFunction.php new file mode 100644 index 00000000000..aac703b386d --- /dev/null +++ b/dev/tools/rector/src/Renaming/GlobalToFunction.php @@ -0,0 +1,76 @@ +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 + ); + } +}