Skip to content
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

YoastCS: more docblock/comment rules #364

Merged
merged 9 commits into from
Dec 14, 2023

Conversation

jrfnl
Copy link
Collaborator

@jrfnl jrfnl commented Dec 14, 2023

YoastCS rules: enforce alignment of the description parts for a @param tag

This rule is disabled in WordPressCS as it doesn't play nice with the convoluted WP native array annotation.

For YoastCS it has been decided to apply the rule anyway. We'll need to evaluate on a case-by-case basis how to convert from the WP array annotation to another form of documenting the array format.

Impact on Yoast packages:

Plugin/Tool Errors/Warnings
PHPUnit Polyfills --
WP Test Utils --
YoastCS --
WHIP --
Yoast Test Helper --
Duplicate Post --
Yst ACF --
Yst WooCommerce 6
Yst News --
Yst Local 6
Yst Video 1
Yst Premium 11
Yst Free 77

Note: this rule was previously already "silently" enforced via clean-up sweeps.

YoastCS rules: enforce proper grammar/punctuation in param description

This rule is disabled in WordPressCS as it doesn't play nice with the convoluted WP native array annotation.

For YoastCS it has been decided to apply the rule anyway. We'll need to evaluate on a case-by-case basis how to convert from the WP array annotation to another form of documenting the array format.

Impact on Yoast packages:

Plugin/Tool Errors/Warnings
PHPUnit Polyfills --
WP Test Utils --
YoastCS --
WHIP --
Yoast Test Helper --
Duplicate Post --
Yst ACF --
Yst WooCommerce 1
Yst News --
Yst Local 1
Yst Video --
Yst Premium --
Yst Free 11

Note: this rule was previously already "silently" enforced via clean-up sweeps.

YoastCS rules: enforce that docblocks should have a valid @return tag

This rule is disabled in WordPressCS as WP Core does not want @return void, but consistency and explicitness of intend is important, so we're going to turn it back on.

Note: @return tags will still not be enforced for constructors which should always be "never" methods anyway.

Impact on Yoast packages:

Plugin/Tool Errors/Warnings
PHPUnit Polyfills 57 // = Test fixtures.
WP Test Utils --
YoastCS --
WHIP 1
Yoast Test Helper 18
Duplicate Post 280
Yst ACF 14
Yst WooCommerce 172
Yst News 92
Yst Local 204 (but there are also still 54 functions without a docblock altogether)
Yst Video 381
Yst Premium 804 + 2 invalid void
Yst Free 4162 + 3 invalid void (and 4 function without a docblock altogether)

YoastCS rules: enforce short form type annotations

I.e. bool not boolean, int not integer.

Impact on Yoast packages:

Plugin/Tool Errors/Warnings
PHPUnit Polyfills --
WP Test Utils --
YoastCS --
WHIP --
Yoast Test Helper --
Duplicate Post --
Yst ACF --
Yst WooCommerce --
Yst News --
Yst Local --
Yst Video --
Yst Premium --
Yst Free 1

Note: this rule was previously already "silently" enforced via clean-up sweeps.

YoastCS rules: enforce type annotations to have null as last type

(whenever relevant)

Impact on Yoast packages:

Plugin/Tool Errors/Warnings
PHPUnit Polyfills --
WP Test Utils --
YoastCS --
WHIP --
Yoast Test Helper --
Duplicate Post --
Yst ACF --
Yst WooCommerce --
Yst News --
Yst Local --
Yst Video --
Yst Premium --
Yst Free --

Note: this rule was previously already "silently" enforced via clean-up sweeps.

YoastCS rules: verify property type annotations

This sniff:

  • Disallows plain array type annotations. These should always be made specific.
  • Flags missing type annotations.

Optionally the sniff can enforce PHP native type declarations instead of annotations in a docblock.

This functionality has been disabled for the following reasons:

  • Adding type declarations without using strict_types leads to bugs as the original (scalar) data will be juggled by PHP, even if the original data was never correct to start with. The loss of the type information of the original data passed, means that the function can no longer discern whether the input is invalid/incorrect.
    Think: a function expecting an integer, but being passed false. This will be juggled to 0, while it should have been flagged as unusable input.
  • Adding strict_types in the context of WP is a no-no to begin with as it can cause fatal errors (= white screen of death) when the exceptions are not caught and they generally are not caught and WP doesn't have a native exception handler in place either.
  • Adding strict_types is useless anyway as it is only enforced for files containing such a declaration, which means that parameters passed to a function call from an external source which doesn't have the strict_types declaration (callback called from WP) will still be juggled, which again leads to bugs.
  • Adding type declarations in functions hooked into WP filters is especially dangerous due to the loss of type info.

All in all, with the type system offered by PHP as-is, adding PHP native type declarations is next to useless unless you have full control of all code + the environment the code is being run on, which for the Yoast code is just not the case (with the exception of Platform).

Impact on Yoast packages:

Plugin/Tool Errors/Warnings
PHPUnit Polyfills --
WP Test Utils --
YoastCS --
WHIP 2
Yoast Test Helper 2
Duplicate Post 1
Yst ACF 4
Yst WooCommerce 7
Yst News 3
Yst Local 40
Yst Video 44
Yst Premium 85
Yst Free 366

YoastCS rules: verify parameter type annotations

This sniff:

  • Disallows plain array type annotations. These should always be made specific.
  • Flags missing type annotations.

Optionally the sniff can enforce PHP native type declarations instead of annotations in a docblock.

This functionality has been disabled for the following reasons:

  • Adding type declarations without using strict_types leads to bugs as the original (scalar) data will be juggled by PHP, even if the original data was never correct to start with. The loss of the type information of the original data passed, means that the function can no longer discern whether the input is invalid/incorrect.
    Think: a function expecting an integer, but being passed false. This will be juggled to 0, while it should have been flagged as unusable input.
  • Adding strict_types in the context of WP is a no-no to begin with as it can cause fatal errors (= white screen of death) when the exceptions are not caught and they generally are not caught and WP doesn't have a native exception handler in place either.
  • Adding strict_types is useless anyway as it is only enforced for files containing such a declaration, which means that parameters passed to a function call from an external source which doesn't have the strict_types declaration (callback called from WP) will still be juggled, which again leads to bugs.
  • Adding type declarations in functions hooked into WP filters is especially dangerous due to the loss of type info.

All in all, with the type system offered by PHP as-is, adding PHP native type declarations is next to useless unless you have full control of all code + the environment the code is being run on, which for the Yoast code is just not the case (with the exception of Platform).

Impact on Yoast packages:

Plugin/Tool Errors/Warnings
PHPUnit Polyfills 4
WP Test Utils 3
YoastCS --
WHIP 4
Yoast Test Helper 10
Duplicate Post 37
Yst ACF 1
Yst WooCommerce 37
Yst News 17
Yst Local 176
Yst Video 147
Yst Premium 263
Yst Free 913

YoastCS rules: verify return type annotations

This sniff:

  • Disallows plain array type annotations. These should always be made specific.
  • Flags missing type annotations.

Optionally the sniff can enforce PHP native type declarations instead of annotations in a docblock.

This functionality has been disabled for the following reasons:

  • Adding type declarations without using strict_types leads to bugs as the original (scalar) data will be juggled by PHP, even if the original data was never correct to start with. The loss of the type information of the original data passed, means that the function can no longer discern whether the input is invalid/incorrect.
    Think: a function expecting an integer, but being passed false. This will be juggled to 0, while it should have been flagged as unusable input.
  • Adding strict_types in the context of WP is a no-no to begin with as it can cause fatal errors (= white screen of death) when the exceptions are not caught and they generally are not caught and WP doesn't have a native exception handler in place either.
  • Adding strict_types is useless anyway as it is only enforced for files containing such a declaration, which means that parameters passed to a function call from an external source which doesn't have the strict_types declaration (callback called from WP) will still be juggled, which again leads to bugs.
  • Adding type declarations in functions hooked into WP filters is especially dangerous due to the loss of type info.

All in all, with the type system offered by PHP as-is, adding PHP native type declarations is next to useless unless you have full control of all code + the environment the code is being run on, which for the Yoast code is just not the case (with the exception of Platform).

Impact on Yoast packages:

Plugin/Tool Errors/Warnings
PHPUnit Polyfills 22
WP Test Utils 9
YoastCS --
WHIP 5
Yoast Test Helper 21
Duplicate Post 44
Yst ACF 5
Yst WooCommerce 48
Yst News 22
Yst Local 132
Yst Video 237
Yst Premium 269
Yst Free 1066

YoastCS rules: disallow the "mixed" type in annotations

An exception is made for the tests, as a data provider for input validation within a function could well be passing every single different type, in which case, mixed is perfectly valid.

Fixes #196

Impact on Yoast packages:

Plugin/Tool Errors/Warnings
PHPUnit Polyfills 49 (this is valid though as these are assertion declarations)
WP Test Utils --
YoastCS 2
WHIP --
Yoast Test Helper 4
Duplicate Post 12
Yst ACF 1
Yst WooCommerce 2
Yst News 1
Yst Local 42
Yst Video 12
Yst Premium 17
Yst Free 151

Related to #303

…am` tag

This rule is disabled in WordPressCS as it doesn't play nice with the convoluted WP native array annotation.

For YoastCS it has been decided to apply the rule anyway. We'll need to evaluate on a case-by-case basis how to convert from the WP array annotation to another form of documenting the array format.

Related to 303

Impact on Yoast packages:

| Plugin/Tool       | Errors/Warnings |
|-------------------|-----------------|
| PHPUnit Polyfills | --
| WP Test Utils     | --
| YoastCS           | --
| WHIP              | --
| Yoast Test Helper | --
| Duplicate Post    | --
| Yst ACF           | --
| Yst WooCommerce   | 6
| Yst News          | --
| Yst Local         | 6
| Yst Video         | 1
| Yst Premium       | 11
| Yst Free          | 77

Note: this rule was previously already "silently" enforced via clean-up sweeps.
This rule is disabled in WordPressCS as it doesn't play nice with the convoluted WP native array annotation.

For YoastCS it has been decided to apply the rule anyway. We'll need to evaluate on a case-by-case basis how to convert from the WP array annotation to another form of documenting the array format.

Related to 303

Impact on Yoast packages:

| Plugin/Tool       | Errors/Warnings |
|-------------------|-----------------|
| PHPUnit Polyfills | --
| WP Test Utils     | --
| YoastCS           | --
| WHIP              | --
| Yoast Test Helper | --
| Duplicate Post    | --
| Yst ACF           | --
| Yst WooCommerce   | 1
| Yst News          | --
| Yst Local         | 1
| Yst Video         | --
| Yst Premium       | --
| Yst Free          | 11

Note: this rule was previously already "silently" enforced via clean-up sweeps.
This rule is disabled in WordPressCS as WP Core does not want `@return void`, but consistency and explicitness of _intend_ is important, so we're going to turn it back on.

Note: `@return` tags will still not be enforced for constructors which should always be "never" methods anyway.

Related to 303

Impact on Yoast packages:

| Plugin/Tool       | Errors/Warnings |
|-------------------|-----------------|
| PHPUnit Polyfills | 57 // = Test fixtures.
| WP Test Utils     | --
| YoastCS           | --
| WHIP              | 1
| Yoast Test Helper | 18
| Duplicate Post    | 280
| Yst ACF           | 14
| Yst WooCommerce   | 172
| Yst News          | 92
| Yst Local         | 204 (but there are also still 54 functions without a docblock altogether)
| Yst Video         | 381
| Yst Premium       | 804 + 2 invalid void
| Yst Free          | 4162 + 3 invalid void (and 4 function without a docblock altogether)
I.e. `bool` not `boolean`, `int` not `integer`.

Related to 303

Impact on Yoast packages:

| Plugin/Tool       | Errors/Warnings |
|-------------------|-----------------|
| PHPUnit Polyfills | --
| WP Test Utils     | --
| YoastCS           | --
| WHIP              | --
| Yoast Test Helper | --
| Duplicate Post    | --
| Yst ACF           | --
| Yst WooCommerce   | --
| Yst News          | --
| Yst Local         | --
| Yst Video         | --
| Yst Premium       | --
| Yst Free          | 1

Note: this rule was previously already "silently" enforced via clean-up sweeps.
(whenever relevant)

Related to 303

Impact on Yoast packages:

| Plugin/Tool       | Errors/Warnings |
|-------------------|-----------------|
| PHPUnit Polyfills | --
| WP Test Utils     | --
| YoastCS           | --
| WHIP              | --
| Yoast Test Helper | --
| Duplicate Post    | --
| Yst ACF           | --
| Yst WooCommerce   | --
| Yst News          | --
| Yst Local         | --
| Yst Video         | --
| Yst Premium       | --
| Yst Free          | --

Note: this rule was previously already "silently" enforced via clean-up sweeps.
This sniff:
* Disallows plain `array` type annotations. These should always be made specific.
* Flags missing type annotations.

Optionally the sniff can enforce PHP native type declarations instead of annotations in a docblock.

This functionality has been **disabled** for the following reasons:
* Adding type declarations without using `strict_types` leads to bugs as the original (scalar) data will be juggled by PHP, even if the original data was never correct to start with. The loss of the type information of the original data passed, means that the function can no longer discern whether the input is invalid/incorrect.
    Think: a function expecting an integer, but being passed `false`. This will be juggled to `0`, while it should have been flagged as unusable input.
* Adding `strict_types` in the context of WP is a no-no to begin with as it can cause fatal errors (= white screen of death) when the exceptions are not caught and they generally are not caught and WP doesn't have a native exception handler in place either.
* Adding `strict_types` is useless anyway as it is only enforced for files containing such a declaration, which means that parameters passed to a function call from an external source which doesn't have the `strict_types` declaration (callback called from WP) will still be juggled, which again leads to bugs.
* Adding type declarations in functions hooked into WP filters is especially dangerous due to the loss of type info.

All in all, with the type system offered by PHP as-is, adding PHP native type declarations is next to useless unless you have full control of all code + the environment the code is being run on, which for the Yoast code is just not the case (with the exception of Platform).

Related to 303

Impact on Yoast packages:

| Plugin/Tool       | Errors/Warnings |
|-------------------|-----------------|
| PHPUnit Polyfills | --
| WP Test Utils     | --
| YoastCS           | --
| WHIP              | 2
| Yoast Test Helper | 2
| Duplicate Post    | 1
| Yst ACF           | 4
| Yst WooCommerce   | 7
| Yst News          | 3
| Yst Local         | 40
| Yst Video         | 44
| Yst Premium       | 85
| Yst Free          | 366
This sniff:
* Disallows plain `array` type annotations. These should always be made specific.
* Flags missing type annotations.

Optionally the sniff can enforce PHP native type declarations instead of annotations in a docblock.

This functionality has been **disabled** for the following reasons:
* Adding type declarations without using `strict_types` leads to bugs as the original (scalar) data will be juggled by PHP, even if the original data was never correct to start with. The loss of the type information of the original data passed, means that the function can no longer discern whether the input is invalid/incorrect.
    Think: a function expecting an integer, but being passed `false`. This will be juggled to `0`, while it should have been flagged as unusable input.
* Adding `strict_types` in the context of WP is a no-no to begin with as it can cause fatal errors (= white screen of death) when the exceptions are not caught and they generally are not caught and WP doesn't have a native exception handler in place either.
* Adding `strict_types` is useless anyway as it is only enforced for files containing such a declaration, which means that parameters passed to a function call from an external source which doesn't have the `strict_types` declaration (callback called from WP) will still be juggled, which again leads to bugs.
* Adding type declarations in functions hooked into WP filters is especially dangerous due to the loss of type info.

All in all, with the type system offered by PHP as-is, adding PHP native type declarations is next to useless unless you have full control of all code + the environment the code is being run on, which for the Yoast code is just not the case (with the exception of Platform).

Related to 303

Impact on Yoast packages:

| Plugin/Tool       | Errors/Warnings |
|-------------------|-----------------|
| PHPUnit Polyfills | 4
| WP Test Utils     | 3
| YoastCS           | --
| WHIP              | 4
| Yoast Test Helper | 10
| Duplicate Post    | 37
| Yst ACF           | 1
| Yst WooCommerce   | 37
| Yst News          | 17
| Yst Local         | 176
| Yst Video         | 147
| Yst Premium       | 263
| Yst Free          | 913
This sniff:
* Disallows plain `array` type annotations. These should always be made specific.
* Flags missing type annotations.

Optionally the sniff can enforce PHP native type declarations instead of annotations in a docblock.

This functionality has been **disabled** for the following reasons:
* Adding type declarations without using `strict_types` leads to bugs as the original (scalar) data will be juggled by PHP, even if the original data was never correct to start with. The loss of the type information of the original data passed, means that the function can no longer discern whether the input is invalid/incorrect.
    Think: a function expecting an integer, but being passed `false`. This will be juggled to `0`, while it should have been flagged as unusable input.
* Adding `strict_types` in the context of WP is a no-no to begin with as it can cause fatal errors (= white screen of death) when the exceptions are not caught and they generally are not caught and WP doesn't have a native exception handler in place either.
* Adding `strict_types` is useless anyway as it is only enforced for files containing such a declaration, which means that parameters passed to a function call from an external source which doesn't have the `strict_types` declaration (callback called from WP) will still be juggled, which again leads to bugs.
* Adding type declarations in functions hooked into WP filters is especially dangerous due to the loss of type info.

All in all, with the type system offered by PHP as-is, adding PHP native type declarations is next to useless unless you have full control of all code + the environment the code is being run on, which for the Yoast code is just not the case (with the exception of Platform).

Related to 303

Impact on Yoast packages:

| Plugin/Tool       | Errors/Warnings |
|-------------------|-----------------|
| PHPUnit Polyfills | 22
| WP Test Utils     | 9
| YoastCS           | --
| WHIP              | 5
| Yoast Test Helper | 21
| Duplicate Post    | 44
| Yst ACF           | 5
| Yst WooCommerce   | 48
| Yst News          | 22
| Yst Local         | 132
| Yst Video         | 237
| Yst Premium       | 269
| Yst Free          | 1066
An exception is made for the tests, as a data provider for input validation within a function could well be passing every single different type, in which case, `mixed` is perfectly valid.

Related to 303

Fixes 196

Impact on Yoast packages:

| Plugin/Tool       | Errors/Warnings |
|-------------------|-----------------|
| PHPUnit Polyfills | 49 (this is valid though as these are assertion declarations)
| WP Test Utils     | --
| YoastCS           | 2
| WHIP              | --
| Yoast Test Helper | 4
| Duplicate Post    | 12
| Yst ACF           | 1
| Yst WooCommerce   | 2
| Yst News          | 1
| Yst Local         | 42
| Yst Video         | 12
| Yst Premium       | 17
| Yst Free          | 151
@coveralls
Copy link

Coverage Status

coverage: 99.411%. remained the same
when pulling 5b93977 on JRF/yoastcs-more-docblock-comment-rules
into 89ffdf4 on develop.

@jrfnl jrfnl merged commit f209bca into develop Dec 14, 2023
28 checks passed
@jrfnl jrfnl deleted the JRF/yoastcs-more-docblock-comment-rules branch December 14, 2023 02:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add a sniff to disallow @return type mixed in docs
2 participants