How to Verify Magento 2 Source Code with M2 Coding Sniffer?

Verify Magento 2 Source Code

As you know, Magento 2 uses the code standard PSR-2 and all extension must follow this standard. Today, I will show you how to check your code satisfies the Magento 2 coding standard or not.

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 Magento2 Source Code:

  1. Step 1: Use composer to download the ECG code standard
  2. Step 2: Please wait the source code is downloaded to your server
  3. Step 3: Please enter the command line to scan your code with Magento2 coding sniffer

Step 1:

Use composer to download the ECG code standard. Please go to your SSH and enter this command:

$ composer require magentoecg/coding-standard

Step 2:

  • Please wait 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 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 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 types, it helps you to check your code satisfies the Magento 2 coding standard or not. Therefore, learning to verify Magento 2 source code with M2 coding sniffer and to manage its custom options one of the tasks for all Magento 2 store owners.

For more Magento tutorial, visit Webnexs Magento Knowledge Base.

Last Update: October 2, 2018  

October 2, 2018   97   Praveen Raja    Magento 2 Tutorial    
Total 0 Votes:
0

Tell us how can we improve this post?

+ = Verify Human or Spambot ?

Leave a Reply

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

Facebook
Twitter
INSTAGRAM
LinkedIn