Magento Code Quality Standards

To maintain consistent, high-quality code in every file and project, we introduce standards and tools for checking whether the code meets the desired quality. Projects that use standards are easier to maintain, and new developers can quickly start creating the same code quality as others. Standards and quality checker tools improve code review and help develop things faster with the same style and quality.

PHP Code Sniffer – Magneto Coding Standard

When testing the Magneto codebase we need to check various files from PHP and JS where we put the main business logic to XML and JSON where we put configs and additional data. Thankfully Adobe maintains a standard for every Magento file. You can check it out at https://github.com/magento/magento-coding-standard

This standard contains a ruleset for PHP CodeSniffer. It tests all files, not only PHP-related. It includes PSR1, and PSR2 standards and adds custom ones on top of it.

You can use it by providing a param standard equal to Magento2:

./vendor/bin/phpcs --standard=Magento2 app/code

To maintain excellent code quality you should take care of all errors including warnings.

Strict Magento Coding Standard

If you are looking for an even stricter coding standard for Magneto than the official one. Check out the MRM Commerce’s ruleset: https://github.com/mrm-commerce/magento-coding-standard-phpcs

It contains:

  • The official Magneto 2 ruleset
  • PSR-12 ruleset which expanded and replaced the PSR-2 coding style guide
  • PHP Compatibility Coding Standard – to check incompatibilities with projects PHP version
  • Security Audit Coding Standard – to check security issues
  • Slevomat Coding Standard – that includes advanced checks to make code beautiful and consistent

It’s a great set of rules to make your code future-proof. I suggest using it whenever possible.

Coding Standards for Hyvä

Hyvä also has a ruleset dedicated to developing Hyvä based themes, it contains an official Magento 2 ruleset with some adding and modifications to handle Hyvä approach.

https://github.com/hyva-themes/hyva-coding-standard

When working with Alpine.js and Tailwind CSS you can just use Prettier with Tailwind CSS plugin (https://github.com/tailwindlabs/prettier-plugin-tailwindcss) that will format your code to make it look nice and consistent across all files.

PHP Mess Detector

This is another great and must-have tool. It checks code in various ways to help maintain clean and well-designed solutions. It has a few rulesets with keen on checking as follows:

  • Clean Code – enforce SOLID and maintainable code
  • Code Size – finds complex code blocks and helps refactor them
  • Controversial Rules – strict rules about using things considered as bad practices
  • Design – detects poor design choices that violate object-oriented programming
  • Naming – ensure consistent and meaningful naming conventions
  • Unused Code – helps to detect dead code

https://phpmd.org

Other useful tools

PHP CodeSniffer comes with a handy tool PHP Code Beautifier and Fixer which can automatically fix some issues reported by CodeSniffer. There are sometimes some problems with automatically fixed code – it breaks, this problem is mostly solved by the Slevomat Coding Standard but if you using it, you still need to just double-check if the code is ok.

https://github.com/squizlabs/PHP_CodeSniffer

PHP Metrics – a great tool to analyze new projects or make a reflection on existing ones, it generates a full HTML report of the project codebase. It measures maintainability, coupling, and cohesion for every class. It also looks for security and performance vulnerabilities. It is a great tool for visualizing a project’s debt.

https://phpmetrics.github.io/website

Composer Unused – a smart tool to check your composer dependencies. By analyzing the project’s codebase, it helps find dependencies that are not used anymore.

https://github.com/composer-unused/composer-unused

Copy/Paste Detector – this tool is no longer maintained but it still works as a charm. It does what it says – check if the codebase has fragments that look duplicated. There are some issues with it – it detects Magento boilerplate as a duplicate code, oh well.

https://github.com/sebastianbergmann/phpcpd

PHPLOC – a nice tool to check the size of the project, mostly for statistic nerds.

https://github.com/sebastianbergmann/phploc

PHPCB – a simple code benchmark that lets you test some fragments of code to check how they perform, it’s a great tool when you working on some algorithm and you want to compare which version is the fastest.

https://github.com/smuuf/phpcb

The PHP and Magento community created many more tools and standards, so it’s worth exploring them. There are also a lot of commercial products with the aim of improving code quality.

How to work with them?

I recommend using the strictest rules as possible in new projects. Starting with them will help you maintain good code quality without headaches. If you want to include them in the existing project it may be more problematic. Thankfully these tools can be configured to be less strict if needed. You don’t need to fix the whole codebase at once, you can just test your current changes and improve code file by file.

Continuous Integration

Code quality should be checked after every change, it should be a part of the CI pipeline. You can also add git hooks to use code quality checkers before committing. For the automated process of code quality checks, I recommend adding at least PHP CodeSniffer and PHP Mess Detector. Other tools can be used manually from time to time, but I still recommend them to consider for automatic process, it should not add much more time to whole CI execution. Of course, for tools like PHP Metrics, it doesn’t make sense to add it as a part of the automatic check after every commit but you can make a nightly build on the main branch and host the result so everyone in the team can take a look when we stand.

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.