ElearningWorld.org

For the online learning world

Elearning WorldMoodleTechnical

Uniformity

Creating software, code, is an art form. As a consequence programmers have their own style, one that they develop over time. This style can be recognised and be different to other programmers. But when many programmers work on the same project then this can make reading and maintaining the code a bit difficult as you’re context switching between the styles.

Enter the coding standard or style. A set of guidelines that state how the code should be written and look. Ones that not only dictate but assist with preventing errors through improved readability. One such example is Moodle’s ‘Coding style’.

But how can the guidelines be enforced? Enter the ‘Code Sniffer’.

Disclaimers

Names / logos can be trademarks of their respective owners. Please review their website for details. I am independent from the organisations mentioned and am in no way writing for or endorsed by them. The information presented in this article is written according to my own understanding, there could be inaccuracies, so please do undertake your own research. The featured image is my copyright, please don’t use without my permission.

Code Sniffer

The PHP code in Moodle can be checked against the Coding style by the ‘PHP CodeSniffer’ (moodledev.io/general/development/tools/phpcs and phpqa.io/projects/phpcs.html) tool. This ‘sniffs’ the code looking for issues and produces a report:

FILE: /home/gareth/moodle/public/version.php
-----------------------------------------------------------------------------------------------------------------
FOUND 3 ERRORS AND 1 WARNING AFFECTING 4 LINES
-----------------------------------------------------------------------------------------------------------------
  2 | ERROR   | [x] Moodle boilerplate not found at first line
 33 | ERROR   | [x] Expected 1 space before comment text but found 9; use block comment if you need indentation
 34 | ERROR   | [x] Expected 1 space before comment text but found 11; use block comment if you need indentation
 35 | WARNING | [ ] Inline comments must end in full-stops, exclamation marks, or question marks
-----------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 3 MARKED SNIFF VIOLATIONS AUTOMATICALLY
-----------------------------------------------------------------------------------------------------------------

But what then? You now have a list of things to fix and that will take time. However, if the sniffer can detect them then there must be a way for a tool to fix them too? Indeed there is! The ‘PHP Code Beautifier and Fixer‘. This will do as much as it can leaving you with little or none, bar to commit the code.

Installing

After following ‘PHP CodeSniffer’ and ‘Setup and installation’, then we should have ‘phpcs’, ‘phpcbf’ and ‘grunt’ installed, though I did need to install ‘moodle-cs’ without ‘global’, being ‘composer require moodlehq/moodle-cs’ to have the ‘vendor’ folder that will contain ‘phpcs’ and ‘phpcbf’, i.e.

php vendor/bin/phpcs --version
PHP_CodeSniffer version 3.13.5 (stable) by Squiz and PHPCSStandards

So I’m not sure what I’m doing wrong.

In our Moodle folder run:

npx grunt ignorefiles

this will create the file ‘phpcs.xml’ file containing a list of files and folders for the checker to ignore. It also has the line:

<rule ref="./phpcs.xml.dist"/>

And contrary to ‘Configuration’ then if we add:

<rule ref="moodle-extra"/>

to ‘phpcs.xml.dist’:

<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="MoodleCore">
    <rule ref="moodle"/>
    <rule ref="moodle-extra"/>
    <config name="testVersion" value="8.2-"/>
</ruleset>

Then this is a different way of getting the extra standard to check.

Using

With Moodle 5.1, version 5.1.3+ (Build: 20260410) as out installation, then if we run:

php vendor/bin/phpcs -s public/lib/classes

then we can get the output:

FILE: /home/gareth/moodle/public/lib/classes/dml/table.php
-----------------------------------------------------------------------------------------------------------------------------------------------------------
FOUND 2 ERRORS AND 1 WARNING AFFECTING 3 LINES
-----------------------------------------------------------------------------------------------------------------------------------------------------------
  31 | WARNING | [ ] Unexpected MOODLE_INTERNAL check. No side effects or multiple artifacts detected.
     |         |     (moodle.Files.MoodleInternal.MoodleInternalNotNeeded)
  39 | ERROR   | [x] Opening brace must not be followed by a blank line (PSR12.Classes.OpeningBraceSpace.Found)
 105 | ERROR   | [x] Expected 1 space after FUNCTION keyword; 0 found (Squiz.Functions.MultiLineFunctionDeclaration.SpaceAfterFunction)
-----------------------------------------------------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY
-----------------------------------------------------------------------------------------------------------------------------------------------------------

And much more! Critically it indicates what sniffs can be fixed automatically. Doing so with the command:

php vendor/bin/phpcbf public/lib/classes

Note: No need for the ‘-s’ switch.

Which gives us a report of what it did:

PHPCBF RESULT SUMMARY
-------------------------------------------------------------------------------------------------------------------------
FILE                                                                                                     FIXED  REMAINING
-------------------------------------------------------------------------------------------------------------------------
/home/gareth/moodle/public/lib/classes/oauth2/client.php                                                 9      0
...
/home/gareth/moodle/public/lib/classes/dml/table.php                                                     2      1
-------------------------------------------------------------------------------------------------------------------------
A TOTAL OF 4876 ERRORS WERE FIXED IN 621 FILES
-------------------------------------------------------------------------------------------------------------------------

Now all we need to do is address anything the fixer could not fix.

What if?

What if there was a sniff that we disagreed with, that made no sense? One example is with the Adaptable theme ‘version.php’ file:

FILE: F:\moodledev\moodle51\moodle51\public\theme\adaptable\version.php
--------------------------------------------------------------------------------
FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE
--------------------------------------------------------------------------------
 38 | WARNING | This comment is 50% valid code; is this commented out code?
    |         | (Squiz.PHP.CommentedOutCode.Found)
--------------------------------------------------------------------------------

All we need to do is edit that file and add the ‘sniff’ as a comment (or added to the existing):

$plugin->requires = 2025100600.00; // 5.1 (Build: 20251006).  phpcs:ignore Squiz.PHP.CommentedOutCode.Found

and bingo! No more warning. More information on ‘Advanced Usage’.

Conclusion

I hope that you found this post useful.

Gareth Barnard
Latest posts by Gareth Barnard (see all)
blank

Gareth Barnard

Gareth is a developer of numerous Moodle Themes including Essential (the most popular Moodle Theme ever), Foundation, and other plugins such as course formats, including Collapsed Topics.

Add a reply or comment...