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
- Download the latest rector release and unzip it into the Sugar instance folder. ex:
/path/to/sugar/rector-0.15.20
- 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:
cache
vendor
portal2
upload
upgrades
*.log
rector.php
- Commit the initial repository state
git add . && git commit -am 'init'
- You might wanna keep your initial commit hash in case you need to reset
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:
cd /path/to/sugar && rector-0.15.20/bin/rector init
-
Agree to create a rector.php file with the dummy config. It should generate a template file similar to this:
<?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([
// LevelSetList::UP_TO_PHP_74
// ]);
};
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:
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/ Removing custom/modules/Forecasts/Ext/clients/ Removing custom/modules/pmse_Project/ sh-3.2$ git status On branch main nothing to commit, working tree clean