Skip to main content

PHP CS Fixer

What it does​

PHP CS Fixer is a tool that rewrites PHP code so that it follows one set of formatting rules. Alfa Commerce's rules are in the file .php-cs-fixer.php at the root of the repository. On a pull request the tool does not only report: it fixes the code and commits the fixes to your branch itself.

When it is used​

On every pull request into main or developer, as the job PHP CS Fixer of the workflow Code Quality. It does not run when main or developer receives commits, and it does not run for a pull request into another branch.

What you must provide​

Nothing. You may run it yourself before you push, see Running it yourself.

What it covers​

Files whose name ends in .php, in these folders:

CoveredNot covered
administrator/src/The component's tmpl/, layouts/, forms/ and services/ folders
site/src/script.php and the other files at the root
api/src/libraries/
plugins/ (whole, with each plugin's tmpl/ and services/)scripts/
modules/ (whole, with each module's tmpl/)Hidden files and version control folders

The rules​

Two rule sets, then single rules. Risky rules are allowed (setRiskyAllowed(true)).

Rule setWhat it is
@PSR12The PSR-12 coding style: four spaces, brace placement, spacing of keywords and signatures.
@PHP82MigrationRewrites older syntax to the forms of PHP 8.2.
GroupRuleSetting
Importsordered_importsSorted alphabetically
Importsno_unused_importsOn
Importsglobal_namespace_importClasses are imported. Constants and functions are not.
Spacingarray_indentationOn
Spacingbinary_operator_spacesOne space around operators
Spacingconcat_spaceOne space around the dot
Spacingnot_operator_with_successor_spaceOff
Spacingtype_declaration_spacesOn
Spacingmethod_argument_spaceArguments on several lines go one on each line
Clean codeno_extra_blank_linesFor extra lines, throw, use, and the three kinds of bracket blocks
Clean codeno_trailing_comma_in_singlelineOn
Clean codetrailing_comma_in_multilineIn arrays, arguments and parameters
Clean codeno_whitespace_before_comma_in_arrayOn
Clean codetrim_array_spacesOn
Clean codesingle_quoteSingle quotes where nothing is inserted into the string
Clean codecast_spacesOne space after a cast
Strictdeclare_strict_typesOff
Strictstrict_comparisonOff
Strictstrict_paramOff
Arraysarray_syntaxThe short form []
Return typesreturn_type_declarationNo space before the colon
Return typesnullable_type_declaration_for_default_null_valueOn
PHPDocphpdoc_alignLeft
PHPDocphpdoc_scalarOn
PHPDocphpdoc_single_line_var_spacingOn
PHPDocphpdoc_trimOn
PHPDocphpdoc_typesOn
PHPDocno_empty_phpdocOn
PHPDocno_superfluous_phpdoc_tagsOn. mixed is allowed and inheritdoc is kept.

Two things that surprise contributors:

  • The three strict rules are switched off on purpose. The configuration's comment gives the reason: Joomla compatibility. The tool will not add declare(strict_types=1) and will not turn == into ===.
  • no_superfluous_phpdoc_tags removes a @param or @return line that only repeats what the signature already says. A line that adds a description stays.

Running it yourself​

The workflow sets up PHP 8.2 and asks for the tool php-cs-fixer without naming a version, so the repository pins none. Install PHP CS Fixer the way its own documentation describes, then run it from the root of the repository. From the next promotion of developer to main (with 2.2.1, which is not released), the workflow sets up PHP 8.3; it still names no version of the tool.

The workflow's command, which changes the files and prints each change:

php-cs-fixer fix --config=.php-cs-fixer.php --allow-risky=yes --diff

The same command with the tool's dry run option added, to see what would change without changing anything:

php-cs-fixer fix --config=.php-cs-fixer.php --allow-risky=yes --diff --dry-run

The tool writes a cache file, .php-cs-fixer.cache. Git ignores it.

What happens next​

On a pull request the job does this, in order:

  1. It checks out the branch of your pull request.
  2. It runs the command above.
  3. If files changed, it commits them to your branch. The author is github-actions[bot] and the message is style: auto-fix code style with PHP CS Fixer.

You then have a commit on GitHub that you do not have on your computer. Pull before you commit again.

What can go wrong​

What you seeWhyWhat to do
Your push is refused because the branch on GitHub is aheadThe job committed its fixes.Pull, then push.
A PHPDoc line you wrote is goneIt only repeated the signature.Nothing, or add a description to the line so that it carries information.
The check is redA step broke: the tool could not run, or the commit could not be made. Formatting problems alone never turn it red.Open the run and read the failing step.
A local run changes other things than the job didYour version of the tool differs from the one the job installed that day.Compare the versions.

Not documented yet​

  • Whether the job can commit its fixes to a pull request that comes from a fork. The workflow file does not settle it. From the next promotion of developer to main, the job checks out the branch from the pull request's own repository, so a fork's branch is found; whether it can then push to that fork was not tried.
  • Which version of PHP CS Fixer a run installs. The workflow names none.