/**
  * testWmciMetricIsCalculatedForCurrentAndNotParentClass
  * 
  * @return void
  */
 public function testWmciMetricIsCalculatedForCurrentAndNotParentClass()
 {
     $packages = self::parseCodeResourceForTest();
     $class = $packages->current()->getClasses()->current();
     $ccnAnalyzer = new PHP_Depend_Metrics_CyclomaticComplexity_Analyzer();
     $ccnAnalyzer->setCache(new PHP_Depend_Util_Cache_Driver_Memory());
     $analyzer = new PHP_Depend_Metrics_ClassLevel_Analyzer();
     $analyzer->addAnalyzer($ccnAnalyzer);
     $analyzer->analyze($packages);
     $metrics = $analyzer->getNodeMetrics($class);
     self::assertEquals(2, $metrics['wmci']);
 }
Exemplo n.º 2
0
 /**
  * Aggregates the required metrics from the registered analyzers.
  *
  * @return array(string => mixed)
  * @throws RuntimeException If one of the required analyzers isn't set.
  */
 private function _collectMetrics()
 {
     if ($this->_coupling === null) {
         throw new RuntimeException('Missing Coupling analyzer.');
     }
     if ($this->_cyclomaticComplexity === null) {
         throw new RuntimeException('Missing Cyclomatic Complexity analyzer.');
     }
     if ($this->_inheritance === null) {
         throw new RuntimeException('Missing Inheritance analyzer.');
     }
     if ($this->_nodeCount === null) {
         throw new RuntimeException('Missing Node Count analyzer.');
     }
     if ($this->_nodeLoc === null) {
         throw new RuntimeException('Missing Node LOC analyzer.');
     }
     $coupling = $this->_coupling->getProjectMetrics();
     $cyclomatic = $this->_cyclomaticComplexity->getProjectMetrics();
     $inheritance = $this->_inheritance->getProjectMetrics();
     $nodeCount = $this->_nodeCount->getProjectMetrics();
     $nodeLoc = $this->_nodeLoc->getProjectMetrics();
     return array('cyclo' => $cyclomatic['ccn2'], 'loc' => $nodeLoc['eloc'], 'nom' => $nodeCount['nom'] + $nodeCount['nof'], 'noc' => $nodeCount['noc'], 'nop' => $nodeCount['nop'], 'ahh' => round($inheritance['ahh'], 3), 'andc' => round($inheritance['andc'], 3), 'fanout' => $coupling['fanout'], 'calls' => $coupling['calls']);
 }
Exemplo n.º 3
0
 /**
  * Calculates the crap index for the given callable.
  *
  * @param PHP_Depend_Code_AbstractCallable $callable The context callable.
  *
  * @return float
  */
 private function _calculateCrapIndex(PHP_Depend_Code_AbstractCallable $callable)
 {
     $report = $this->_createOrReturnCoverageReport();
     $complexity = $this->_ccnAnalyzer->getCCN2($callable);
     $coverage = $report->getCoverage($callable);
     if ($coverage == 0) {
         return pow($complexity, 2) + $complexity;
     } else {
         if ($coverage > 99.5) {
             return $complexity;
         }
     }
     return pow($complexity, 2) * pow(1 - $coverage / 100, 3) + $complexity;
 }
Exemplo n.º 4
0
 /**
  * Analyzes the source code associated with the calling test method and
  * returns all measured metrics.
  *
  * @return mixed
  * @since 1.0.6
  */
 private function _calculateTraitMetrics()
 {
     $packages = $this->parseCodeResourceForTest();
     $package = $packages->current();
     $ccnAnalyzer = new PHP_Depend_Metrics_CyclomaticComplexity_Analyzer();
     $ccnAnalyzer->setCache(new PHP_Depend_Util_Cache_Driver_Memory());
     $analyzer = new PHP_Depend_Metrics_ClassLevel_Analyzer();
     $analyzer->addAnalyzer($ccnAnalyzer);
     $analyzer->analyze($packages);
     return $analyzer->getNodeMetrics($package->getTraits()->current());
 }
 /**
  * testClassLevelAnalyzerNotRunsEndlessForDeepInterfaceHierarchy
  * 
  * @return void
  */
 public function testClassLevelAnalyzerNotRunsEndlessForDeepInterfaceHierarchy()
 {
     set_time_limit(5);
     $ccnAnalyzer = new PHP_Depend_Metrics_CyclomaticComplexity_Analyzer();
     $ccnAnalyzer->setCache(new PHP_Depend_Util_Cache_Driver_Memory());
     $analyzer = new PHP_Depend_Metrics_ClassLevel_Analyzer();
     $analyzer->addAnalyzer($ccnAnalyzer);
     $analyzer->analyze($this->parseCodeResourceForTest());
 }
Exemplo n.º 6
0
 /**
  * testAnalyzerAlsoCalculatesCCNAndCCN2OfClosureInMethod
  *
  * @return void
  * @covers PHP_Depend_Metrics_CyclomaticComplexity_Analyzer
  * @group pdepend
  * @group pdepend::metrics
  * @group pdepend::metrics::cyclomaticcomplexity
  * @group unittest
  */
 public function testAnalyzerAlsoCalculatesCCNAndCCN2OfClosureInMethod()
 {
     $analyzer = new PHP_Depend_Metrics_CyclomaticComplexity_Analyzer();
     $analyzer->analyze(self::parseTestCaseSource(__METHOD__));
     $expected = array('ccn' => 3, 'ccn2' => 3);
     $actual = $analyzer->getProjectMetrics();
     $this->assertEquals($expected, $actual);
 }
Exemplo n.º 7
0
 /**
  * Calculates the Weight Method Per Class metric, this method only counts
  * protected and public methods of parent classes.
  *
  * @param PHP_Depend_Code_Class $class The context class instance.
  *
  * @return integer
  */
 private function _calculateWMCi(PHP_Depend_Code_Class $class)
 {
     // List of methods, this method only counts not overwritten methods.
     $ccn = array();
     // First collect all methods of the context class
     foreach ($class->getMethods() as $m) {
         $ccn[$m->getName()] = $this->_cyclomaticAnalyzer->getCCN2($m);
     }
     // Get parent class and collect all non private methods.
     $parent = $class->getParentClass();
     while ($parent !== null) {
         // Count all methods
         foreach ($parent->getMethods() as $m) {
             if (!$m->isPrivate() && !isset($methods[$m->getName()])) {
                 $ccn[$m->getName()] = $this->_cyclomaticAnalyzer->getCCN2($m);
             }
         }
         // Fetch parent class
         $parent = $parent->getParentClass();
     }
     return array_sum($ccn);
 }
Exemplo n.º 8
0
 /**
  * Calculates the Weight Method Per Class metric.
  *
  * @param PHP_Depend_Code_AbstractType $type The context type instance.
  *
  * @return integer[]
  * @since 1.0.6
  */
 private function calculateWmci(PHP_Depend_Code_AbstractType $type)
 {
     $ccn = array();
     foreach ($type->getMethods() as $method) {
         $ccn[$method->getName()] = $this->cyclomaticAnalyzer->getCcn2($method);
     }
     return $ccn;
 }
Exemplo n.º 9
0
 /**
  * Returns a pre configured ccn analyzer.
  *
  * @return PHP_Depend_Metrics_CyclomaticComplexity_Analyzer
  * @since 1.0.0
  */
 private function _createAnalyzer()
 {
     $analyzer = new PHP_Depend_Metrics_CyclomaticComplexity_Analyzer();
     $analyzer->setCache($this->_cache);
     return $analyzer;
 }
 /**
  * testAnalyzerCountsNumberOfMethodsForClassSize
  *
  * @return void
  */
 public function testAnalyzerCountsNumberOfMethodsForClassSize()
 {
     $packages = self::parseCodeResourceForTest();
     $class = $packages->current()->getClasses()->current();
     $ccnAnalyzer = new PHP_Depend_Metrics_CyclomaticComplexity_Analyzer();
     $ccnAnalyzer->setCache(new PHP_Depend_Util_Cache_Driver_Memory());
     $analyzer = new PHP_Depend_Metrics_ClassLevel_Analyzer();
     $analyzer->addAnalyzer($ccnAnalyzer);
     $analyzer->analyze($packages);
     $metrics = $analyzer->getNodeMetrics($class);
     self::assertEquals(6, $metrics['csz']);
 }
Exemplo n.º 11
0
 /**
  * Tests the result of the phpunit logger with some real analyzers.
  *
  * @return void
  * @covers PHP_Depend_Log_Phpunit_Xml
  * @group pdepend
  * @group pdepend::logs
  * @group pdepend::logs::summary
  * @group unittest
  */
 public function testPHPUnitLoggerResult()
 {
     $packages = self::parseTestCaseSource(__METHOD__);
     $logger = new PHP_Depend_Log_Phpunit_Xml();
     $logger->setLogFile($this->_tempFile);
     $logger->setCode($packages);
     $analyzer0 = new PHP_Depend_Metrics_CyclomaticComplexity_Analyzer();
     $analyzer0->analyze($packages);
     $analyzer1 = new PHP_Depend_Metrics_ClassLevel_Analyzer();
     $analyzer1->addAnalyzer($analyzer0);
     $analyzer1->analyze($packages);
     $analyzer2 = new PHP_Depend_Metrics_CodeRank_Analyzer();
     $analyzer2->analyze($packages);
     $analyzer3 = new PHP_Depend_Metrics_Coupling_Analyzer();
     $analyzer3->analyze($packages);
     $analyzer4 = new PHP_Depend_Metrics_Hierarchy_Analyzer();
     $analyzer4->analyze($packages);
     $analyzer5 = new PHP_Depend_Metrics_Inheritance_Analyzer();
     $analyzer5->analyze($packages);
     $analyzer6 = new PHP_Depend_Metrics_NodeCount_Analyzer();
     $analyzer6->analyze($packages);
     $analyzer7 = new PHP_Depend_Metrics_NodeLoc_Analyzer();
     $analyzer7->analyze($packages);
     $logger->log($analyzer0);
     $logger->log($analyzer1);
     $logger->log($analyzer2);
     $logger->log($analyzer3);
     $logger->log($analyzer4);
     $logger->log($analyzer5);
     $logger->log($analyzer6);
     $logger->log($analyzer7);
     $logger->close();
     $actual = file_get_contents($this->_tempFile);
     $expected = $this->_loadExpected('phpunit-log.xml');
     $this->assertXmlStringEqualsXmlString($expected, $actual);
 }