mirror of
https://github.com/Dolibarr/dolibarr.git
synced 2025-12-05 09:08:09 +01:00
Add Rector (automatic refactoring)
Add first rule: access global to function (getDolGlobalInt, getDolGlobalString)
This commit is contained in:
1
dev/.gitignore
vendored
1
dev/.gitignore
vendored
@@ -1 +1,2 @@
|
|||||||
/spec
|
/spec
|
||||||
|
/tools/rector/vendor/
|
||||||
|
|||||||
19
dev/tools/rector/composer.json
Normal file
19
dev/tools/rector/composer.json
Normal file
@@ -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"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
137
dev/tools/rector/composer.lock
generated
Normal file
137
dev/tools/rector/composer.lock
generated
Normal file
@@ -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"
|
||||||
|
}
|
||||||
22
dev/tools/rector/readme.md
Normal file
22
dev/tools/rector/readme.md
Normal file
@@ -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
|
||||||
|
```
|
||||||
30
dev/tools/rector/rector.php
Normal file
30
dev/tools/rector/rector.php
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
use Rector\Config\RectorConfig;
|
||||||
|
use Rector\Core\ValueObject\PhpVersion;
|
||||||
|
use Rector\Set\ValueObject\LevelSetList;
|
||||||
|
|
||||||
|
return static function (RectorConfig $rectorConfig): void {
|
||||||
|
$rectorConfig->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
|
||||||
|
// ]);
|
||||||
|
};
|
||||||
76
dev/tools/rector/src/Renaming/GlobalToFunction.php
Normal file
76
dev/tools/rector/src/Renaming/GlobalToFunction.php
Normal 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
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user