Simple Moodle coding style checker

Simple Moodle coding style checker

by Tim Hunt -
Number of replies: 15
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

My colleage sam marshall made this for the OU development team, and I just took 5 minutes to throw it onto github so others could benefit.

https://github.com/timhunt/moodle-local_codechecker

At the moment the tool is very basic, but can easily be extended in future.

After you have installed this local plugin into Moodle in the usual way, you should see a new option Code checker under Site administration -> Development.

We hope you find this tool useful. Please feel free to enhance it.

This only works with Moodle 2.0.

(Note there is another coding style checker included in the download, but it is much harder to use, and the checks it does are not consistent with the coding guidelines because they are out-of-date. It is burried in lib/pear/PHP/runsniffer.)

Average of ratings: -
In reply to Tim Hunt

Re: Simple Moodle coding style checker

by Michael de Raadt -

Hi, Tim.

Thanks for sharing this. I installed and tested the module and it was really quite useful.

Can I suggest that you share this in M&P? I would certainly recommend this to anyone contributing code.

In my testing I found one minor issue: it expects a space after comments inside strings. I noted the regular expression used in checks for commas and I wasn't able to think of how to add such an exception.

Michael;

In reply to Michael de Raadt

Re: Simple Moodle coding style checker

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Oh yes. Modules and plugins database entry: http://moodle.org/mod/data/view.php?d=13&rid=4682. Thanks for the reminder.

We know the checks are not 100% accurate. You always have to treat the output with a pinch of salt and a modicum of common sense.

The goal was to build something useful as quicky as possible.

The other code-checker, based on the pear/PHP/codechecker library is much more sophisticated. Since that is built around a PHP parser, it does understand things like what is a string, what is a comment and what is code. One day it would be nice to combine the sopisticated checks from there with the nice interface and self-contined-plugin-ness of our report.

However, sam's goal was to create something the caught the most common problems as quickly as possible, and his plugin achieves that goal for now.

In reply to Tim Hunt

Re: Simple Moodle coding style checker

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Just a quick question, after running this on some of my code - should contributed modules contain the text 'This file is part of Moodle', or should they, instead, read something along the lines of 'This file is part of the XXXXX plugin for Moodle'?

In reply to Tim Hunt

Re: Simple Moodle coding style checker

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Since I did not have anything in particular to do on Saturday, there is now a new version https://github.com/timhunt/moodle-local_codechecker/tree/experiment that uses the PHP CodeSniffer library. That does much better checks.

In reply to Tim Hunt

Re: Simple Moodle coding style checker

by Anthony Borrow -
Picture of Core developers Picture of Plugin developers Picture of Testers

Thanks Tim - this will be a great tool for helping CONTRIB developers. I will make sure to use it when checking code for the first time. Peace - Anthony

In reply to Tim Hunt

Re: Simple Moodle coding style checker

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Just to note that several people had reported that this did not work on Windows. Jamie Pratt has just worked out what the problem is, and so there is a new version that now does work on windows.

(The problem/fix was https://github.com/timhunt/moodle-local_codechecker/commit/3f364b89235eae5ba5964e5567f14e6c5c856a9e)

In reply to Tim Hunt

Re: Simple Moodle coding style checker

by Joseph Rézeau -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators

Hi Tim,

that URL you gave throws a 404 error

Joseph

In reply to Tim Hunt

Re: Simple Moodle coding style checker

by Howard Miller -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
This is brilliant stuff...

Excepting that a block I wrote just threw up nearly 1,000 errors smile Sigh!!
In reply to Howard Miller

Re: Simple Moodle coding style checker

by Patrick Pollet -

Hi

Would have loved to test some of my contribs against it but I got a fatal error (see attachment).

 

I am under ubuntu Ubuntu 9.10 fully up to date and using  PHP 5.2.10-2ubuntu6.10 with Suhosin-Patch 0.9.7 (cli) (built: May  2 2011 23:13:14)

 

Cheers.

 

Edit : same error when running it at the command line :

xxxx@zzzz:/var/www/moodle20/local/codechecker$ sudo php run.php local/codechecker

Registering sniffs in moodle standard...

Fatal error: isDeprecated() cannot be called statically in /var/www/moodle20.git/moodle/local/codechecker/pear/PHP/CodeSniffer/Standards/Generic/Sniffs/PHP/DeprecatedFunctionsSniff.php on line 59

Attachment screenshot_014.png
In reply to Patrick Pollet

Re: Simple Moodle coding style checker

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

I have not seen that. Perhaps it does not work with PHP5.2?

Except that strangely, that is one of the standard checks. You could try editing local/codechecker/moodle/ruleset.xml, and removing the reference to that rule.

In reply to Tim Hunt

Re: Simple Moodle coding style checker

by Patrick Pollet -

> You could try editing local/codechecker/moodle/ruleset.xml, and removing the reference to that rule.

Yes it did fix it. See attached file for codechecker against itself . Not bad , 15 errors mostly stories about 'must be in lower-case letters'  in non Moodle code.cool

For my contrib code it is another story .  Got to go to work sad

Thanks

In reply to Patrick Pollet

Re: Simple Moodle coding style checker

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Yes. It is ironic that code-checker cannot pass its own tests wink (because we are subclassing, and overriding methods from a third-party library that does not follow Moodle's coding rules.)