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

Various updates to enum sniff fixes #1

Merged
merged 13 commits into from
Jan 19, 2022

Conversation

jrfnl
Copy link

@jrfnl jrfnl commented Jan 18, 2022

As discussed upstream, here are some (mostly) minor tweaks for PR squizlabs#3482

Feel free to squash these fixes into your original commits for a clean history.

Notes:

  • Most are just small test improvements to add additional safeguards.
  • The new test in Squiz.Commenting.InlineComment was breaking a test which had to be at the end of the file. Fixed by moving the new test up.
  • The new test in Squiz.WhiteSpace.ControlStructureSpacing wasn't actually testing anything. Fixed by wrapping it in a control structure.
  • The test improvements involving "valid name" sniffs will, in most cases, now cause the tests to fail. This is due to bug Name of typed enum tokenized as T_GOTO_LABEL squizlabs/PHP_CodeSniffer#3534, which was discovered during this review. The tests will pass once the upstream bugfix PR has been merged.
  • The Squiz.Classes.ValidClassName sniff needed an additional fix to be able to handle backed enums.

* Test case file 1 should contain both a duplicate and a non-duplicate enum.
* Test case file 2 should contain an enum which was already declared in file 1 to verify cross-file detection.
Ensure that all aspects of the sniff are tested for enums.

While these tests pass, if you look at the actual error message output, you will see some weirdness (declaration name for the `enum` set as `string`).
This should be fixed once PR 3534 has been merged.
Ensure that all aspects of the sniff are tested for enums.

While these tests pass, if you look at the actual error message output, you will see some weirdness (declaration name for the `enum` set as `string`).
This should be fixed once PR 3534 has been merged.
Ensure that backed enum signatures are included in the tests.

Note: the adjusted tests _without a space between the enum name and the colon_ will fail until PR 3534 has been merged.
Test more aspects of the sniff are applied correctly to enums.
The enum test was placed _below_ a test which was marked as testing a specific situation at the end of a file, which invalidated the "end of file" test.

Fixed by moving the new test up.
Add a _valid_ test case for a backed enum.

Note: this new test will fail until PR 3534 has been merged.
Let's also test the implements keyword when used with an enum.
Ensure that backed enum signatures are included in the tests.

As this sniff retrieves the class name itself instead of using the `File::getDeclarationName()` method, these tests _will_ find the name correctly (not mistake the type for the name), but _may_ include the colon in the name as the "name end" determination checks on scope opener and whitespace and doesn't take the colon for the type declaration into account.
Only use tabs when in the test cases when they serve a specific purpose.
Let's also test that `$this` in a non-static enum method doesn't get flagged.
The test as-it-was, wasn't actually testing anything. As the sniff is about control structures, the `enum` needs to be nested in one before the test would fail without the fix,
@kukulich kukulich merged commit 18b107f into kukulich:php81-enum Jan 19, 2022
@jrfnl jrfnl deleted the feature/update-enum-sniff-fixes branch April 15, 2022 07:41
kukulich pushed a commit that referenced this pull request Apr 22, 2022
The `Util\Standards::getInstalledStandardPath()` method returns either a string or `null`, however the `strpos()` method which is used in both the `Util\Common::isPharFile()` method, as well as in the follow-on condition, only accepts a `string` as `$haystack`.

As of PHP 8.1, this will generate a deprecation notice `strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated`.

Discovered while testing an external standard against PHPCS `master` on PHP 8.1.

Fixed now.
kukulich pushed a commit that referenced this pull request Apr 22, 2022
On PHP 8.1, passing `null` to `strtolower()` generates a `strtolower(): Passing null to parameter #1 ($string) of type string is deprecated` notice.

The `File::getDeclarationName()` will return `null` for anonymous classes, so we need an extra safeguard here.

This fix is covered by the existing unit tests and was exposed when running the tests on PHP 8.1 with `error_reporting` set to `E_ALL`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants