/** * Calculates Maintainability Index * * @param \Hal\Metrics\Complexity\Text\Halstead\Result $rHalstead * @param \Hal\Metrics\Complexity\Text\Length\Result $rLoc * @param \Hal\Metrics\Complexity\Component\McCabe\Result $rMcCabe * @return Result */ public function calculate(\Hal\Metrics\Complexity\Text\Halstead\Result $rHalstead, \Hal\Metrics\Complexity\Text\Length\Result $rLoc, \Hal\Metrics\Complexity\Component\McCabe\Result $rMcCabe) { $result = new Result(); $result->setMaintainabilityIndexWithoutComment(max((171 - 5.2 * \log($rHalstead->getVolume()) - 0.23 * $rMcCabe->getCyclomaticComplexityNumber() - 16.2 * \log($rLoc->getLogicalLoc())) * 100 / 171, 0)); // comment weight if ($rLoc->getLoc() > 0) { $CM = $rLoc->getCommentLoc() / $rLoc->getLoc(); $result->setCommentWeight(50 * sin(sqrt(2.4 * $CM))); } return $result; }
public function testHalsteadResultCanBeConvertedToArray() { $result = new Result(); $array = $result->asArray(); $this->assertArrayHasKey('volume', $array); $this->assertArrayHasKey('length', $array); $this->assertArrayHasKey('vocabulary', $array); $this->assertArrayHasKey('effort', $array); $this->assertArrayHasKey('difficulty', $array); $this->assertArrayHasKey('time', $array); $this->assertArrayHasKey('bugs', $array); }
/** * Calculate Halstead metrics * * @param TokenCollection $tokens * @return Result */ public function calculate($tokens) { $this->inventory($tokens); $result = new Result(); $uniqueOperators = array_map('unserialize', array_unique(array_map('serialize', $this->operators))); $uniqueOperands = array_map('unserialize', array_unique(array_map('serialize', $this->operands))); $n1 = sizeof($uniqueOperators, COUNT_NORMAL); $n2 = sizeof($uniqueOperands, COUNT_NORMAL); $N1 = sizeof($this->operators, COUNT_NORMAL); $N2 = sizeof($this->operands, COUNT_NORMAL); if ($n2 == 0 || $N2 == 0 || $n2 == 2) { // files without operators $V = $n1 = $n2 = $N1 = $N2 = $E = $D = $B = $T = $I = $L = 0; } else { $devAbility = 3000; $N = $N1 + $N2; $n = $n1 + $n2; $V = $N * log($n, 2); $L = 2 / max(1, $n1) * ($n2 / $N2); $D = $n1 / 2 * ($N2 / $n2); $E = $V * $D; $B = $V / $devAbility; $T = $E / 18; $I = $L * $V; } $result->setLength($N1 + $N2)->setVocabulary($n1 + $n2)->setVolume(round($V, 2))->setDifficulty(round($D, 2))->setEffort(round($E, 2))->setLevel(round($L, 2))->setBugs(round($B, 2))->setTime(round($T))->setIntelligentContent(round($I, 2))->setNumberOfOperators($N1)->setNumberOfOperands($N2)->setNumberOfUniqueOperators($n1)->setNumberOfUniqueOperands($n2); return $result; }