GithubHelp home page GithubHelp logo

alexkarand / coding-standard Goto Github PK

View Code? Open in Web Editor NEW

This project forked from magento-ecg/coding-standard

0.0 0.0 0.0 147 KB

Magento PHP_CodeSniffer Coding Standard

License: MIT License

PHP 96.17% HTML 2.79% Shell 1.04%

coding-standard's Introduction

ECG Magento Code Sniffer Coding Standard

SensioLabsInsight Medal

ECG Magento Code Sniffer Coding Standard is a set of rules and sniffs for PHP_CodeSniffer tool.

It allows automatically check your code against some of the common Magento and PHP coding issues, like:

  • 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;

and many others.

Both Magento and Magento 2 are supported.

Installation

Before starting using our coding standard install PHP_CodeSniffer.

The recommended installation method for PHPCS is globally with Composer:

composer global require "squizlabs/php_codesniffer=*"

Make sure Composer's bin directory (defaulted to ~/.composer/vendor/bin/) is in your PATH.

Clone or download this repo somewhere on your computer or install it with Composer:

composer require magento-ecg/coding-standard

Note: PHP_CodeSniffer 3.x is now required to run our coding standard. To install PHP_CodeSniffer 2.x compatible version:

composer require magento-ecg/coding-standard:4.*

Add the ECG standards directory to PHP_CodeSniffer's installed paths:

$ ./vendor/bin/phpcs --config-set installed_paths /path/to/your/folder/vendor/magento-ecg/coding-standard

You can check Installation by running following command:

$ ./vendor/bin/phpcs -i

It should do the following output:

The installed coding standards are EcgM2, PEAR, Zend, PSR2, MySource, Squiz, PSR1, PSR12 and Magento2

Usage CLI Tool

Select a standard to run with CodeSniffer: EcgM2 for Magento 2

Run CodeSniffer:

$ ./vendor/bin/phpcs --standard=EcgM2,PSR2,Magento2 /path/to/code

This package is compatible with Composer Installer Plugins for PHPCS coding standards (such as https://github.com/Dealerdirect/phpcodesniffer-composer-installer) and can be automatically registered with PHPCS during installation.

To check design templates, you must specify phtml in the --extensions argument: --extensions=php,phtml

Usage Hook pre-commit

Get hook file from the existing repository.

$ /path/to/your/folder/vendor/magento-ecg/coding-standard/.githooks

Install Hooks on the project. Copy hook into the project GIT folder

cp -R ./vendor/magento-ecg/coding-standard/.githooks ./

To automate hooks installation for all developers, it needs to include the following lines into your project's composer.json

    "scripts": {
        "post-install-cmd": [
            "git config core.hooksPath .githooks"
        ],
        "post-update-cmd": [
            "git config core.hooksPath .githooks"
        ]
    }

Run composer install to refresh the project state.

composer install

You are done! Now, each product commit will be validated for an alignment with Magento Coding Standards.

Usage PHPStorm

PhpStorm provides code style check through integration with the PHP Code Sniffer tool, which validates your code for consistency with a coding standard and best practices.

Once Tool installed and configured in PhpStorm, the tool is available in any opened PHP file. Errors and warnings reported by PHP Code Sniffer on-the-fly are displayed as popup messages.

Install coding standard for development See Installation steps.

Configure PhpStorm

  • Open Preference: "Settings > Preferences > Editor > Inspections > PHP > Quality Tools".
  • Select a checkbox next to "PHP Code Sniffer validation" option.
  • On the right-hand pane of the page, configure the PHP Code Sniffer tool using the controls in the "Options" area:
  • From the "Severity" list, choose the "Error" for the PHP Code Sniffer inspection.
  • From the "Scope" list, choose the "Changed Files" to limit the inspection application to only changed files.
  • In the "Check files with extensions" field, provide the comma-separated list of file extensions that should be checked by PHP Code Sniffer.
  • To have PHP Code Sniffer report warnings in addition to errors, select the "Show warnings as..." checkbox and choose the "Errors" from the list.
  • Choose installed coding standard path as the main for standards
  • Select "EcgM2" it the "Coding standard" list.

https://www.jetbrains.com/help/phpstorm/using-php-code-sniffer.html#installing-configuring-code-sniffer

Note: We would also suggest enabling PHP Mess Detector validation as well.

Note: in case of using a Docker or VM, It needs to configure new PHPCS CLI Interpreter which will point out to VM/Docker.

Requirements

PHP 5.4 and up.

Checkout the php-5.3-compatible branch to get the PHP 5.3 version.

Contribution

Please feel free to contribute new sniffs or any fixes or improvements for the existing ones.

coding-standard's People

Contributors

zlik avatar zhooravlik avatar ovekeryk avatar alexkarand avatar alexkaranda avatar davidwindell avatar macieklewkowicz avatar philwinkle avatar zifius avatar schmengler avatar pborreli avatar diazwatson avatar tkn98 avatar lenaorobei avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.