-
Notifications
You must be signed in to change notification settings - Fork 152
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Overview | ||
PHP CodeSniffer is a static code analysis tool that tokenizes PHP, JavaScript and CSS files to detect violations of a defined coding standard. | ||
|
||
A coding standard in PHP CodeSniffer is a collection of sniff files combined in the `ruleset.xml` file. Each sniff checks one part of the coding standard. | ||
|
||
Each sniff can have its own `severity` (priority) and `type` (two options available: `error` or `warning`). | ||
|
||
## 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) | ||
- [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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
||
Each of these coding standards contain a lot of sniffs in common. However, there are also some differences in the code checking approach. For example: | ||
|
||
- 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._ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This does not really smell good, maybe report such occurrences for refactoring? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're right. Will remove such sniffs from |
||
|
||
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). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
||
## Goals | ||
1. Make it easier to run static checks for extensions developers. | ||
2. Store all related to Magento 2 sniffs in one place. | ||
3. Make static check consistent. | ||
|
||
## Solution | ||
1. Consolidate all related Magento 2 sniffs to the new GitHub repository. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
||
2. Create a new Composer package with `phpcodesniffer-standard` type and add it to the Magento 2 `composer.json` `reqiure-dev` section. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this a standard package type? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
3. Assign severity to each sniff. The higher the severity, the more strict the rule is. | ||
|
||
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). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok. Thanks. |
||
|
||
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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 What do you think about deeper There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
||
7. Work with community on improvements to sniffs and make regular releases. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.