function NestingLevelSniff::process
Processes this test, when one of its tokens is encountered.
Parameters
\PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.:
int $stackPtr The position of the current token: in the stack passed in $tokens.
Return value
void
Overrides Sniff::process
File
-
vendor/
squizlabs/ php_codesniffer/ src/ Standards/ Generic/ Sniffs/ Metrics/ NestingLevelSniff.php, line 55
Class
Namespace
PHP_CodeSniffer\Standards\Generic\Sniffs\MetricsCode
public function process(File $phpcsFile, $stackPtr) {
$tokens = $phpcsFile->getTokens();
// Ignore abstract methods.
if (isset($tokens[$stackPtr]['scope_opener']) === false) {
return;
}
// Detect start and end of this function definition.
$start = $tokens[$stackPtr]['scope_opener'];
$end = $tokens[$stackPtr]['scope_closer'];
$nestingLevel = 0;
// Find the maximum nesting level of any token in the function.
for ($i = $start + 1; $i < $end; $i++) {
$level = $tokens[$i]['level'];
if ($nestingLevel < $level) {
$nestingLevel = $level;
}
}
// We subtract the nesting level of the function itself.
$nestingLevel = $nestingLevel - $tokens[$stackPtr]['level'] - 1;
if ($nestingLevel > $this->absoluteNestingLevel) {
$error = 'Function\'s nesting level (%s) exceeds allowed maximum of %s';
$data = [
$nestingLevel,
$this->absoluteNestingLevel,
];
$phpcsFile->addError($error, $stackPtr, 'MaxExceeded', $data);
}
else {
if ($nestingLevel > $this->nestingLevel) {
$warning = 'Function\'s nesting level (%s) exceeds %s; consider refactoring the function';
$data = [
$nestingLevel,
$this->nestingLevel,
];
$phpcsFile->addWarning($warning, $stackPtr, 'TooHigh', $data);
}
}
}