Magento 2 code quality tools

If you are planning to publish an extension to Magento’s marketplace, it will have to pass a few quality gates before getting published. Magento did provide some tools to allow you testing locally before submitting the extension.

First, there is https://github.com/magento/magento-coding-standard . While this tool is mostly centered around code formatting, it can catch some nastier stuff too, like loading a model in a loop or using deprecated libraries. Assuming that you will be testing your extension in the context of a Magento website, you first need to add to your composer.json file, somewhere at root level:

"scripts": {
    "post-install-cmd": [
        "([ $COMPOSER_DEV_MODE -eq 0 ] || vendor/bin/phpcs --config-set installed_paths ../../magento/magento-coding-standard/)"
    ],
    "post-update-cmd": [
        "([ $COMPOSER_DEV_MODE -eq 0 ] || vendor/bin/phpcs --config-set installed_paths ../../magento/magento-coding-standard/)"
    ]
}

Then install the coding standards package:

composer require --dev magento/magento-coding-standard

Then run the checker:

vendor/bin/phpcs --standard=Magento2 app/code/My/Extension

This will provide an output like:

FOUND 0 ERRORS AND 16 WARNINGS AFFECTING 12 LINES
--------------------------------------------------------------------------------------------------------------------------------
  7 | WARNING | [x] Opening brace of a class must be on the line after the definition
 11 | WARNING | [x] The first parameter of a multi-line function declaration must be on the line after the opening bracket
 12 | WARNING | [x] Multi-line function declaration not indented correctly; expected 8 spaces but found 32
 13 | WARNING | [x] Multi-line function declaration not indented correctly; expected 8 spaces but found 32
 13 | WARNING | [x] The closing parenthesis of a multi-line function declaration must be on a new line
 14 | WARNING | [x] The closing parenthesis and the opening brace of a multi-line function declaration must be on the same line
 45 | WARNING | [ ] Expected "if (...) {\n"; found "if(...) {\n"
 45 | WARNING | [x] Expected 1 space(s) after IF keyword; 0 found
 45 | WARNING | [x] No space found after comma in function call
 47 | WARNING | [x] Expected 1 space after closing brace; newline found
 48 | WARNING | [ ] Expected "} else {\n"; found "}\n        else {\n"
 55 | WARNING | [ ] Line exceeds 120 characters; contains 125 characters
 56 | WARNING | [x] No space found after comma in function call
 56 | WARNING | [x] No space found after comma in function call
 59 | WARNING | [ ] Code must not contain multiple empty lines in a row; found 2 empty lines.
 64 | WARNING | [x] Expected 1 newline at end of file; 0 found
--------------------------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 12 MARKED SNIFF VIOLATIONS AUTOMATICALLY
--------------------------------------------------------------------------------------------------------------------------------

You can fix some of the issues automatically:

vendor/bin/phpcbf --standard=Magento2 app/code/My/Extension

Finally, Magento also offers https://github.com/magento/marketplace-tools . This is not a quality tool per se, just a little helper to check that the archive you are about to upload is correct. Use it like:

./validate_m2_package.php app/code/My/Extension/Extension.zip

While Magento 2 does more checks against your extension so it can still get rejected, the above should catch the obvious issues before starting the submission process. I also find them useful to quickly check the quality of a 3rd party extension.

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s