Code Sniffer: Cleaning your coding with ease
A good program doesn't have only good logic and structure, but nice and readable codes as well. Of course, you can refactor the look of your codes manually, but there are tools to take care of that for you, like Code Sniffer.
First, let us help you with the installation of Code Sniffer.
There are many ways to install Code Sniffer, but the most preferable way, that we recommend, is using PEAR.
Open your terminal then run these commands in order to install PEAR
sudo apt-get update
sudo apt-get install php-pear
Then install Code Sniffer with this command
pear install PHP_CodeSniffer
All done! Now you have Code Sniffer available system-wide.
There are many coding standards for PHP such as PSR1, PSR2, … and some of the most common standards have been installed by default.
Go to /usr/share/php/PHP/CodeSniffer/Standards if you want to have a look at these installed standards.
To check for code standard flaws, for example, key in the following command line
phpcs -n --standard=PSR2 --extensions="php" /path/to/code
This will check for codes with severity of warning to 0 (-n), standard of PSR2 and files with .php extension. The command will produce something like this
There are errors that can be fixed automatically using code beautifier or phpbcs in Code Sniffer. For example:
phpcs --standard=EcgM2 -s --report=summary --warning-severity=3 /path/to/code
If you want to uncover advanced Code sniffer usage, visit the following link for the full guide:
How to add custom fields in Product Edit pages in Magento 2April 19, 2019
How to Get and Set Config Value programmatically in Magento 2August 13, 2019
How to write and use Events and Observers in Magento 2?June 13, 2019
7 Best Magento 2 Instagram Extensions You Cannot Miss!October 21, 2019
How to add custom command line in Magento 2October 7, 2019
Order Confirmation Email Template: Secret weapon to engage customersSeptember 17, 2019
How to Create Custom REST API in Magento 2 (Updated 2019)September 14, 2019
Instagram Shopping Feature: The New Future of eCommerceSeptember 9, 2019
Magento Black Friday 2019: Huge deals for M2 extensionsNovember 20, 2019
How to add columns to customer grid in Magento 2November 18, 2019
Your 7 Best Magento 2 Banner Slider Extension!November 12, 2019
How to add a custom column to product grid in Magento 2November 7, 2019
Do you ACTUALLY need eCommerce Loyalty Program?November 7, 2019