From d8b51334dee18ea3ab104dc9855e3bd22840ff1f Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Mon, 9 Dec 2024 14:40:52 -0300 Subject: [PATCH] Generic/NestingLevel: 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" (#684 (comment)). 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. This commit also updates the related code comment to better reflect what the if condition does. --- src/Standards/Generic/Sniffs/Metrics/NestingLevelSniff.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Standards/Generic/Sniffs/Metrics/NestingLevelSniff.php b/src/Standards/Generic/Sniffs/Metrics/NestingLevelSniff.php index 3c086c7fe4..d2672b5eeb 100644 --- a/src/Standards/Generic/Sniffs/Metrics/NestingLevelSniff.php +++ b/src/Standards/Generic/Sniffs/Metrics/NestingLevelSniff.php @@ -56,8 +56,8 @@ public function process(File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); - // Ignore abstract methods. - if (isset($tokens[$stackPtr]['scope_opener']) === false) { + // Ignore abstract and interface methods. Bail early when live coding. + if (isset($tokens[$stackPtr]['scope_opener'], $tokens[$stackPtr]['scope_closer']) === false) { return; }