6

I create magento 2 extenstion. I need to follow standard coding. Which steps need to follow for standard Magento 2 Extension development.

1

3 Answers 3

9
1   must have composer.json in root folder of extension(Eg. app/code/Vender/Module/composer.json)   
2   must have registration.php in root folder of extension (Eg. app/code/Vender/Module/registration.php)
3   Do not use $_REQUEST, $_POST directly
4   Never end class file with ending php tag ?>
5   Remove unnecessory code and comments
6   Use spaces for indentation
7   check with phpcs codind standard and remove all errors, Refer Coding standard tab
8   Must pass severity=10 while chicking coding standard with MEQP2, Refer coding standard
9   do compilation and resolve all errors
10  validate_m2_package_v2

Nore : 
Vendorname : abcde
Type : magento2-module
(Note :"If you provided sequence tag in module.xml then don't forget to add those modules in require tag)
Name: <vendor-name>/<package-name>

sample file : composer.json

{
    "name": "abcde/modulename",
    "description": "N/A",
    "require": {
        "php": "~5.5.0|~5.6.0|~7.0.0",
        "magento/module-catalog": "100.1.*"
    },
    "type": "magento2-module",
    "version": "1.0.0",
    "license": [
          "OSL-3.0",
          "AFL-3.0"
         ],
    "authors": [
          {
           "name": "abc",
           "email": "[email protected]",
           "homepage": "websitelink",
           "role": "Developer"
          }
    ], 
    "autoload": {
        "files": [
            "registration.php"
        ],
        "psr-4": {
            "Vender\\Module\\": ""
        }
    }
}
-------------------------
How to Install Coding Standard

cd to your magento install directory

run below

composer create-project --repository=https://repo.magento.com magento/marketplace-eqp magento-coding-standard

composer require magento/marketplace-eqp

cd magento-coding-standard

If you ger error: PHP Fatal error:  Uncaught PHP_CodeSniffer_Exception: Referenced sniff "MEQP1.Exceptions.Namespace" does not exist
vendor/bin/phpcs --config-set installed_paths ../../..


configure
vendor/bin/phpcs --config-set m2-path <path-to-magento2>
eg
sudo vendor/bin/phpcs --config-set m2-path  /media/webdata/var/www-data/html/ext_dev/m2/giftcard/mv211

../m2/magento-coding-standard# sudo vendor/bin/phpcs .../fullpath/m2/app/code/Vendor/Module/view/  --standard=MEQP2 --extensions=phtml,xml,css,js,php
(fine error, worning)

../m2/magento-coding-standard# sudo vendor/bin/phpcbf .../fullpath/m2/app/code/Vendor/Module/view/ --standard=MEQP2 --no-patch --extensions=php,phtml,xml,css,js
(remove line space)


Marketplace Technical Review

$ vendor/bin/phpcs /.../fullpath/m2/app/code/Vendor/Module  --standard=MEQP2 --severity=10

https://gist.github.com/alankent/fcf280dd9c599921b71d#file-validate_m2_package_v2-php  (download file)

php validate_m2_package.php my-theme.zip my-module.zip

// @codingStandardsIgnoreFile
/* @noEscape */
5

For a Magento 2 extension, you should be following the Magento EQP (Extension Quality Program) code standards. Magento provides a tool to validate the PSR-2 code standards and most of Magento's added standards on top of that.

https://github.com/magento/marketplace-eqp

They provide this as a PHP_CodeSniffer ruleset. You can either run it manually against your files (with their directions), or you can set it up as the CodeSniffer ruleset in PhpStorm if you use it.

Documentation on Magento 2's code standards as a whole is available here: http://devdocs.magento.com/guides/v2.0/coding-standards/bk-coding-standards.html


Setting up MEQP Coding standard checks in PHPStorm

By udovicic, source https://gist.github.com/udovicic/acc0452666cc63204d91e2f21b1bc12f

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 the repo with official standards:

    git clone [email protected]: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

1
  • 3
    Instead of copying the standards, you can also use vendor/bin/phpcs --config-set installed_paths vendor/magento/marketplace-eqp Commented Feb 22, 2018 at 10:19
4

My experience. First i have upgraded built-in phpcs default magento to 2.9.0 via composer.json

composer.json

"squizlabs/php_codesniffer": "2.9.0",


Get MEQP ruleset

composer require magento/marketplace-eqp

After install successfully. Copy all folder in vendor/magento/marketplace-eqp AbstractSniffs MEQP1 MEQP2 Utils to vendor/squizlabs/php_codesniffer/CodeSniffer/Standards

run command in root dir magento

vendor/bin/phpcs --version // Make sure used version in composer.json
vendor/bin/phpcs --config-set installed-paths yourAbsolutePathTo/squizlabs/php_codesniffer/CodeSniffer/Standards
vendor/bin/phpcs /fullpathtomagento2_root/app/code/Vendor/Module/ --standard=MEQP2 --severity=10 --extensions=phtml,php

In here you can setup configure in PHPSTORM if you need
You are done!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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