From 92e5a9a6aebcd07bc54aef63403b3cc1cd8fe54e Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Mon, 2 Dec 2024 10:02:06 -0300 Subject: [PATCH] Generic/CyclomaticComplexity: ensure the sniff bails if `scope_closer` is not set Quoting @jrfnl: "Only checking for the scope_opener, when accessing/using both the scope_opener and scope_closer indexes, is probably fine in practical terms. However, the part of the code base which sets these indexes is not sufficiently covered by tests, nor does it document that those indexes will only be set if both can be set, so there may be edge case exceptions" (https://github.com/PHPCSStandards/PHP_CodeSniffer/pull/684#discussion_r1865313147). The sniff was already working fine before this change. Checking if `scope_closer` is just an extra precaution to err on the side of caution. --- .../Generic/Sniffs/Metrics/CyclomaticComplexitySniff.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Standards/Generic/Sniffs/Metrics/CyclomaticComplexitySniff.php b/src/Standards/Generic/Sniffs/Metrics/CyclomaticComplexitySniff.php index c37140a990..a6b17d1315 100644 --- a/src/Standards/Generic/Sniffs/Metrics/CyclomaticComplexitySniff.php +++ b/src/Standards/Generic/Sniffs/Metrics/CyclomaticComplexitySniff.php @@ -61,7 +61,7 @@ public function process(File $phpcsFile, $stackPtr) $tokens = $phpcsFile->getTokens(); // Ignore abstract and interface methods. Bail early when live coding. - if (isset($tokens[$stackPtr]['scope_opener']) === false) { + if (isset($tokens[$stackPtr]['scope_opener'], $tokens[$stackPtr]['scope_closer']) === false) { return; }