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

test(phpcs): Check for unused variables in PHPCS itself #446

Merged
merged 7 commits into from
Jun 5, 2024

Conversation

klausi
Copy link
Contributor

@klausi klausi commented Apr 13, 2024

I found an used variable in src/Standards/Squiz/Sniffs/Functions/MultiLineFunctionDeclarationSniff.php . We should check for unused variables with automated testing.

Description

We can check for unused variables with https://github.com/slevomat/coding-standard/blob/master/doc/variables.md#slevomatcodingstandardvariablesunusedvariable .

Not sure if you want to have a dependency on Slevomat for development? This pull request demonstrates a possibility for the approach, but I did not finish fixing all unused variables yet.

Let me know if this direction makes sense and then I can fix them!

Suggested changelog entry

not needed, as this is an internal fix and will not be visible for users.

Related issues/external references

Types of changes

Bug fix

PR checklist

  • I have checked there is no other PR open for the same change.
  • I have read the Contribution Guidelines.
  • I grant the project the right to include and distribute the code under the BSD-3-Clause license (and I have the right to grant these rights).
  • I have added tests to cover my changes.
  • I have verified that the code complies with the projects coding standards.
  • [Required for new sniffs] I have added XML documentation for the sniff.
  • [Required for new files] I have added any new files to the package.xml file.

@klausi klausi changed the title test(phcs): Check for unused variables in PHPCS it self test(phcs): Check for unused variables in PHPCS itself Apr 13, 2024
@jrfnl
Copy link
Member

jrfnl commented Apr 13, 2024

@klausi Thanks for this PR, I can accept the code changes, but not the change to CI/dependency addition.

@klausi
Copy link
Contributor Author

klausi commented Apr 14, 2024

OK, I assume you also don't want to fork the Slevomat sniff into PHPCS to avoid the dependency?

Anyway, I could setup an external CI to check the PHPCS master branch once per day with cron and then send you manual pull requests whenever new unused variables are introduced by accident. It should not happen often anyway.

I will fix the reported instances first to see if there are any false positives.

src/Files/File.php Outdated Show resolved Hide resolved
@jrfnl
Copy link
Member

jrfnl commented Apr 14, 2024

OK, I assume you also don't want to fork the Slevomat sniff into PHPCS to avoid the dependency?

Correct, see the CONTRIBUTING guide on copyright.

Aynway, I could setup an external CI to check the PHPCS master branch once per day with cron and then send you manual pull requests whenever new unused variables are introduced by accident. It should not happen often anyway.

I appreciate the offer, but no need, I can add it to my local phpcs.xml overload file which contains various other extra sniffs as well and based on which I once in a while send in a clean up PR, like #339. I think that should be enough.

I will fix the reported instances first to see if there are any false positives.

Appreciated, I'll have a look. One word of warning: please be careful, not all code is covered by tests.

@klausi
Copy link
Contributor Author

klausi commented Apr 19, 2024

Here is the Slevomat config I used to verify that all unused variables are fixed: klausi@3386955

Copy link
Member

@jrfnl jrfnl left a comment

Choose a reason for hiding this comment

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

Thanks for updating the PR @klausi . I've left various comments inline to have a look at. For now, I tend to err on the side of caution with some things as - especially on the framework side - there are significant gaps in tests/documentation, which may make certain changes more risky and others undesirable as they may lead to more detective work being needed at a later point in time.

Let me know what you think.

src/Reports/Full.php Outdated Show resolved Hide resolved
src/Tokenizers/CSS.php Outdated Show resolved Hide resolved
src/Util/Common.php Outdated Show resolved Hide resolved
src/Runner.php Show resolved Hide resolved
tests/Standards/AbstractSniffUnitTest.php Outdated Show resolved Hide resolved
tests/bootstrap.php Outdated Show resolved Hide resolved
@jrfnl
Copy link
Member

jrfnl commented May 16, 2024

@klausi Had a chance to gather your thought on my remarks yet ?

@klausi
Copy link
Contributor Author

klausi commented May 16, 2024

Sorry, got busy with other things, but your feedback looks good! Will work it in when I have time again.

@jrfnl
Copy link
Member

jrfnl commented May 16, 2024

@klausi Take your time. I just wanted to check in.

@klausi
Copy link
Contributor Author

klausi commented May 31, 2024

I'm done with the reverts, let me know if you need anything else!

Copy link
Member

@jrfnl jrfnl left a comment

Choose a reason for hiding this comment

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

@klausi Thank you for working on this. All looking good now.

I've opened up issue #521 as a follow up and will open a couple of PRs with follow ups as well (related to some of the reverts).

@jrfnl jrfnl merged commit d140424 into PHPCSStandards:master Jun 5, 2024
40 checks passed
jrfnl pushed a commit that referenced this pull request Jun 5, 2024
* test(phcs): Check for unused variables in PHPCS it self

* Manually fixed all unused varaibles

* Revert Slevomat dependency

* Revert array_keys changes

* revert changes that should stay
jrfnl added a commit that referenced this pull request Jun 5, 2024
Add initial set of tests for the `Common::getSniffCode()` method.

Related to 146
Related to [review comment in PR 446](#446 (comment)).
jrfnl added a commit that referenced this pull request Jun 5, 2024
* Remove the use of `array_pop()` in favour of directly referencing the required "parts" by their index in the array.
* Remove the unused `$sniffDir` variable.
* Remove the unnecessary `$code` variable.

Related to [review comment in PR 446](#446 (comment)).
jrfnl added a commit that referenced this pull request Jun 5, 2024
* Remove the use of `array_pop()` in favour of directly referencing the required "parts" by their index in the array.
* Remove the unused `$sniffDir` variable.
* Remove the unnecessary `$code` variable.

Related to [review comment in PR 446](#446 (comment)).
jrfnl added a commit that referenced this pull request Jun 5, 2024
* Remove the use of `array_pop()` in favour of directly referencing the required "parts" by their index in the array.
* Remove the unused `$sniffDir` variable.
* Remove the unnecessary `$code` variable.

Related to [review comment in PR 446](#446 (comment)).
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.

None yet

2 participants