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; } diff --git a/src/Standards/Generic/Tests/Metrics/NestingLevelUnitTest.inc b/src/Standards/Generic/Tests/Metrics/NestingLevelUnitTest.1.inc similarity index 68% rename from src/Standards/Generic/Tests/Metrics/NestingLevelUnitTest.inc rename to src/Standards/Generic/Tests/Metrics/NestingLevelUnitTest.1.inc index 9708792418..3f1dd92617 100644 --- a/src/Standards/Generic/Tests/Metrics/NestingLevelUnitTest.inc +++ b/src/Standards/Generic/Tests/Metrics/NestingLevelUnitTest.1.inc @@ -27,16 +27,16 @@ function nestingFive() function nestingSix() { if ($condition) { - echo 'hi'; - switch ($condition) - { + } else { + switch ($condition) { case '1': if ($condition === '1') { - if ($cond) { + } elseif ($condition === '2') { + do { foreach ($conds as $cond) { echo 'hi'; } - } + } while ($cond > 5); } break; } @@ -79,19 +79,19 @@ function nestingEleven() case '1': if ($condition === '1') { if ($cond) { - switch ($cond) { - case '1': - if ($cond === '1') { - foreach ($conds as $cond) { - if ($cond === 'hi') { - if ($cond !== 'bye') { - echo 'hi'; - } + try { + if ( $cond === '1' ) { + for ( $i = 0; $i < 10; $i ++ ) { + while ($i < 5) { + if ( $cond === 'hi' ) { + match ( $cond ) { + 'hi' => 'something', + }; } } } - break; - } + } + } catch (Exception $e) {} } } break; @@ -99,4 +99,10 @@ function nestingEleven() } } -?> +abstract class AbstractClass { + abstract public function sniffShouldIgnoreAbstractMethods(); +} + +interface MyInterface { + public function sniffShouldIgnoreInterfaceMethods(); +} diff --git a/src/Standards/Generic/Tests/Metrics/NestingLevelUnitTest.2.inc b/src/Standards/Generic/Tests/Metrics/NestingLevelUnitTest.2.inc new file mode 100644 index 0000000000..9658af3005 --- /dev/null +++ b/src/Standards/Generic/Tests/Metrics/NestingLevelUnitTest.2.inc @@ -0,0 +1,7 @@ + */ - public function getErrorList() + public function getErrorList($testFile='') { - return [73 => 1]; + switch ($testFile) { + case 'NestingLevelUnitTest.1.inc': + return [73 => 1]; + default: + return []; + } }//end getErrorList() @@ -41,14 +48,21 @@ public function getErrorList() * The key of the array should represent the line number and the value * should represent the number of warnings that should occur on that line. * + * @param string $testFile The name of the test file to process. + * * @return array */ - public function getWarningList() + public function getWarningList($testFile='') { - return [ - 27 => 1, - 46 => 1, - ]; + switch ($testFile) { + case 'NestingLevelUnitTest.1.inc': + return [ + 27 => 1, + 46 => 1, + ]; + default: + return []; + } }//end getWarningList()