## General plugins (Local): Code-checker

local_codechecker
Maintained by Tim Hunt, Eloy Lafuente (stronk7)
A tool for developers that helps them follow Moodle's coding style.
14k
230
7
Moodle 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9

The code checker tool is based on the Zend PHP CodeSniffer library. It implements a set of 'sniffs' that check for many parts of the Moodle coding style. They also present a nice web interface for viewing the results.

### Contributors

Eloy Lafuente (stronk7)

• Thu, Jun 6, 2013, 1:13 AM

It's ironical that the codechecker should throw an error re its own code:

Did you remember to call setType() for 'exclude'? Defaulting to PARAM_RAW cleaning.

line 1303 of \lib\formslib.php: call to debugging()
line 281 of \lib\formslib.php: call to moodleform->detectMissingSetType()
line 202 of \lib\formslib.php: call to moodleform->_process_submission()
line 46 of \local\codechecker\index.php: call to moodleform->moodleform()
• Thu, Jun 6, 2013, 2:53 AM
haha, funny! thanks for the report, I'll roll a new minor version soon.
• Thu, Jun 6, 2013, 3:02 AM
Seems it was already fixed but the new release, planned for the end of April was never rolled. Doing...
• Tue, Jun 25, 2013, 9:26 PM
When using local_codechecker on our files I found some strange messages from it: some are just overlooked, other may be bugs.

error
························'backtracking_matcher'·=>·'backtracking_matcher'*/
46: Line indented incorrectly; expected at least 8 spaces, found 0
It's quite obvious, that there is more than 0 spaces on that line. There is some problem in spaces counting in lines with multiline comments.

warning
····public·function·__construct()·{
81: Useless method overriding detected
AFAIK PHP don't call parent constructor automatically, so overriding __construct() just to call parent ones isn't useless at all. This warning may mislead someone.

error
····················foreach·($ranges·as·&$tmp)
440: Inline control structures are not allowed
That's totally strange. & in foreach is a common method to change elements of array inside foreach loop. What's wrong with it and what "inline control structures" have to do with such code?

warning
····//·"What·is"·hint·is·obviously·response·based,·since·it·used·to·better·understand·mistake·message.
I suggest adding some punctuation marks, from which a sentence could start: double and single quotes, $in case comment start from variable names. I have also some problematic strings, when comment starts from function or class name, which should be lowercase. • Tue, Jun 25, 2013, 9:49 PM The indent problem seems to be getting confused by some commented out code, but you should not leave commented out code around. (It should be saved in git history. If a PHP class does not have a constructor at all, then the parent constructor is automatically used, so that warning is correct The foreach does not have a { after the ). It is nothing to do with the &. Comment starting with uppercase is just a pain. Blame Eloy • Tue, Jun 25, 2013, 10:06 PM Oleg: 1) yup, looks like commented code mix. More context lines would help to confirm it. 2) yup, any method only having a call to its parent is useless. Just allow inheritance to work, constructors included. 3) yup, curly brace is expected. 4) no, don't blame me. Blame code style guidelines, I'm not the inventor, just the implementor (in fact I think it was Tim the inventor). Easy to replace with: The "What is" hint... anyway. Have been attempts to open the hand a bit with this, but all them have been rejected. Ciao • Wed, Jul 10, 2013, 10:36 AM upon MDLSITE-2339 I changed the description to include words "code checker" • Wed, Jul 9, 2014, 6:49 AM with more and more jquery based scripts (yay!) I noticed that the codechecker marks minified jQuery plugins always a faulty, are there any plans to include compatibility with jQuery javascript style? • Wed, Jul 9, 2014, 5:01 PM Hi David, I suppose that, at fist shoot, the approach would be to exclude any jQuery js by default. Later, once we have the proper tools defined jslint/jhlint/js coding style... it shouldn't be complex to create a codechecker sniff wrapping whatever we want to test. But, as said, surely the 1st step is to exclude JS stuff, to avoid informing about something that is, right now, undecided. It would be great if you could create an issue in the tracker (related to this component) defining current behavior, with some real example, and also which are your expectations/desired result. Thanks! • Fri, Jul 18, 2014, 1:25 AM Gents, Using 2.3.1 - San Fermín release! and getting errors for legitimate code in a js file: value·=·value·<<·2; Expected 1 space after "<"; 0 found Expected 1 space before "<"; 0 found ..... value·|=·toggleflag; Expected 1 space after "|"; 0 found Expected 1 space before "="; 0 found ..... value·&=·~toggleflag; Expected 1 space after "&" operator; 0 found Expected 1 space before "="; 0 found Cheers, Gareth • Thu, Oct 16, 2014, 7:55 PM "More documentation..." under Useful Links gives a 404. • Wed, Jun 17, 2015, 5:00 AM When I ran code checker against my plugin, I am getting this error: The use of function print_r() is forbidden Why is that? I have look at some Moodle core code and it is using this function. What function should we use instead? • Wed, Jun 17, 2015, 5:50 AM Well, it is just a warning. However, print_r is generally only used in debug code. And debug code should generally be removed before everything is finished. That is why the warning exists. • Wed, Jun 17, 2015, 5:59 AM Ok thanks Tim. I will remove the use of print_r in my plugin. • Thu, Jul 30, 2015, 3:19 AM When I check Moodle 2.9 core code with the code checker I very often get a lot of errors. I notice many whitespace errors, »Inline comments must end in full-stops, exclamation marks, or question marks« errors and »Member variable "unique_end_html_token" must not contain underscores.« errors. Does the code checker need an updated to follow the actual Moodle coding style or is Moodle core code not validated against the Moodle coding style? I would like to be able to reliable use the code checker to validate coding style. The code checker is a great tool, especially when it is integrated into the IDE and errors/warnings are shown while working on the code. But when it throws may errors for core code it becomes confusing and irritating. Any idea how the issue can be solved? An example: lib/outputrenderers.php - 209 error(s) and 239 warning(s) Total: 209 error(s) and 239 warning(s) lib/outputrenderers.php #183: ········//·Remove·_renderable·suffixes Inline comments must end in full-stops, exclamation marks, or question marks #311: ········//·Remove·_renderable·suffixes Inline comments must end in full-stops, exclamation marks, or question marks #335: ········//·pass·to·core·renderer·if·method·not·found·here Inline comments must start with a capital letter, digit or 3-dots sequence Inline comments must end in full-stops, exclamation marks, or question marks #393: ····protected·$unique_end_html_token;
a
#398: ····protected·$unique_performance_info_token; Member variable "unique_performance_info_token" must not contain underscores. #403: ····protected·$unique_main_content_token;
Member variable "unique_main_content_token" must not contain underscores.
#437: ············//·legacy·xhtml·1.0
Inline comments must end in full-stops, exclamation marks, or question marks
#439: ············return·('html·PUBLIC·"-//W3C//DTD·XHTML·1.0·Strict//EN"·"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'·.·"\n");
Line exceeds 132 characters; contains 140 characters
#484: ········if·($this->metarefreshtag==''·&&·$this->page->periodicrefreshdelay!==null)·{
#485: ············$output·.=·'page->periodicrefreshdelay.';url='.$this->page->url->out().'"·/>';