Refactoring your code required a very basic setup to make your life easier to track what Rector does to your code (if you use its auto-fix mode).
Basic setup means installing software you will need such as Rector and Git. Git will help us to keep track of changes in your Sugar instance folder after we initialize it.
Keep that info in mind if you are planing to use a pre-existing instance for this work, it will add a lot of extra files to it.
Step by Step
- Download a build of SugarCRM. Please use the one that is compatible with your MPL.
- Install the build locally or use your own Sugar instance if you have one local
- If you are using PHP >= 8.3
- Download the this rector release and unzip it into the Sugar instance folder. ex:
/path/to/sugar/rector-1.2.3
- Rector team might add or remove classes that we utilize on our scans
- Latest we are using is
1.2.3
for PHP 8.3, later than that might not work.
- Download the this rector release and unzip it into the Sugar instance folder. ex:
- If you are using PHP < 8.3
- Download the this rector release and unzip it into the Sugar instance folder. ex:
/path/to/sugar/rector-0.18.1
- Rector team might add or remove classes that we utilize on our scans
- Latest we are using is 0.18.1, later than that might not work.
- Download the this rector release and unzip it into the Sugar instance folder. ex:
- Initialize a git repository inside the instance folder: cd /path/to/sugar && git init. It’s just convenient to use git to track the list of files and some other things.
- Create /path/to/sugar/.gitignore file to skip the unneeded changes with the following contents:
Fullscreen
1
2
3
4
5
6
7
cache
vendor
portal2
upload
upgrades
*.log
rector.php
- Commit the initial repository state
Fullscreen
1
git add . && git commit -am 'init'
- You might wanna keep your initial commit hash in case you need to reset
Fullscreen
1
2
3
4
sh-3.2$ git log
commit 0ac2bd9c175f7bff9842dcd8179fc9ec67b7002f (HEAD -> main)
Author: Rafael Fernandes <rafael.fernandes@sugarcrm.com>
Date: Mon Apr 17 10:40:56 2023 -0400
Initialize Rector config (rector.php)
- If you are using 0.15.x or less, you can use the following:
Fullscreen
1
cd /path/to/sugar && rector-0.15.20/bin/rector init
- If you are using 0.16.x or higher, you can use:
Fullscreen
1
2
3
4
5
6
7
cd /path/to/sugar && rector-0.18.1/bin/rector
No "rector.php" config found. Should we generate it for you? [yes]:
> yes
[OK] The config is added now. Re-run command to make Rector do the work!
-
Agree to create a rector.php file with the dummy config. It should generate a template file similar to this:
Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
declare(strict_types=1);
use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector;
use Rector\Config\RectorConfig;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Set\ValueObject\SetList;
return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([
__DIR__ . '/Ext',
//... more subdirs here
__DIR__ . '/upgrades',
]);
// register a single rule
$rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class);
// define sets of rules
// $rectorConfig->sets([
Reset your Repo to start over
- You can always start over at your initial Git commit and undo all changes by Rector or yourself after that point.
- Take the initial commit hash from git log and reset as follows:
Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
sh-3.2$ git log
commit 0ac2bd9c175f7bff9842dcd8179fc9ec67b7002f (HEAD -> main)
Author: Rafael Fernandes <rafael.fernandes@sugarcrm.com>
Date: Mon Apr 17 10:40:56 2023 -0400
sh-3.2$ git status
On branch main
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: vCard.php
no changes added to commit (use "git add" and/or "git commit -a")
sh-3.2$ git reset --hard 0ac2bd9c175f7bff9842dcd8179fc9ec67b7002f
HEAD is now at 0ac2bd9c init
sh-3.2$ git clean -fd
Removing custom/Extension/application/Ext/Include/BuildingBlock_HelloWorldDashlet.php
Removing custom/Extension/application/Ext/Include/orderMapping.php
Removing custom/modules/ActivityStream/