PHPCSStandards / PHP_CodeSniffer

PHP_CodeSniffer tokenizes PHP files and detects violations of a defined set of coding standards.
BSD 3-Clause "New" or "Revised" License
856 stars 53 forks source link

Generic/DisallowYodaConditions: improve code coverage #465

Closed rodrigoprimo closed 3 months ago

rodrigoprimo commented 4 months ago

Description

This PR improves code coverage for the Generic.ControlStructures.DisallowYodaConditions sniff. It adds a few tests for uncovered lines and documents using tests a few comparison operators that this sniff listens for that were not part of the tests before.

Besides that, this PR removes an unused variable, fixes a typo in a code comment, removes two unreachable variables, and renames a variable to make it more clear what it contains.

It is impossible to add code coverage for the line below using the current test framework and we should not remove it as external sniffs could be using the DisallowYodaConditionsSniff::isArrayStatic() and relying on the current behavior (returning true if the passed token index is not an array):

https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/30b3148b302aaba28d72bdb2432b144ff9d47751/src/Standards/Generic/Sniffs/ControlStructures/DisallowYodaConditionsSniff.php#L152

I left a comment documenting that the line cannot be covered by tests but cannot be removed as well.

I considered using // @codeCoverageIgnore or // @codeCoverageIgnoreStart and // @codeCoverageIgnoreEnd, but that is not possible as well as those annotations trigger different sniff errors for the coding standard used by PHPCS (Squiz.Commenting.PostStatementComment.Found and Squiz.Commenting.InlineComment.InvalidEndChar). I will open a separate issue for this.

Related issues/external references

Part of https://github.com/PHPCSStandards/PHP_CodeSniffer/issues/146

Types of changes

PR checklist

jrfnl commented 3 months ago

I've rebased the PR without changes (other than re-organizing the commits, but no changes to the code). Merging once the build has passed.