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 Get and Set Config Value programmatically in Magento 2August 13, 2019
How to send email in Magento 2 programmaticallyMay 1, 2019
How To Change Website Logo In Magento 2October 18, 2018
How To Check If Customer Is Logged Into Magento 2February 25, 2020
How to create and expose extension attributes to REST APIs in Magento 2February 21, 2020
The 3 Magento Ultimate Email Marketing You Need!February 17, 2020
Do you know: The 7 hottest 2020 eCommerce trendsFebruary 7, 2020
OpenCart To Magento Switch - A Comprehensive GuidelineMarch 30, 2020
How To Migrate From Shopify To Magento 2 From A To ZMarch 26, 2020
Stop Misunderstanding Affiliate Marketing!March 23, 2020
Conversion Rate Optimization 101: How To Get What You Want?March 18, 2020