From 8457fd689e1239a83e828a843616d8f9f20b787a Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Fri, 7 Jun 2024 13:25:51 -0300 Subject: [PATCH] Generic/DisallowYodaConditions: add code coverage ignore annotation This commit adds the `@codeCoverageIgnoreStart/@codeCoverageIgnoreEnd` PHPUnit annotations to ignore, for the purposes of code coverage, a line that can't be tested. I considered doing this initially when I worked on improving code coverage for this sniff in #465, but I wrongly assumed that was not an option due to the `Squiz.Commenting.InlineComment.InvalidEndChar` error (which is part of the standard used by PHPCS). What I failed to notice back then is that I was adding the annotation below a comment, and this triggers the error: ``` // Shouldn't be possible but may happen if external sniffs are using this method. // @codeCoverageIgnoreStart ``` Adding the annotation before the comment as it is done in this commit does not trigger the error because the `Squiz.Commenting.InlineComment` sniff will only trigger the `InvalidEndChar` error if the first character of the comment is a letter. Using `return true; // @codeCoverageIgnore` is not an option because it triggers `Squiz.Commenting.PostStatementComment.Found`. --- .../Sniffs/ControlStructures/DisallowYodaConditionsSniff.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Standards/Generic/Sniffs/ControlStructures/DisallowYodaConditionsSniff.php b/src/Standards/Generic/Sniffs/ControlStructures/DisallowYodaConditionsSniff.php index 3dc7795b37..ee19817693 100644 --- a/src/Standards/Generic/Sniffs/ControlStructures/DisallowYodaConditionsSniff.php +++ b/src/Standards/Generic/Sniffs/ControlStructures/DisallowYodaConditionsSniff.php @@ -145,8 +145,10 @@ public function isArrayStatic(File $phpcsFile, $arrayToken) $start = $tokens[$arrayToken]['parenthesis_opener']; $end = $tokens[$arrayToken]['parenthesis_closer']; } else { + // @codeCoverageIgnoreStart // Shouldn't be possible but may happen if external sniffs are using this method. return true; + // @codeCoverageIgnoreEnd } $staticTokens = Tokens::$emptyTokens;