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

Arrays/ArrayKeySpacingRestrictions: implement PHPCSUtils and various bug fixes and other improvements #2075

Merged
merged 10 commits into from
Aug 12, 2022

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Aug 12, 2022

This PR is the result of a complete review of the WordPress.Arrays.ArrayKeySpacingRestrictions sniff.

I've basically gone through the complete sniff with a fine toothcomb with the following questions in the back of my mind:

  • What can we use from PHPCSUtils ?
  • Any changes needed to support modern PHP ?
  • Anything else I can think of which could/would break the sniff (false positives/negatives) ?

👉🏻 This PR will be easiest to review by looking at each commit individually.

Related to #1877

Commit details

Arrays/ArrayKeySpacingRestrictions: don't warn on parse error

Most sniffs will silently bow out on parse errors. This is the preferred behaviour as sniffs are often run in IDEs during live coding.

The ArrayKeySpacingRestrictions sniff would, however, throw a warning when a close bracket was missing.

This commit removes that warning.

This is in line with similar changes (no longer throwing warnings/errors on parse errors) which will be included in PHPCS 4.x upstream.

Note: there were no tests covering the warning, so the test files are untouched.

Refs:

Arrays/ArrayKeySpacingRestrictions: improve handling of brackets for array assignments without key

As it was, this was handled correctly, though the error message was unclear as it referred to "array keys", while these assignments don't have an explicit key.

To fix this, I'm now special casing array assignments without an explicit key.

The original error message in that situation was: "Array keys must NOT be surrounded by spaces if they only contain a string or an integer."
The new error message will be: "There should be no space between the square brackets for an array assignment without an explicit key. Found: ..."

The new error message will also have a different, new error code: SpacesBetweenBrackets.
Note: the new error code could be considered a breaking change, though I doubt many people will notice this as this is not typically an error code which would be used in inline ignore annotations.

Includes unit tests.

Note: the behaviour for array assignments without an explicit key, but with a comment between the brackets, has not been changed. In that case, the original NoSpacesAroundArrayKeys error will still be thrown.

All the same, tests have been added to verify and safeguard this.

Arrays/ArrayKeySpacingRestrictions: tweak test order

... to more easily allow for additional tests for integer keys to be added.

Arrays/ArrayKeySpacingRestrictions: rename three local variables

... to make them more descriptive.

Arrays/ArrayKeySpacingRestrictions: bug fix - require spaces around calculations and allow for + sign

This commit fixes two bugs:

  1. Signed integers with a plus sign would be treated differently from signed integers with a minus sign.
    While using the plus sign for unary integers will be rare, it is valid and they should be treated the same as unary integers with a minus sign.
  2. When an array key consisted of a calculation with multiple integers and a minus sign, no spaces would be demanded around the array key, even though the key should be surrounded by spaces.

Both bugs have now been fixed by walking the tokens in the array key more precisely.

Includes unit tests covering both situations.

Includes minor tweaks to some conditions as the $needs_spaces variables will now always be boolean, so we can make the conditions in which the variable is being used more readable.

Arrays/ArrayKeySpacingRestrictions: add tests with non-decimal integers

.. to safeguard these are handled the same as decimal integers.

Arrays/ArrayKeySpacingRestrictions: minor simplifications

Simplify the fixer code for the "Array keys must [NOT] be surrounded by spaces" errors.

Arrays/ArrayKeySpacingRestrictions: implement the PHPCSUtils SpacesFixer

This implements use of the PHPCSUtils SpacesFixer class for checking and fixing the width of the whitespace inside the brackets.

While this doesn't change the effective behaviour of the sniff, it simplifies the code and improves the clarity of the error messages.
Note: the error codes do not change.

Old error messages:

 33 | ERROR | [x] There should be exactly one space before the array key. Found: 5
 33 | ERROR | [x] There should be exactly one space after the array key. Found: 4
 34 | ERROR | [x] There should be exactly one space before the array key. Found: newline
 35 | ERROR | [x] There should be exactly one space after the array key. Found: newline

New error messages:

 33 | ERROR | [x] There should be exactly 1 space before the array key. Found: 5 spaces
 33 | ERROR | [x] There should be exactly 1 space after the array key. Found: 4 spaces
 34 | ERROR | [x] There should be exactly 1 space before the array key. Found: a new line
 35 | ERROR | [x] There should be exactly 1 space after the array key. Found: multiple new lines

PHP 7.4 | Arrays/ArrayKeySpacingRestrictions: add tests with numeric literals with underscores

.. to safeguard these are handled correctly.

PHP 8.1 | Arrays/ArrayKeySpacingRestrictions: add tests with octal numeric literals

.. to safeguard these are handled correctly.

jrfnl added 10 commits August 12, 2022 11:22
Most sniffs will silently bow out on parse errors. This is the preferred behaviour as sniffs are often run in IDEs during live coding.

The `ArrayKeySpacingRestrictions` sniff would, however, throw a warning when a close bracket was missing.

This commit removes that warning.

This is in line with similar changes (no longer throwing warnings/errors on parse errors) which will be included in PHPCS 4.x upstream.

Note: there were no tests covering the warning, so the test files are untouched.

Refs:
* squizlabs/PHP_CodeSniffer 2455
…array assignments without key

As it was, this was handled correctly, though the error message was unclear as it referred to "array keys", while these assignments don't have an explicit key.

To fix this, I'm now special casing array assignments without an explicit key.

The original error message in that situation was: "Array keys must NOT be surrounded by spaces if they only contain a string or an integer."
The new error message will be: "There should be no space between the square brackets for an array assignment without an explicit key. Found: ..."

The new error message will also have a different, new error code: `SpacesBetweenBrackets`.
Note: the new error code could be considered a breaking change, though I doubt many people will notice this as this is not typically an error code which would be used in inline ignore annotations.

Includes unit tests.

Note: the behaviour for array assignments without an explicit key, but with a comment between the brackets, has not been changed. In that case, the original `NoSpacesAroundArrayKeys` error will still be thrown.

All the same, tests have been added to verify and safeguard this.
... to more easily allow for additional tests for integer keys to be added.
…alculations and allow for + sign

This commit fixes two bugs:
1. Signed integers with a plus sign would be treated differently from signed integers with a minus sign.
    While using the plus sign for unary integers will be rare, it is valid and they should be treated the same as unary integers with a minus sign.
2. When an array key consisted of a calculation with multiple integers and a minus sign, no spaces would be demanded around the array key, even though they key should be surrounded by spaces.

Both bugs have now been fixed by walking the tokens in the array key more precisely.

Includes unit tests covering both situations.

Includes minor tweaks to some conditions as the `$needs_spaces` variables will now always be boolean, so we can make the conditions in which the variable is being used more readable.
.. to safeguard these are handled the same as decimal integers.
Simplify the fixer code for the "Array keys must [NOT] be surrounded by spaces" errors.
This implements use of the PHPCSUtils `SpacesFixer` class for checking and fixing the width of the whitespace inside the brackets.

While this doesn't change the effective behaviour of the sniff, it simplifies the code and improves the clarity of the error messages.
Note: the error _codes_ do not change.

Old error messages:
```
 33 | ERROR | [x] There should be exactly one space before the array key. Found: 5
 33 | ERROR | [x] There should be exactly one space after the array key. Found: 4
 34 | ERROR | [x] There should be exactly one space before the array key. Found: newline
 35 | ERROR | [x] There should be exactly one space after the array key. Found: newline
```

New error messages:
```
 33 | ERROR | [x] There should be exactly 1 space before the array key. Found: 5 spaces
 33 | ERROR | [x] There should be exactly 1 space after the array key. Found: 4 spaces
 34 | ERROR | [x] There should be exactly 1 space before the array key. Found: a new line
 35 | ERROR | [x] There should be exactly 1 space after the array key. Found: multiple new lines
```
…literals with underscores

.. to safeguard these are handled correctly.
…meric literals

.. to safeguard these are handled correctly.
Copy link
Member

@dingo-d dingo-d left a comment

Choose a reason for hiding this comment

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

Love the SpacesFixer util, cleaned up the code a ton 😄

Copy link
Member

@GaryJones GaryJones left a comment

Choose a reason for hiding this comment

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

@GaryJones GaryJones merged commit df62c52 into develop Aug 12, 2022
@GaryJones GaryJones deleted the feature/arraykeyspacingrestrictions-improvements branch August 12, 2022 11:41
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.

3 participants