|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Phauthentic\CognitiveCodeAnalysis\Business\Halstead; |
| 6 | + |
| 7 | +/** |
| 8 | + * @SuppressWarnings("PHPMD.ShortVariable") |
| 9 | + */ |
| 10 | +class HalsteadMetricsCalculator implements HalsteadMetricsCalculatorInterface |
| 11 | +{ |
| 12 | + /** |
| 13 | + * Calculate complete Halstead metrics for given operators and operands. |
| 14 | + * |
| 15 | + * @param array<string> $operators Array of operator types |
| 16 | + * @param array<string> $operands Array of operand values |
| 17 | + * @param string $identifier Identifier for the metrics (class name or method FQN) |
| 18 | + * @return array<string, mixed> Array containing all Halstead metrics |
| 19 | + */ |
| 20 | + public function calculateMetrics(array $operators, array $operands, string $identifier): array |
| 21 | + { |
| 22 | + // Step 1: Count distinct and total occurrences of operators and operands |
| 23 | + $distinctOperators = count(array_unique($operators)); |
| 24 | + $distinctOperands = count(array_unique($operands)); |
| 25 | + $totalOperators = count($operators); |
| 26 | + $totalOperands = count($operands); |
| 27 | + |
| 28 | + // Step 2: Calculate basic metrics |
| 29 | + $programLength = $this->calculateProgramLength($totalOperators, $totalOperands); |
| 30 | + $programVocabulary = $this->calculateProgramVocabulary($distinctOperators, $distinctOperands); |
| 31 | + |
| 32 | + // Step 3: Calculate advanced metrics |
| 33 | + $volume = $this->calculateVolume($programLength, $programVocabulary); |
| 34 | + $difficulty = $this->calculateDifficulty($distinctOperators, $totalOperands, $distinctOperands); |
| 35 | + $effort = $difficulty * $volume; |
| 36 | + |
| 37 | + // Step 4: Prepare the results array |
| 38 | + return [ |
| 39 | + 'n1' => $distinctOperators, |
| 40 | + 'n2' => $distinctOperands, |
| 41 | + 'N1' => $totalOperators, |
| 42 | + 'N2' => $totalOperands, |
| 43 | + 'programLength' => $programLength, |
| 44 | + 'programVocabulary' => $programVocabulary, |
| 45 | + 'volume' => $volume, |
| 46 | + 'difficulty' => $difficulty, |
| 47 | + 'effort' => $effort, |
| 48 | + 'fqName' => $identifier, |
| 49 | + ]; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Calculate the program length. |
| 54 | + * |
| 55 | + * @param int $N1 The total occurrences of operators |
| 56 | + * @param int $N2 The total occurrences of operands |
| 57 | + * @return int The program length |
| 58 | + */ |
| 59 | + public function calculateProgramLength(int $N1, int $N2): int |
| 60 | + { |
| 61 | + return $N1 + $N2; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Calculate the program vocabulary. |
| 66 | + * |
| 67 | + * @param int $n1 The count of distinct operators |
| 68 | + * @param int $n2 The count of distinct operands |
| 69 | + * @return int The program vocabulary |
| 70 | + */ |
| 71 | + public function calculateProgramVocabulary(int $n1, int $n2): int |
| 72 | + { |
| 73 | + return $n1 + $n2; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Calculate the volume of the program. |
| 78 | + * |
| 79 | + * @param int $programLength The length of the program |
| 80 | + * @param int $programVocabulary The vocabulary of the program |
| 81 | + * @return float The volume of the program |
| 82 | + */ |
| 83 | + public function calculateVolume(int $programLength, int $programVocabulary): float |
| 84 | + { |
| 85 | + if ($programVocabulary <= 0) { |
| 86 | + return 0.0; |
| 87 | + } |
| 88 | + return $programLength * log($programVocabulary, 2); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Calculate the difficulty of the program. |
| 93 | + * |
| 94 | + * @param int $n1 The count of distinct operators |
| 95 | + * @param int $N2 The total occurrences of operands |
| 96 | + * @param int $n2 The count of distinct operands |
| 97 | + * @return float The difficulty of the program |
| 98 | + */ |
| 99 | + public function calculateDifficulty(int $n1, int $N2, int $n2): float |
| 100 | + { |
| 101 | + if ($n2 === 0) { |
| 102 | + return 0.0; |
| 103 | + } |
| 104 | + return ($n1 / 2) * ($N2 / $n2); |
| 105 | + } |
| 106 | +} |
0 commit comments