SugarClub
SugarClub
  • User
  • Site
  • Search
  • User
  • Groups & Discussions
    Groups & Discussions
    • Product Forums
      Product-focused Q&A, discussions, best practices, fixes, and help
      Product Forums
      • Sugar Market
      • Sugar Sell & Enterprise
      • Sugar Serve
      • sales-i
    • User Groups
      Professional, Industry, Language
    • Get Involved
      Learn how to become a Raving Fan
    • Social Club
      Live, interactive, virtual meetups with other Sugar customers and Sugar’s Subject Matter experts!
    • Leadership Lounge
      Network with fellow organizational leaders, ask questions, and share insights
    • Developers
      Visit DevClub, the SugarClub group for Sugar Developers
      Developers
      • DevClub
      • Mobile Developers
      • Developer Builds
        Supplemental access level required. Inquiries: developers@sugarcrm.com
    • Additional Groups (Access Required)
      Groups that require special access will be displayed here. Contact sugarclub@sugarcrm.com for assistance. Click here to see all groups
      Additional Groups (Access Required)
      • SugarCloud Platform
  • Product Information
    Product Information
    • Release Central
      Find release-specific content to prepare for your next Sugar update
    • Documentation & Resources
      Looking to expand your Sugar knowledge? Explore our in-depth documentation and other helpful resources!
    • Product Update Blogs
      Updates about each Sugar product
    • Customer Stories »
      Case Studies by SugarCRM
  • Training & Certification
    Training & Certification
    • Training & Certification Home
      Live & On-Demand classes, Quick Videos, Sugar Certifications, and more!
    • Quick Videos
      Short videos about using Sugar
    • My SugarU Dashboard »
    • SugarU News & Updates
  • Adoption
    Adoption
    • Grow Adoption Framework
      Get started on your adoption journey and review the adoption resources from SugarCRM
  • Calendar
  • News
    News
    • Sugar News
    • SugarCRM.com News »
    • Dev Blog
    • SugarCRM Marketplace Blog
  • Help
    Help
    • Welcome to Sugar!
      New to Sugar? Get started here!
    • SugarClub Help & Instructions
      Learn more about SugarClub and find answers to questions about this site
    • New to SugarClub?
      Start your community journey here
    • Technical Support
      Sugar's support resources
      Technical Support
      • Case Portal »
        Access the SugarCRM Case Portal
      • Working with Sugar Support »
        Find out more about engaging with the SugarCRM Support team
      • SugarCloud Information
        Find information about SugarCloud service updates and site status. Contact sugarclub@sugarcrm.com to request access
  • More from Sugar
    More from Sugar
    • DevClub
    • PartnerClub
    • Support
    • SugarOutfitters Marketplace
    • sugarcrm.com
  • DevClub
  • PartnerClub
  • Support
  • Marketplace
  • sugarcrm.com
DevClub
DevClub
Dev Tutorials Rector Basic Setup
Click here to join this group and curate your SugarClub experience.
  • +On-Boarding Framework
  • +Customization Guides
  • +Modern UI Technical Guide
  • -Automated PHP Compatibility Tool
    • Rector Basic Setup
    • Prepare Rector for MLP/Addon
    • Prepare Rector for Customizations
    • Executing Rector
    • Keeping Compatibility
    • Troubleshooting Rector
    • PHP 7.4 Warnings to PHP8.2 Errors
  • Did you know? Copying related records is a breeze!
  • How to write code for SugarCloud webinar Q&A
  • HOW TO: enforce ACL on Tags
  • Programatically manage Dropdown List
  • Remove custom fields created via package installation
  • Sugar Developer Tools
  • Tutorial:  How to register custom platforms in Sugar instances via Platform extension
  • Adding a google reCAPTCHA in a Web-to-Lead form
  • Sugar Developer Blog Style Guide

Rector Basic Setup

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 Sugar >= 25.1
    • You must use Rector that is shipped with Sugar and located at vendor lib in your Sugar Instance folder. ex: /path/to/sugar/vendor/rector/rector/bin/rector
      • You might need to give +x permissions to execute rector.
        • chmod +x /path/to/sugar/vendor/rector/rector/bin/rector
      • You may still download newer versions of Rector from its release repository, however, you'd need to follow their installation instructions
  • If you are using Sugar < 25
    • 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.
  • 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
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
cache
vendor
portal2
upload
upgrades
*.log
rector.php

  • Commit the initial repository state

Fullscreen
1
git add . && git commit -am 'init'
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
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
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
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 rector 0.15.x or less, you can use the following:

Fullscreen
1
cd /path/to/sugar && rector-0.15.20/bin/rector init
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
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 && /path/to/rector/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!
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
cd /path/to/sugar && /path/to/rector/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([
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
<?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:

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/
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
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

You can move on to the Prepare Phase:

  • Prepare Rector for MLP/Addon
  • Prepare Rector for Customizations
  • 0 comments
  • 0 members are here
  • Sign in to reply
Related
Recommended