| Total Complexity | 8 |
| Total Lines | 76 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 10 | class CyclomaticComplexityAssessor |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * @var array<int, int> |
||
| 14 | */ |
||
| 15 | private $tokens = [ |
||
| 16 | \T_CLASS => 1, |
||
| 17 | \T_INTERFACE => 1, |
||
| 18 | \T_TRAIT => 1, |
||
| 19 | \T_IF => 1, |
||
| 20 | \T_ELSEIF => 1, |
||
| 21 | \T_FOR => 1, |
||
| 22 | \T_FOREACH => 1, |
||
| 23 | \T_WHILE => 1, |
||
| 24 | \T_CASE => 1, |
||
| 25 | \T_CATCH => 1, |
||
| 26 | \T_BOOLEAN_AND => 1, |
||
| 27 | \T_LOGICAL_AND => 1, |
||
| 28 | \T_BOOLEAN_OR => 1, |
||
| 29 | \T_LOGICAL_OR => 1, |
||
| 30 | \T_COALESCE => 1, |
||
| 31 | ]; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Class constructor. |
||
| 35 | */ |
||
| 36 | public function __construct() |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Assess the files cyclomatic complexity. |
||
| 43 | * |
||
| 44 | * @param string $contents The contents of a PHP file. |
||
| 45 | */ |
||
| 46 | public function assess(string $contents): int |
||
| 47 | { |
||
| 48 | $score = 0; |
||
| 49 | foreach (\token_get_all($contents) as $token) { |
||
| 50 | $score += $this->getComplexity($token[0]); |
||
| 51 | } |
||
| 52 | |||
| 53 | return \max(1, $score); |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Add missing tokens depending on the PHP version. |
||
| 58 | */ |
||
| 59 | private function init(): void |
||
| 73 | } |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @param integer|string $code Code of a PHP token. |
||
| 78 | */ |
||
| 79 | private function getComplexity($code): int |
||
| 88 |