Skip to content

Proposal: Sniffs consolidation #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 15, 2019
Merged

Conversation

lenaorobei
Copy link
Contributor

Proposal for PHP CodeSniffer coding standards consolidation.

Copy link
Contributor

@orlangur orlangur left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @lenaorobei,

To summarize, consolidation and unification is always a good idea, the only part here which looks doubtful to me is introduction of a separate repository which goes against monolithic approach of core repo. It is much better from productivity perspective and clearness on which set of rules is currently active for core (2.2, 2.3 release line etc).

MEQP may reside in separate repo, MFTF may have some deviations from core coding standard but they all must not be written from scratch but rather based on core one.

One important thing, not mentioned in proposal - whenever new rule is added to core standard, does not matter from MEQP or some other source, core codebase must be fully compliant with such rule. It does not make sense to introduce rule which is not followed and then add hundreds to thousands of phpcs suppressions.


## Problem Overview
There are few Magento 2 related coding standards:
- [Magento 2 Core sniffs](https://github.com/magento/magento2/tree/2.3-develop/dev/tests/static/framework/Magento/Sniffs)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only core coding standard and it is already consolidated.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving sniffs to a separate repo would also allow to use them on other projects.

There are few Magento 2 related coding standards:
- [Magento 2 Core sniffs](https://github.com/magento/magento2/tree/2.3-develop/dev/tests/static/framework/Magento/Sniffs)
- [Magento Marketplace Extension Quality Program](https://github.com/magento/marketplace-eqp) (in short: MEQP)
- [ExtDN PHP CodeSniffer rules](https://github.com/extdn/extdn-phpcs) (in short: Extdn)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some MEQP sniffs were added into core before. Some additional sniffs, like mentioned ones, can be always added in the future. It would be really nice to establish process for such additon.


- The MEQP coding standard uses a `severity` approach (from 10 to 1). The test fails only in the case of a `severity` 10 finding (`error`). Issues with `severity` from 1 to 9 marked as a `warning` and do not cause test failure.

- In the Magento 2 Core checking strategy, all findings lead to test failure. **Note:** _By default, PHP CodeSniffer assigns a severity of 5 to all errors and warnings._
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Core coding standard must be as strict as possible. Would be really nice to get rid of warnings in terms of PSR-2 at some point.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the direction we are working in.


- MEQP coding standard contains some false-positive sniffs, for which the aim is just to warn developers and draw their attention to the particular code piece.

- Some of Magento Core sniffs require Magento 2 instance to make a kind of `dynamic` check.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not really smell good, maybe report such occurrences for refactoring?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. Will remove such sniffs from phpcs rules and investigate tools which work reflection and class dependencies.


- Some of Magento Core sniffs require Magento 2 instance to make a kind of `dynamic` check.

As a result it leads to inconsistency and [code duplication](https://github.com/magento/magento2/issues/19477). Extension developers are confused about what code standard to use in their extension development and Magento 2 related projects contribution such as [MFTF](https://github.com/magento/magento2-functional-testing-framework/issues/265).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

19477 has nothing to do with inconsistency. It is all about MEQP release was not tied properly to Magento 2.3.0 release.

Regarding code duplication, you mentioned earlier

Each of these coding standards contain a lot of sniffs in common

The big question here is why MEQP standard is not based on Magento 2 standard in the first place.

Same for MFTF, it's better to have exactly same standard but it is also fine if it would extend Magento 2 core standard.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea is to have one set of rules for Magento code modules, Magento related projects and vendor's extensions.

3. Make static check consistent.

## Proposal
1. Consolidate all related Magento 2 sniffs to the new GitHub repository.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disagree with that. It is much easier and more effective to manage Core Coding Standard as a part or Core Repo in https://github.com/magento/magento2/tree/2.3-develop/dev/tests/static/framework/Magento.

However, it would be good to have it versioned and available separately as any other core component. Ideally would be to have it as separate read-only git subtree split.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small addition on this: I believe that best place for such standard is somewhere near https://github.com/magento/magento2/tree/2.3-develop/lib/internal/Magento/Framework/TestFramework.

Some Magento/Framework/PhpcsCodingStandard or Magento/Framework/CodeSniffer as an independent splittable component. Should by aligned with other framework-splitting activities currently performed.

Copy link
Member

@melnikovi melnikovi Feb 15, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Versioning being discussed in this proposal magento/magento-coding-standard#17. Coding standard should not be part of application framework. It's more related to dev tools than to components you use in your application business logic.


3. Assign severity to each sniff. The higher the severity, the more strict the rule is.

4. Create separate `ruleset.xml` files (set of sniffs) for Magento Core and Magento Extension Quality Program (reasons in the Problem Overview section).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see this is different from current situation.


5. For sniffs that require Magento 2 instance use [bootstrap file](https://github.com/squizlabs/PHP_CodeSniffer/wiki/Advanced-Usage#using-a-bootstrap-file).

6. Revisit other Magento 2 static tests (`PHPMD` rules and covered by `PHPUnit` static tests) and if it's possible rewrite them to use PHP CodeSniffer.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, sounds really good, I proposed this when there was the only custom design rule related to final keyword.

What do you think about deeper php-cs-fixer integration? As to me, it would be really nice to update https://github.com/magento/magento2/blob/2.3-develop/.php_cs.dist with as much existing useful rules as possible and enforce code similar to phpcs and phpmd.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Integration of PHP CS Fixer sounds good, can be investigated more in the future proposals.

@lenaorobei
Copy link
Contributor Author

lenaorobei commented Dec 18, 2018

@orlangur thank you for your input, however after discussion with Magento architecture team we came with the decision to create separate GitHub repository and start the consolidation process. We decided to keep one ruleset.xml for now. I will update the corresponding section of the proposal.
Some thoughts you've mentioned look reasonable for me, so I encourage you to participate in the discussion and implementation of the new unified coding standard.

@orlangur
Copy link
Contributor

@lenaorobei yeah, I saw https://github.com/magento/magento-coding-standard created. Too bad...

Having custom process instead of unified one for such a simple thing looks like a real mess to me.

Asked @buskamuza for meeting minutes of corresponding decision-making meeting.

@buskamuza
Copy link
Contributor

@orlangur , everything can be discussed here. This is why this proposal exists. And everything will be discussed on one of the open Architectural meetings.

I don't see any issues with a separate repo for code style rules. While, I also don't see how coding standard is a part of Magento Framework. It doesn't provide any application functionality, and it should be possible to install it on its own, w/o entire Magento Framework.

If you have specific concerns, please list them here, so we can address each of them in more details.


4. Create one `ruleset.xml` file (set of sniffs) for both projects - Magento Core and Magento Extension Quality Program.

5. For sniffs that require Magento 2 instance use [bootstrap file](https://github.com/squizlabs/PHP_CodeSniffer/wiki/Advanced-Usage#using-a-bootstrap-file).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please provide references to such sniffs? I agree with @orlangur that ideally we shouldn't have such ones, but let's review.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is the example: https://github.com/magento/magento2/blob/2.3-develop/dev/tests/static/framework/Magento/Sniffs/LiteralNamespaces/LiteralNamespacesSniff.php#L71

Before throw an error the sniff checks class_exists($className) || interface_exists($className).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. Thanks.

## Proposal
1. Consolidate all related Magento 2 sniffs to the new GitHub repository.

2. Create a new Composer package with `phpcodesniffer-standard` type and add it to the Magento 2 `composer.json` `reqiure-dev` section.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a standard package type?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's standard package type. It allows the package to be automatically processed by composer install plugins. The user doesn't have to manually run phpcs --config-set command to configure coding standard.

2. Store all related to Magento 2 sniffs in one place.
3. Make static check consistent.

## Proposal
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR is a proposal. Please rephrase it so that it looks as a final document with a decision. E.g., "Solution"

@orlangur
Copy link
Contributor

orlangur commented Jan 4, 2019

@buskamuza,

If you have specific concerns, please list them here

They are listed above ;)

@lenaorobei lenaorobei mentioned this pull request Jan 7, 2019
@melnikovi melnikovi merged commit c4cebc5 into magento:master Feb 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants