/
Rector

Rector

New in ExamSys 7.7.0

Rector is a tool we are using to help us upgrade ExamSys for new versions of PHP. It makes automatic changes to the code based on the rules we have configures it to use.

For the PHP rules these will very often be converting code to use new language features introduced in that version of PHP.

Rector is installed using composer, it will be installed automatically then you initialise a Behat or PHPUnit site.

From the root directory of ExamSys you can run it without it making any changes using:

vendor/bin/rector process --dry-run

or making changes using:

vendor/bin/rector process

Rector is configured via the rector.php file in the root directory of ExamSys

Code we write must meet the standards that our Rector configuration has declared.

Using Rector to upgrade ExamSys for a new version of PHP

When using Rector to upgrade ExamSys for a new version of PHP our aim should be to step up versions with reasonably small sets of automatic changes.

General steps

  1. Update the Rector configuration

  2. Do a dry run and sense check what you see

  3. Modify the configuration until you have a set you are able to review

  4. Commit the configuration changes

  5. Do a Rector run that updates ExamSys code

  6. Code review the changes it makes yourself

  7. Run Behat, Code checker and PHPUnit

  8. Commit the changes if you are happy with them

  9. Make any manual modifications required to conform with the ExamSys coding style, or fix issues you noticed during your review or from the automatic tests.

  10. Do a dry run of Rector to verify that it will not make further changes

  11. Commit the changes

Configuration for PHP upgrades

In the configuration file there will be a call to ->withPhpSets() this should be modified so that we bump it at most by a single version of PHP at one time, for example withPhpSets(php81: true) to withPhpSets(php82: true).

If we find that there are large numbers of changes we can use the dry run to temporarily remove some rules from processing using the ->withSkip()method. You will be able to find the generalised name of rules when doing a dry run. We must document the reason we have any skipped rules in the configuration:

use Rector\Php71\Rector\FuncCall\RemoveExtraParametersRector; use Rector\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector; ... ->withSkip([ // Some function calls are incorrectly modified with this rule, // we should look for ways to correct this in the future. RemoveExtraParametersRector::class, // The following rules have been temporarily excluded to reduce the // size of the change we are reviewing. We will stop skipping them in // new commits soon: ClassPropertyAssignToConstructorPromotionRector::class, ])

We should skip rules if:

  • They break our code

  • As a way to make the number of automatic changes into an easier to review number. When we do this we would expect further changes to remove those rules

Structure of a Rector change

When making changes via Rector we require that there are either two or three commits. The commits must be:

  1. The Rector configuration change

  2. The changes made by Rector (you must not include any manual modifications in this commit)

  3. Any manual changes you need to make to fix any code style or other issues created by the automatic changes

This structure is required so that we can adequately review what has happened during the change.

More information

Related content

Development tools in ExamSys
Development tools in ExamSys
More like this
Updating the ExamSys version number
Updating the ExamSys version number
More like this
4. Updating the Application
4. Updating the Application
More like this
8. Common Installation Errors
8. Common Installation Errors
More like this
1. Installing the Application
1. Installing the Application
More like this
9. Command Line Install/Update
9. Command Line Install/Update
More like this