1

I am creating a custom extension for Magento 2 and looking for the tool that can help me in the code standards. I come to know about PHP code sniffers from google.

Is the PHP code sniffer is right tool for Magento coding standard? or We have some other tools?

3

For those, who come here from the google some instructions about how to install and use this great tool with PHPStorm:

  1. You should install PHP_Codesniffer. The simplest way is to require it using your composer (but you can read about another way on the code-sniffer page):

    composer global require "squizlabs/php_codesniffer:2.6.2"
    
  2. Then you should detect, where is phpcs. If you install it using composer global require you can detect absolute path using command:

    composer global config bin-dir --absolute
    

    As a result you can see the path from root, for me it looks like this: absolute path to composer bins so my phpcs accessible by path /Users/sergei/.composer/vendor/bin/phpcs and when I type /Users/sergei/.composer/vendor/bin/phpcs -h in the console I see the phpcs help message: phpcs help message part This message means that code-sniffer was installed successfully and can be used to make the code better.

  3. Install the Magento coding standards. All standards stored in the code-sniffer CodeSniffer/Standards directory. If you install it like in my example, it can be found in the global composer directory in vendor/squizlabs/php_codesniffer/CodeSniffer/Standards/ for me this path from root was /Users/sergei/.composer/vendor/squizlabs/php_codesniffer/CodeSniffer/Standards/. We should download MEQP1/MEQP2 manually and copy it to the standards directory:

    • Go to the any working directory, I prefer to use users home directory (cd ~)
    • Clone the Magento standards using git clone git@github.com:magento/marketplace-eqp.git (or download it manually using your web-browser)
    • Do checkout to the version 1.0.5 of the standards

      cd marketplace-eqp/
      git checkout 1.0.5
      
    • Copy the standards and Utils to the Squizlabs code-sniffer standards directory:

      cp -r marketplace-eqp/MEQP* marketplace-eqp/Utils /Users/sergei/.composer/vendor/squizlabs/php_codesniffer/CodeSniffer/Standards/
      
    • Now you are able to use the code-sniffer with Magento standards from the command line like this:

      /Users/sergei/.composer/vendor/bin/phpcs /Users/sergei/PhpstormProjects/aws-botapi/app/code/MageWorx/ShippingRules --standard=MEQP2
      

      where the path /Users/sergei/PhpstormProjects/aws-botapi/app/code/MageWorx/ShippingRules should be a path to the directory with your code.

    • Add code-sniffer to the PHPStorm to make access to it simplest. Open PHPStorm. Open the Preferences > Tools > External Tools:

      External Tools window in the PHPStorm

      and click on the +, then add next info:

      • Program - path to your phpcs, in my example it was /Users/sergei/.composer/vendor/bin/phpcs
      • Arguments - path to the clicked file\directory and desired standard: $FileDir$ --standard=MEQP1. Note that the $FileDir$ is a variable in the PHPStorm which means a currently selected file or directory.
      • Working directory - $ProjectFileDir$ varaible (by default)

      MEQP1 window

      Now it can be accessible from the External tools from context menu right in the PHPStorm:

      MEQP1 check in the context menu of the PHPStorm

      Just select desired directory or file, right-click on it and select External tools > MEQP1 and you can see a result of this test in the bottom of working space (the Run tab):

      MEQP1 result in the run tab

      You can add MEQP2 test the same way (using the + button or the clone button), just add another name, like MEQP2 and change the standard in the arguments input:

      MEQP2 tool

PS: we use the version of standards 1.0.5 and phpcs of version 2.6.2 because its compatible and works good, when version of the standards from the master does not work :(

  • @PankajSharma feel free to ask any question, if you need some clarification – Siarhey Uchukhlebau Feb 27 '18 at 8:44
0

Yes, php code sniffer is the right tool for magento 2 extension development but you have to use right standard.

Magento has integrated its own standards in php code sniffer called MEQP1 for M1 extensions and MEQP2 for m2 extensions.

You can download both from github - MEQP1 , MEQP2

After installing you can just add standard to test code with agrument

--standard="MEQP2"

You can read for about coding standards here - http://devdocs.magento.com/guides/v2.2/coding-standards/bk-coding-standards.html

0

What I normally use is a package my company developed. It will check your composer files (if applicable), PHPMD, PHPCS, PHPStan and ....

You can find the package here https://github.com/mediact/coding-standard. It's easy to install. Just require it in your extension (not your Magento project) and run it with ./vendor/bin/grumphp run. It will also automatically check all your changed code when you do a git commit on your extension.

If you have any questions regarding this package, feel free to ask.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.