magento / magento/magento-coding-standard

[Proposal] Version specific sniffs

Open
#17 10 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

need to discuss proposal version specifiс
Dominant language
PHP
Stars
375
Forks
165
PR merge metrics
No merged PRs in 30d

Description

Problem Overview

Some of the rules like strict_types were introduced in later Magento versions and are not applicable to earlier ones. Magento Marketplace still checks the code of extensions compatible with Magento 2.0, 2.1, 2.2. How to handle version specific rules?

Solution

Provide mechanism of version specific sniffs using OOB PHP CodeSniffer functionality. By default PHP CodeSniffer will do check assuming the latest Magento version.

Implementation Details

Create new sniffs Group which will handle runtime parameter magentoVersion and run sniff only when it meets version requirement.

For example
phpcs --runtime-set magentoVersion 2.2

Each sniff from version specific group will call doRun method that checks versions compatibiliy.

use PHP_CodeSniffer\Config;

class VersionChecker
{
    public function doRun($sniffVersion)
    {
        $runtimeVersion = Config::getConfigData('magentoVersion');
        if ($runtimeVersion !== null) {
            return version_compare($runtimeVersion, $sniffVersion, '>=');
        }
        return true;
    }
}

Magento version specific sniff will contain code that determines whether the sniff needs to be executed.

class SomeNewlyIntroducedSniff implements Sniff
{
   // Magento version where the rule was introduced. 
    private $introducedIn = '2.3';

    private $versionChecker;

    public function __construct()
    {
        $this->versionChecker = new VersionChecker();
    }

    public function process(File $phpcsFile, $stackPtr)
    {
        if ($this->versionChecker->doRun($this->introducedIn) === false) {
            return;
        }
        //code goes here
    }
}
Pros
  • everything in one place;
  • no need to maintain the whole repo versioning.
Cons
  • only default behavior will work in IDE;
  • need to care about legacy sniffs if specific Magento version became unsupported.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the existing sniff groups and PHP_CodeSniffer's runtime configuration support, then compare the proposal's VersionChecker and magentoVersion examples with the repository's current architecture. The work is done when version-specific sniffs can reliably skip rules for older Magento versions while retaining the documented default behavior and compatibility expectations.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.