Magento Coding Standard
With Magento Coding Standard rules you are now able to validate if you write code that is based on Magento coding standards. Writing standardized code makes it better readable, if you are working on modules you want to sale, you need to use this!
Magento Coding Standard
I bet you know bad source code. Especially in context of PHP you find many bad written code snippets. If you work with Magento, you may know, that it is not that easy to write spaghetti code, because you need to us Overloading, Overwriting, Code Injections and so on. Modern design patterns prevent you from writing code that sucks, but if you want, you can make it unreadable and complicated.
Magento once created coding standards. If you write your code based on them, you create readable modules and make the world a bit better. But how do you know about them? On GitHub you can find a ruleset to use with PHP CodeSniffer tool. It lists you all problems for analyzed code.
Install
It is quite easy to install this ruleset by composer. Just run the following command:
1 | composer require --dev magento/magento-coding-standard |
Usage
As easy as installing Magento Coding Standard is to use it. Again you may use your servers command line to run it. As param you need to set a module that needs to be analysed:
1 | vendor/bin/phpcs --standard=Magento2 app/code/MyExtension |
You need to change MyExtension to the name of your or any other installed extension. Feel free to analyze commercial modules and reports failures to its support.
Conclusion
Using Magento Coding Standard improves your code. It gets better readable and is more suitable in Magento 2 context. If you want to be a professional, this can help you to write professional modules for business usage.
How do you improve your written Magento 2 source code? Are you using CodeSniffer already?