Skip to content

Instantly share code, notes, and snippets.

@udovicic
Last active January 6, 2021 19:16
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save udovicic/acc0452666cc63204d91e2f21b1bc12f to your computer and use it in GitHub Desktop.
Save udovicic/acc0452666cc63204d91e2f21b1bc12f to your computer and use it in GitHub Desktop.
Instructions for setting up Magento 2 code analysis with PHPSTorm

Setting up MEQP Coding standard checks in PHPStorm

Overview

In order to submit extensions to Magento marketplace, source code needs to pass Magento Extension Quality Program Coding Standard checks.

You can either do it manually, by following this guide in official Github repository, or by fixing things as you code, by setting up check in PHP Storm.

Install and configure Code Sniffer on system

  • Install the phpcs to your system by executing:
# pear install PHP_CodeSniffer
$ git clone git@github.com:magento/marketplace-eqp.git 
  • Locate where phpcs sotres standards and copy them from the repo above. On Ubuntu based systems, they are located in /usr/share/php/PHP/CodeSniffer/Standards. You have to copy MEQP1, MEQP2 and Utils folders there:
# cp -R marketplace-eqp/MEQP1 /usr/share/php/PHP/CodeSniffer/Standards/
# cp -R marketplace-eqp/MEQP2 /usr/share/php/PHP/CodeSniffer/Standards/
# cp -R marketplace-eqp/Utils /usr/share/php/PHP/CodeSniffer/Standards/

PHPStorm setup

  • Make sure you have configured PHP interprter. You can do so by going to

Languages & Frameworks > PHP

  • Make sure you have confiugred PHP Code Sniffer. You can do so by going to

Languages & Frameworks > PHP > Code Sniffer

  • Enable code sniffer and choose validation standard:

Editor > Inspections > PHP

  • Find PHP Code Sniffer Validations and tick the box next to it, in order to turn it on

  • On the right pane, select MEQP1 for Magento 1 validation, or MEQP2 for Magento 2 validation

@Bhavik-kumar
Copy link

Same Steps I followed and getting below error.

Fatal error: Trait 'MEQP\Utils\Helper' not found in D:\Xampp\php\pear\PHP\CodeSniffer\Standards\MEQP2\Sniffs\Classes\CollectionDependencySniff.php on line 22

Call Stack:
    0.2038     345312   1. {main}() D:\Xampp\php\phpcs:0
    0.2070     505744   2. PHP_CodeSniffer\Runner->runPHPCS() D:\Xampp\php\phpcs:18
    0.2386     913720   3. PHP_CodeSniffer\Runner->init() D:\Xampp\php\pear\PHP\CodeSniffer\src\Runner.php:70
    0.2453    1175712   4. PHP_CodeSniffer\Ruleset->__construct() D:\Xampp\php\pear\PHP\CodeSniffer\src\Runner.php:294
    0.4383    1293520   5. PHP_CodeSniffer\Ruleset->registerSniffs() D:\Xampp\php\pear\PHP\CodeSniffer\src\Ruleset.php:215
    0.4383    1293568   6. PHP_CodeSniffer\Autoload::loadFile() D:\Xampp\php\pear\PHP\CodeSniffer\src\Ruleset.php:1103
    0.4387    1334864   7. include('D:\Xampp\php\pear\PHP\CodeSniffer\Standards\MEQP2\Sniffs\Classes\CollectionDependencySniff.php') D:\Xampp\php\pear\PHP\CodeSniffer\autoload.php:167

@WesleyVestjens
Copy link

WesleyVestjens commented Apr 20, 2018

Same error as Bhavik-kumar:

phpcs --standard=MEQP2 <mydir>
PHP Fatal error:  Trait 'MEQP\Utils\Helper' not found in /home/$USER/marketplace-eqp/MEQP1/Sniffs/Classes/ResourceModelSniff.php on line 21
PHP Stack trace:
PHP   1. {main}() /home/$USER/.composer/vendor/squizlabs/php_codesniffer/bin/phpcs:0
PHP   2. PHP_CodeSniffer\Runner->runPHPCS() /home/$USER/.composer/vendor/squizlabs/php_codesniffer/bin/phpcs:18
PHP   3. PHP_CodeSniffer\Runner->init() /home/$USER/.composer/vendor/squizlabs/php_codesniffer/src/Runner.php:70
PHP   4. PHP_CodeSniffer\Ruleset->__construct() /home/$USER/.composer/vendor/squizlabs/php_codesniffer/src/Runner.php:294
PHP   5. PHP_CodeSniffer\Ruleset->registerSniffs() /home/$USER/.composer/vendor/squizlabs/php_codesniffer/src/Ruleset.php:217
PHP   6. PHP_CodeSniffer\Autoload::loadFile() /home/$USER/.composer/vendor/squizlabs/php_codesniffer/src/Ruleset.php:1103
PHP   7. include() /home/$USER/.composer/vendor/squizlabs/php_codesniffer/autoload.php:167
PHP   8. spl_autoload_call() /home/$USER/marketplace-eqp/MEQP2/Sniffs/Classes/ResourceModelSniff.php:15
PHP   9. PHP_CodeSniffer\Autoload::load() /home/$USER/marketplace-eqp/MEQP2/Sniffs/Classes/ResourceModelSniff.php:15
PHP  10. PHP_CodeSniffer\Autoload::loadFile() /home/$USER/.composer/vendor/squizlabs/php_codesniffer/autoload.php:134
PHP  11. include() /home/$USER/.composer/vendor/squizlabs/php_codesniffer/autoload.php:167

Does anyone know/have the solution?

@michielgerritsen
Copy link

If you don't want to copy the MEQP rules to the code sniffer folder you can also tell CodeSniffer where your rules are located:
phpcs --config-set installed_paths ~/.magento-eqp

@michielgerritsen
Copy link

Another note: Apparently the latest PHPStorm uses the phpcs executable that it finds in the Magento project. So you must require the magento/marketplace-eqp package in you project to fix this error.

@lfluvisotto
Copy link

@michielgerritsen, good catch.

I added via composer require magento/marketplace-eqp --dev in the project

In Languages & Frameworks | PHP | Quality Tools | Code Sniffer, I set the path vendor/bin/phpcs and worked without errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment