ExtDN PHP_CodeSniffer rules for Magento 2

Overview

ExtDN PHP_CodeSniffer rules for Magento 2

Build Status

Introduction

There are already many PHP CodeSniffer rules out there to aid in Magento 2 development:

However, some best practices still need to be integrated and/or some rules do need to be improved. This repository forms an effort to come up with more advanced rulesets than there currently are. Additionally, one of the underlying goals is to create rules that fit Magento core, Magento third party extensions and Magento implements, while they all have different needs.

Usage

To install this package, go to your Magento 2 root and use the following:

composer require extdn/phpcs:dev-master

If this fails because the dependency with magento/marketplace-eqp fails to load, first add the EQP repo to your configuration and then repeat:

composer config repositories.magento-marketplace-eqp vcs https://github.com/magento/marketplace-eqp
composer require magento/marketplace-eqp:dev-master
composer require extdn/phpcs:dev-master

Once installed, you can run PHPCS from the command-line to analyse your code XYZ:

vendor/bin/phpcs --standard=./vendor/extdn/phpcs/Extdn app/code/XYZ
vendor/bin/phpcs --standard=./vendor/extdn/phpcs/Extdn vendor/XYZ

If you have a Composer Installer Plugin for PHPCS coding standards (such as https://github.com/Dealerdirect/phpcodesniffer-composer-installer) configured in the project, you can refer to this standard simply by its name:

vendor/bin/phpcs --standard=Extdn app/code/XYZ
vendor/bin/phpcs --standard=Extdn vendor/XYZ

Where to contribute

We need help in the following areas:

  • Documentation of existing EQP rules (where each EQP rule could be included in this repository its ruleset.xml)
  • Creation of new PHPCS rules (see below How to contribute)
  • Braindumps on where PHPCS lacks and other tools might come in more handy (PhpStan, Phan)
  • Discussions on new rules (through periodic hangouts or discussions per GitHub Issue)

Please note that you are also welcome to contribute to the Magento rulesets directly (core, MEQP, ECG - see links above). Likewise, we will try to get ExtDN rules into Magento rulesets as well, which is part of this ExtDN project.

How to contribute

Any contribution is welcome. However, don't start coding just yet. Make sure first that the work is worth the effort.

  1. Add a new issue under Issues to address new rulesets that are needed or report other issues.

  2. Once the creation of the new rule has been accepted by adding a label Accepted under Issues, we're good to go.

  3. If a similar rule already exists in the core or MEQP or ECG, simply try to include this rule within the ExtDN ruleset.

  4. If no rule exists yet, let's create it. As an example, you can use the SetTemplateInBlockSniff within the folder Extdn/Sniffs/Blocks. It can be tested upon a sample file under Extdn/Samples/Blocks:

vendor/bin/phpcs --standard=./vendor/extdn/phpcs/Extdn vendor/extdn/phpcs/Extdn/Samples
  1. Once this all works, feel free to create a Pull Request (PR) including the addition of this rule to the ruleset.xml file.

Using labels with GitHub issues

Some important labels, used for Github issues:

  • accepted: The rule is accepted by extdn. If nobody claimed it yet, you may start working on it
  • experimental: The rule can be implemented as well, but we will try it out with a low severity first before integrating it into the official ruleset
  • non-PHPCS: The rule is not feasibly implementable with phpcs, will need additional tools. We keep it for later.
  • organizational: Non-code related issues
  • on agenda of hangout: The rule/issue will be discussed in the next community hangout

How to create a Sniffer Rule

We recommend to get started by creating a couple of sniffs for your own. The official PHPCS wiki already gives good information. Likewise, the other sniffs in our repos (and the repos of Magento) will give lots of samples anyway.

Testing

All rules should be accompanied with tests.

Within a Magento installation

To run the sniff unit tests from a main repository, where the rules are installed via composer, first configure phpcs to find the rules:

vendor/bin/phpcs --config-set installed_paths vendor/extdn/phpcs/Extdn,vendor/magento/marketplace-eqp/MEQP2

Then tests can be run like this:

phpunit -c vendor/extdn/phpcs/phpunit-vendor.xml vendor/extdn/phpcs/Extdn/Tests

In a standalone installation

If you have cloned this GitHub repository on its own for development, use composer to install things and run the tests:

composer install
composer test

Each Test.php class should be accompanied by a Test.inc file to allow for unit testing based upon the PHPCS parent class AbstractSniffUnitTest. Make sure to include a Test.md Markdown description which addresses the issue at hand, explains what the rule check for and then also suggests the improvement to be made.

Comments
  • Base Ruleset

    Base Ruleset

    opened by fooman 5
  • Travis fails with OpenSSL key generation error

    Travis fails with OpenSSL key generation error

    Travis fails with an error on the command openssl aes-256-cbc -K $encrypted_2b7e0505597a_key -iv $encrypted_2b7e0505597a_iv -in .travis/deploy_rsa_encrypted -out ~/.ssh/id_rsa -d which is defined in the travis.yml file of this repository. Before jumping ahead to fix this, I would like to know why this is needed :)

    bug 
    opened by jissereitsma 4
  • Rule: Curly braces should not be used in PHTML files

    Rule: Curly braces should not be used in PHTML files

    Curly braces should not be used in PHTML files.

    Bad:

    <?php
    if ($block->getFooList()) {
        foreach ($block->getFooList() as $foo) {
    ?>
            <!-- Template stuff -->
    <?php
        }
    }
    ?>
    

    Better (Proposal 1):

    <?php
    if ($block->getFooList()):
        foreach ($block->getFooList() as $foo):
    ?>
            <!-- Template stuff -->
    <?php
        endforeach;
    endif;
    ?>
    

    Better (Proposal 2):

    <?php if ($block->getFooList()) ?>
        <?php foreach ($block->getFooList() as $foo): ?>
            <!-- Template stuff -->
        <?php endforeach; ?>
    <?php endif; ?>
    

    (Source: https://twitter.com/jissereitsma/status/990301162090975232)

    opened by renttek 4
  • Fix for wrong type in ruleset.xml

    Fix for wrong type in ruleset.xml

    Description

    Related issues

    1. extdn/extdn-phpcs#<issue_number>: Issue title
    2. ...

    Sniff checklist

    • [ ] Sniff is accompanied by unit test
    • [ ] Sniff is documented using the documentation template (FooSniff.md next to FooSniff.php)
    opened by larsroettig 2
  • MEQP Update to version 4

    MEQP Update to version 4

    Rule

    MEQP Version 4 is released under new repo which not merged in extdn-phpcs

    Reason

    A lot of Magento 2 specific rules are added with severity 10 which should be added

    Implementation

    Merge https://github.com/magento/magento-coding-standard/

    opened by milindsingh1 1
  • Add Composer package type

    Add Composer package type

    Description

    Add package type to composer.json to allow the package to be automatically registered with PHPCS.

    Please refer to https://github.com/Dealerdirect/phpcodesniffer-composer-installer/wiki/Change-%60composer.json%60-%22type%22-to-%60phpcodesniffer-standard%60 for more information.

    Related issues

    None.

    Sniff checklist

    N/A

    opened by macieklewkowicz 1
  • Include and document MEQP2.Templates.ThisInTemplate

    Include and document MEQP2.Templates.ThisInTemplate

    Description

    The sniff MEQP2.Templates.ThisInTemplate gets included. It checks for deprecated usage of $this in PHTML templates.

    The Marketplace Extension Quality Program (MEQP2) has been added as composer requirement.

    Sniff checklist

    • [X] Sniff is accompanied by unit test
    • [X] Sniff is documented using the documentation template (FooSniff.md next to FooSniff.php)
    opened by schmengler 1
  • Fix setup instructions in readme

    Fix setup instructions in readme

    Description

    If you follow the README.md instructions to set up the sniffs you get an error:

    [~/projects/glovre-hosting/public_html] composer config repositories:extdn-phpcs vcs [email protected]:extdn/extdn-phpcs.git
    
      [InvalidArgumentException]
      Setting repositories:extdn-phpcs does not exist or is not supported by this command
    

    I've amended the command.

    Sniff checklist

    None relevant, very tiny PR

    opened by mikewhitby 1
  • Rule: No Phone Home

    Rule: No Phone Home

    Modules should not call home - either using an internal PHP call (CURL or file_get_open or something similar) or using a static asset. Not sure if this could be done using PHPCS.

    non-PHPCS 
    opened by jissereitsma 1
  • Deprecated parents fix

    Deprecated parents fix

    Description

    Bugfixes for edge cases in DeprecatedParentsSniff:

    • Due to sniffs that evaluate code, a class may already be autoloaded, do not include the file in that case
    • Do not throw PHP Fatal error: Uncaught Error: Call to a member function getName() on boolean if a block does not have a parent class
    opened by schmengler 0
  • Implement doc for rule on Strict Types

    Implement doc for rule on Strict Types

    Description

    Documentation for rule was still missing. See https://github.com/extdn/extdn-phpcs/issues/47

    Related issues

    1. extdn/extdn-phpcs#47: Document "strict types" rule
    opened by jissereitsma 0
  • Rule: Source models extending \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource must use $_options to cache values

    Rule: Source models extending \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource must use $_options to cache values

    Rule

    The AbstractSource model contains a protected attribute $_options that is meant to cache the options. Unfortunately this is not enforced, instead each implementation of getAllOptions() is responsible to use that cache properly.

    Reason

    EAV source models often load possible values for attributes from external resources. Every access to those values, e.g. with $product->getAttributeText() calls getAllOptions() on the source model. The result of this method should only be calculated once.

    Implementation

    Any class that extends the abstract source model should return $this->_options in getAllOptions()

    opened by schmengler 0
  • Never Extend from Magento\Framework\App\Helper\AbstractHelper

    Never Extend from Magento\Framework\App\Helper\AbstractHelper

    Rule

    Never Extend from Magento\Framework\App\Helper\AbstractHelper

    Reason

    The AbstractHelper has many dependy that you never need for your helper implementation. Also it breacks your helper if you use the ScopeConfigInterface and upgrade 2.1 to 2.2.

    Implementation

    opened by larsroettig 2
  • New labels: Move to Magento core / MEQP

    New labels: Move to Magento core / MEQP

    After a call between various Magento people, @schmengler and myself, it was suggested that having too many PHPCS repositories is a bad thing. In short, we would be happy if some of our rules are merged into the Magento core or the MEQP ruleset. Another goal could be to minimize the chance of us and them working on the same kind of rules.

    Shall we create new labels "Move to Magento core" and "Move to MEQP" because of this?

    opened by jissereitsma 4
  • Rule: Do not use debugging statements

    Rule: Do not use debugging statements

    Description

    In a production-ready extension, there shouldn't be any functions like die(), print_r(), var_export() or var_dump(). This PR adds a new rule that checks upon this. This is a very simple straightforward PHPCS implementation.

    One thing that I foresee as an issue already is that I sometimes log things away by using var_export($x, true). I'm not sure if that's a lack of quality or simply a nifty feature.

    Related issues

    None

    Sniff checklist

    debate needed 
    opened by jissereitsma 0
Owner
ExtDN
We are a network of leading Magento extension developers who share a common vision for a thriving Magento ecosystem with the merchant's success at heart.
ExtDN
Set of rules for PHP_CodeSniffer and PHP-CS-Fixer used by Symplify projects.

20+ Coding Standard checkers for PHP projects with focus on Clean Architecture

null 281 Dec 30, 2022
Composer installer for PHP_CodeSniffer coding standards

PHP_CodeSniffer Standards Composer Installer Plugin This composer installer plugin allows for easy installation of PHP_CodeSniffer coding standards (r

Dealerdirect 462 Dec 22, 2022
The Easiest way to start using PHP CS Fixer and PHP_CodeSniffer with 0-knowledge

The Easiest Way to Use Any Coding Standard Features Blazing fast Parallel run Use PHP_CodeSniffer || PHP-CS-Fixer - anything you like 2nd run under fe

null 1.1k Jan 6, 2023
This composer installer plugin allows for easy installation of PHP_CodeSniffer coding standards

PHP_CodeSniffer Standards Composer Installer Plugin This composer installer plugin allows for easy installation of PHP_CodeSniffer coding standards (r

PHPCSStandards 393 Feb 25, 2022
PHP_CodeSniffer tokenizes PHP files and detects violations of a defined set of coding standards.

PHP_CodeSniffer is a set of two PHP scripts; the main phpcs script that tokenizes PHP, JavaScript and CSS files to detect violations of a defined coding standard, and a second phpcbf script to automatically correct coding standard violations. PHP_CodeSniffer is an essential development tool that ensures your code remains clean and consistent.

Squiz Labs 9.9k Jan 5, 2023
Check modules in app/code and vendor for PHP 8 compatibility status - PHP_CodeSniffer & php-compatibility standard

M2 PHP version compatibility check How To use Requires PHP 7.3+ | PHP 8 This app will run PHP_CodeSniffer with phpcompatibility/php-compatibility on t

William Tran 24 Oct 13, 2022
A tool that allows to quickly export data from Magento 1 and Magento 2 store and import it back into Magento 2

Simple Import / Export tool A tool that allows to quickly export data from Magento 1 and Magento 2 store and import it back into Magento 2. Table data

EcomDev B.V. 51 Dec 5, 2022
Rector upgrades rules for Doctrine

Rector Rules for Doctrine See available Doctrine rules Install This package is already part of rector/rector package, so it works out of the box.

Rector 37 Nov 7, 2022
A Pocketmine-MP (PMMP) plugin to help staff members enforce the rules of the server.

StaffMode is an all-in-one Pocketmine-MP (PMMP) moderation plugin made to simplify the life of staff members.

ItsMax123 9 Sep 17, 2022
Rules to detect game engines and other technologies based on Steam depot file lists

SteamDB File Detection Rule Sets This is a set of scripts that are used by SteamDB to make educated guesses about the engine(s) & technology used to b

Steam Database 103 Dec 14, 2022
Custom PHPStan rules

phpstan-rules Provides additional rules for phpstan/phpstan. Installation Run $ composer require --dev alister/phpstan-rules Usage All of the rules pr

Alister Bulman 1 Nov 4, 2021
Validated properties in PHP8.1 and above using attribute rules

PHP Validated Properties Add Rule attributes to your model properties to make sure they are valid. Why this package? When validating external data com

null 23 Oct 18, 2022
Rector Rules for BEAR.Sunday

Rector Rules for BEAR.Sunday The rector/rector rules for BEAR.Sunday.

BEAR.Sunday 2 Mar 30, 2022
Various PHPStan rules we found useful in ShipMonk.

ShipMonk PHPStan rules Various rules we found useful in ShipMonk. You may found some of them opinionated, so we recommend picking only those fitting y

ShipMonk R&D 31 Dec 22, 2022
Repository containing all the PHPStan rules from the book "Recipes for Decoupling"

PHPStan rules from the book "Recipes for Decoupling" by Matthias Noback In the book "Recipes for Decoupling" we discuss how to decouple from web and C

Matthias Noback 19 Sep 21, 2022
Test and enforce architectural rules in your Laravel applications. Keep your app's architecture clean and consistent!

Laravel Arkitect Laravel Arkitect lets you test and enforce your architectural rules in your Laravel applications, and it's a PHPArkitect wrapper for

SMorteza Ebadi 55 Dec 17, 2022
Dedicated plugin for PocketMine-API 4, this will help staff members to make players follow the rules to the letter

StaffMode Dedicated plugin for PocketMine-API 4, this will help staff members to make players follow the rules to the letter Annotation This plugin is

Kurth 3 Aug 12, 2022