From b442f9a964b585f8450aef786ccfce6117379a61 Mon Sep 17 00:00:00 2001 From: Jacob Brown Date: Wed, 4 Oct 2023 14:58:12 -0500 Subject: [PATCH] Added maximum 5 CyclomaticComplexity for alowing no DocBlock --- .../Annotation/MethodArgumentsSniff.php | 41 ++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/Magento2/Sniffs/Annotation/MethodArgumentsSniff.php b/Magento2/Sniffs/Annotation/MethodArgumentsSniff.php index e272fd22..f8b1e675 100644 --- a/Magento2/Sniffs/Annotation/MethodArgumentsSniff.php +++ b/Magento2/Sniffs/Annotation/MethodArgumentsSniff.php @@ -38,6 +38,8 @@ class MethodArgumentsSniff implements Sniff 'self' ]; + private const MAXIMUM_COMPLEXITY_ALLOWED_FOR_NO_DOCBLOCK = 5; + /** * @inheritdoc */ @@ -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; } /** @@ -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. *