Learn how to verify Magento 2 source code with an M2 coding sniffer with a few steps.
As you know, Magento 2 uses the code standard PSR-2 and all extensions must follow this standard. Today, I will show you how to check your code satisfies the Magento 2 coding standard or not.
Breathe life into your e-commerce experience with Magento marketplace!!!
Verify Magento 2 Source Code
ECG Magento Code Sniffer Coding Standard allows automatically check your code:
- Raw SQL queries
- SQL queries inside a loop
- Direct instantiation of Mage and Enterprise classes
- Unnecessary collection loading
- Excessive code complexity
- Use of dangerous functions
- Use of PHP superglobals
3 main steps that you need to follow to check with the Source Code:
- Step 1: Use composer to download the ECG code standard
- Step 2: Please wait until the source code is downloaded to your server
- Step 3: Please enter the command line to scan your code with the Magento 2 coding sniffer
Step 1:
Use composer to download the ECG code standard. Please go to your SSH and enter this command:
$ composer require magento–ecg/coding-standard
Step 2:
- Please wait until the source code is downloaded to your server. You will see the “vendor” which is created after the composer command is finished.
- Add the standards directory to PHP_CodeSniffer installed paths:
$ phpcs –config-set installed_paths ./vendor/magento-ecg/coding-standard
Step 3:
- Please enter the command line to scan your code with Magento 2 coding sniffer:
$ phpcs –standard=EcgM2 /path/to/code
- PHP CodeSniffer will automatically scan Magento PHP files. To check design templates, you can run:
$ phpcs –standard=EcgM2 /path/to/code –extensions argument: –extensions=php,phtml
Here is some error and warning which your code may have:
- WARNING| Usage of $this in template files is deprecated: you can use $block instead of $this in the template
- WARNING| Possible useless method overriding detected: You code override, but it is useless
- WARNING| Function’s cyclomatic complexity (15) exceeds 10: use too much “if” or “for” in one function
- ERROR| The use of function call_user_func_array() is forbidden
- WARNING| Model LSD method load() detected in the loop: load model in the loop. You should join a table instead of that.
And that is how you verify Magento 2 source code with an M2 coding sniffer.
I think that is all the process and the note to verify Magento 2 source code with M2 coding sniffer. This is a really helpful kind of product type, it helps you to check your code satisfies the Magento 2 coding standard or not. Therefore, learning to verify Magento 2 source code with an M2 coding sniffer and to manage its custom options one of the tasks for all Magento 2 store owners.
For more Magento tutorials, visit Webnexs Magento Knowledge Base.
Contact Webnexs to develop a Magento store in just a few days here today!
Leave a Reply