Skip to content

Commit b442f9a

Browse files
Added maximum 5 CyclomaticComplexity for alowing no DocBlock
1 parent 83c8668 commit b442f9a

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

Magento2/Sniffs/Annotation/MethodArgumentsSniff.php

+40-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ class MethodArgumentsSniff implements Sniff
3838
'self'
3939
];
4040

41+
private const MAXIMUM_COMPLEXITY_ALLOWED_FOR_NO_DOCBLOCK = 5;
42+
4143
/**
4244
* @inheritdoc
4345
*/
@@ -649,7 +651,8 @@ private function checkIfMethodNeedsDocBlock(File $phpcsFile, $stackPtr) : bool
649651
return true;
650652
}
651653
}
652-
return false;
654+
$complexity = $this->getMethodComplexity($phpcsFile, $stackPtr);
655+
return $complexity > static::MAXIMUM_COMPLEXITY_ALLOWED_FOR_NO_DOCBLOCK;
653656
}
654657

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

676+
/**
677+
* Get method CyclomaticComplexity
678+
*
679+
* @param File $phpcsFile
680+
* @param int $stackPtr
681+
* @return int
682+
*/
683+
private function getMethodComplexity(File $phpcsFile, $stackPtr) : int
684+
{
685+
$tokens = $phpcsFile->getTokens();
686+
$start = $tokens[$stackPtr]['scope_opener'];
687+
$end = $tokens[$stackPtr]['scope_closer'];
688+
$predicateNodes = [
689+
T_CASE => true,
690+
T_DEFAULT => true,
691+
T_CATCH => true,
692+
T_IF => true,
693+
T_FOR => true,
694+
T_FOREACH => true,
695+
T_WHILE => true,
696+
T_ELSEIF => true,
697+
T_INLINE_THEN => true,
698+
T_COALESCE => true,
699+
T_COALESCE_EQUAL => true,
700+
T_MATCH_ARROW => true,
701+
T_NULLSAFE_OBJECT_OPERATOR => true,
702+
];
703+
$complexity = 1;
704+
for ($stackPtr = $start + 1; $stackPtr < $end; $stackPtr++) {
705+
if (isset($predicateNodes[$tokens[$stackPtr]['code']])) {
706+
$complexity++;
707+
}
708+
}
709+
return $complexity;
710+
}
711+
673712
/**
674713
* Validates function params format consistency.
675714
*

0 commit comments

Comments
 (0)