public function testICanConvertSystemComplexityResultToArray() { $result = new Result(); $array = $result->asArray(); $this->assertArrayHasKey('sysc', $array); $this->assertArrayHasKey('dc', $array); }
/** * Calculates Card And Agresti metric * * Fan-out = Structural fan-out = Number of other procedures this procedure calls * v = number of input/output variables for a procedure * * (SC) Structural complexity = fan-out^2 * (DC) Data complexity = v / (fan-out + 1) * * @param ReflectedClass $class * @return Result */ public function calculate(ReflectedClass $class) { $result = new Result(); $sy = $dc = $sc = array(); // in system foreach ($class->getMethods() as $method) { $fanout = sizeof($method->getDependencies(), COUNT_NORMAL); $v = sizeof($method->getArguments(), COUNT_NORMAL) + sizeof($method->getReturns(), COUNT_NORMAL); $ldc = $v / ($fanout + 1); $lsc = pow($fanout, 2); $sy[] = $ldc + $lsc; $dc[] = $ldc; $sc[] = $lsc; } $result->setRelativeStructuralComplexity(empty($sc) ? 0 : round(array_sum($sc) / sizeof($sc, COUNT_NORMAL), 2))->setRelativeDataComplexity(empty($dc) ? 0 : round(array_sum($dc) / sizeof($dc, COUNT_NORMAL), 2))->setRelativeSystemComplexity(empty($sy) ? 0 : round(array_sum($sy) / sizeof($sy, COUNT_NORMAL), 2))->setTotalStructuralComplexity(round(array_sum($sc), 2))->setTotalDataComplexity(round(array_sum($dc), 2))->setTotalSystemComplexity(round(array_sum($dc) + array_sum($sc), 2)); return $result; }
/** * Calculates system complexity * * @param string $filename * @return Result */ public function calculate($filename) { $rOOP = $this->classMap->getClassesIn($filename); $result = new Result($filename); $len = 0; $sy = $dc = $sc = array(); $calculator = new SystemComplexity(); foreach ($rOOP->getClasses() as $class) { $r = $calculator->calculate($class); $len += sizeof($class->getMethods(), COUNT_NORMAL); $sy[] = $r->getTotalSystemComplexity(); $dc[] = $r->getTotalDataComplexity(); $sc[] = $r->getTotalStructuralComplexity(); } if ($len > 0 && sizeof($dc, COUNT_NORMAL) > 0) { $result->setRelativeStructuralComplexity(round(array_sum($sc) / $len, 2))->setRelativeDataComplexity(round(array_sum($dc) / $len, 2))->setRelativeSystemComplexity(round(array_sum($sy) / $len, 2))->setTotalStructuralComplexity(array_sum($sc))->setTotalDataComplexity(array_sum($dc))->setTotalSystemComplexity(array_sum($dc) + array_sum($sc)); } return $result; }