google / site-kit-wp

Site Kit is a one-stop solution for WordPress users to use everything Google has to offer to make them successful on the web.
https://sitekit.withgoogle.com
Apache License 2.0
1.25k stars 290 forks source link

Extend PHPCS rules to enforce all style rules #924

Open aaemnnosttv opened 4 years ago

aaemnnosttv commented 4 years ago

Feature Description

In addition to following WordPress core coding styles for PHP, there are some additional coding style rules which are applied that are not covered by the PHPCS configuration.

This may be partially covered by #547 but some rules will likely still not be enforced.

E.g.

We should extend the PHPCS configuration to include these additions as much as possible.


Do not alter or remove anything below. The following sections will be managed by moderators only.

Acceptance criteria

There should be PHPCS rules to enforce the following:

Semantic

Organizational

Bonus points for opening this also against WordPress core, which probably could use all of it :-)

Implementation Brief

namespace PHP_CodeSniffer\Standards\MyStandard\Sniffs\Commenting;

use PHP_CodeSniffer\Sniffs\Sniff; use PHP_CodeSniffer\Files\File;

class DisallowHashCommentsSniff implements Sniff { /**

?>



Create a new sniff file for each of the rules in the table:

**Semantic**

| File Name/Class Name | Register Token(s) | Process Function Brief |
| --- | --- | --- |
| `Semantic/DescriptionStartsWithCapitalLetter.php` / `DescriptionStartsWithCapitalLetter` | T_COMMENT | Every description of everything starts with a capital letter (descriptions, parameter docs, return docs, ...). |
| `Semantic/DescriptionEndsWithFullStop.php` / `DescriptionEndsWithFullStop` | T_COMMENT | Every description of everything ends in a dot (descriptions, parameter docs, return docs, ...). |
| `Semantic/DescriptionStartsWithThirdPersonVerb.php` / `DescriptionStartsWithThirdPersonVerb` | T_COMMENT | For functions and methods, the first word of the description must end in an "s" (this is probably 99% reliable, but most importantly should never yield a false positive - I don't think there are any exceptions to the grammatical rule of 3P verbs ending in an "s"). |

**Organizational**

| File Name/Class Name | Register Token(s) | Process Function Brief |
| --- | --- | --- |
| `Organizational/CommentHasSinceTag.php` / `DescriptionStartsWithThirdPersonVerb` | T_COMMENT | Every property/function/method/class/interface/trait doc-block should have a since tag. |
| `Organizational/CommentTagsInCorrectOrder.php` / `CommentTagsInCorrectOrder` | T_COMMENT | The order of tags (if present, for each) should be since (multiple entries allowed), deprecated, access, static, global, var, param (multiple entries allowed), return. |
| `Organizational/CommentTagsCorrectlyGrouped.php` / `CommentTagsCorrectlyGrouped ` | T_COMMENT | For methods and functions, tags should be grouped: since/deprecated/access/static should be grouped together, separated by a blank line from param/return which should be grouped together. If there is global, it should be separated from both others by a blank line (essentially be its own group). |
| `Organizational/CommentTagsHaveNoSpaces.php` / `CommentTagsHaveNoSpaces ` | T_COMMENT | For properties, all tags should be in a single group (no blank lines between them). |
| `Organizational/CommentTagsDescriptionsAligned.php` / `CommentTagsDescriptionsAligned ` | T_COMMENT | All param tag types and descriptions in a single doc-block should be indented in a way that they are aligned with each other. |

## Changelog entry

* <!-- One sentence summarizing the PR, to be used in the changelog. -->
aaemnnosttv commented 4 years ago

@felixarntz I've been looking into the IB for this one and it looks like the work to be done is fairly substantial - especially since we have no custom sniffs of our own yet - and may need to be broken into multiple issues. There's also a question as to whether this repo is the right place for these rules to be defined as it seems like some of these are filling in rules that aren't implemented in WPCS yet.

Either way I don't think it makes sense to plan this one for the upcoming sprint as there is more investigation/definition needed here.

felixarntz commented 4 years ago

@aaemnnosttv I believe we should move forward with this and implement these rules as part of the repository. We're now enforcing them in JS (see #1493), and it makes sense to add the same strictness to our PHP codebase.

I believe contributing this to WPCS would be a great effort, and we should do so, but I suggest decoupling that. We can replace our own implementation with theirs if such rules get adopted there.

aaemnnosttv commented 4 years ago

Sounds good, thanks for confirming @felixarntz.

I'm going to remove the GFI label though since I don't think it meets that criteria based on my previous investigation here.

benbowler commented 3 years ago

I've added an IB and an estimate.

aaemnnosttv commented 3 years ago

@benbowler – is this ready for review?

benbowler commented 3 years ago

Yes, moved into the review column.

tofumatt commented 3 years ago

A minor few issues with the IB:

  1. There's a mention of a folder named includes/CodeStandards/GoogleSiteKit/Sniffs/Semantical but "Semantical" isn't a word. Did you mean "Semantic"?
  2. There's a mixture of mentions of includes/**CodeStandards**/GoogleSiteKit and includes/**CodeStandards**/GoogleSiteKit. Are they meant to be different folders? That's sort of confusing if so.

I think the approach looks proper though—I'm not wildly familiar with writing PHP Sniff rules but I'd think this would work similar to the ESLint rules and this approach matches that from what I can tell.

Can you address the two issues I mentioned? After that this should be good to go 🙂

benbowler commented 3 years ago

Hey @tofumatt, I've replaced all examples of Semantical with Semantic and updated all of the folder names to CodeStandards.

tofumatt commented 3 years ago

IB ✅

aaemnnosttv commented 3 years ago

Bumping the estimate here since this one will undoubtedly be a beast and will also require a large number of changes across the codebase in order to conform to the new style rules.

cc: @eclarke1 @fhollis

benbowler commented 3 years ago

Ready for review on implementation.

felixarntz commented 3 years ago

@aaemnnosttv Assigning to you for now; can you check who would want to proceed with the work that Ben started?

aaemnnosttv commented 3 years ago

@felixarntz – happy to take this over although there are a few things I'd like to discuss first; let's discuss on our next call.

aaemnnosttv commented 3 years ago

@felixarntz given our conversation about the changes here, do you think this makes sense to keep in execution or move it back to definition since the implementation will be substantially different? I suppose the ACs could remain the same, but we should probably move and plan this issue to be specific to the final integration of the new sniffs since the majority of the engineering will no longer be part of the SK codebase.

felixarntz commented 3 years ago

@aaemnnosttv Yeah, let's move this back to ACs for now and later update them once the path forward is clear.

aaemnnosttv commented 3 years ago

@eclarke1 @fhollis moving back to ACs and removing from the sprint as this issue will be redefined to be much simpler but won't be something we can do until later.

aaemnnosttv commented 3 years ago

@felixarntz moving to stalled for now as this likely won't be actionable in the near future.

felixarntz commented 3 years ago

Moving this back to Backlog as we now have the new repository and #4187 as a prerequisite so this can soon move forward.

swissspidy commented 3 years ago

Just stumbled upon https://github.com/GoogleChromeLabs/wpcs-core-extra-sniffs & this issue.

Are you planning on submitting some of these rules to WPCS directly? Seems like most of them would be happily accepted there as they also apply to core.

felixarntz commented 3 years ago

@swissspidy Eventually, yes. We wanted to start them in their own repository though to be independent of WPCS itself. It would also allow us to "prove" them in the wild (in Site Kit) before proposing them for merge.

aaemnnosttv commented 2 years ago

@felixarntz what do you think about converting this to an epic? I think we probably have a single issue for laying the foundation and then each rule could probably have its own issue which could also potentially allow for some to happen at the same time.

felixarntz commented 2 years ago

@aaemnnosttv Good idea, that makes sense. Although I think we can skip the epic here and break this down into multiple issues without one - we already have a label to mark anything related to that other repository. First we should get started with #4187 though.