Skip to content

Commit

Permalink
Added maximum 5 CyclomaticComplexity for alowing no DocBlock
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobBrownAustin committed Oct 4, 2023
1 parent 83c8668 commit b442f9a
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion Magento2/Sniffs/Annotation/MethodArgumentsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class MethodArgumentsSniff implements Sniff
'self'
];

private const MAXIMUM_COMPLEXITY_ALLOWED_FOR_NO_DOCBLOCK = 5;

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -649,7 +651,8 @@ private function checkIfMethodNeedsDocBlock(File $phpcsFile, $stackPtr) : bool
return true;
}
}
return false;
$complexity = $this->getMethodComplexity($phpcsFile, $stackPtr);
return $complexity > static::MAXIMUM_COMPLEXITY_ALLOWED_FOR_NO_DOCBLOCK;
}

/**
Expand All @@ -670,6 +673,42 @@ private function checkIfNamespaceContainsApi(File $phpcsFile) : bool
return false;
}

/**
* Get method CyclomaticComplexity
*
* @param File $phpcsFile
* @param int $stackPtr
* @return int
*/
private function getMethodComplexity(File $phpcsFile, $stackPtr) : int
{
$tokens = $phpcsFile->getTokens();
$start = $tokens[$stackPtr]['scope_opener'];
$end = $tokens[$stackPtr]['scope_closer'];
$predicateNodes = [
T_CASE => true,
T_DEFAULT => true,
T_CATCH => true,
T_IF => true,
T_FOR => true,
T_FOREACH => true,
T_WHILE => true,
T_ELSEIF => true,
T_INLINE_THEN => true,
T_COALESCE => true,
T_COALESCE_EQUAL => true,
T_MATCH_ARROW => true,
T_NULLSAFE_OBJECT_OPERATOR => true,
];
$complexity = 1;
for ($stackPtr = $start + 1; $stackPtr < $end; $stackPtr++) {
if (isset($predicateNodes[$tokens[$stackPtr]['code']])) {
$complexity++;
}
}
return $complexity;
}

/**
* Validates function params format consistency.
*
Expand Down

0 comments on commit b442f9a

Please sign in to comment.