public function collect(Request $request, Response $response, \Exception $exception = null)
 {
     // filter loaded files
     $files = get_included_files();
     $files = array_filter($files, function ($v) {
         $excludes = array('vendor', 'pear', '\\.phar', 'bootstrap\\.php', 'Test', '/app', 'AppKernel.php', 'autoload.php', 'cache/', 'app.php', 'app_dev.php', 'Form', 'PhpMetrics', 'classes.php');
         return !preg_match('!' . implode('|', $excludes) . '!', $v);
     });
     // Prepare datas
     $all = $average = array('cfiles' => 0, 'maintainability' => array(), 'commentWeight' => array(), 'complexity' => array(), 'loc' => array(), 'lloc' => array(), 'cloc' => array(), 'bugs' => array(), 'difficulty' => array(), 'intelligentContent' => array(), 'vocabulary' => array());
     $scoreByFile = array();
     // group files into tmp folder
     $folder = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid();
     mkdir($folder);
     foreach ($files as $file) {
         // run PhpMetrics
         $tokenizer = new Tokenizer();
         $tokenType = new TokenType();
         // halstead
         $halstead = new Halstead($tokenizer, $tokenType);
         $rHalstead = $halstead->calculate($file);
         // loc
         $loc = new Loc($tokenizer);
         $rLoc = $loc->calculate($file);
         // complexity
         $complexity = new McCabe($tokenizer);
         $rComplexity = $complexity->calculate($file);
         // maintainability
         $maintainability = new MaintainabilityIndex();
         $rMaintenability = $maintainability->calculate($rHalstead, $rLoc, $rComplexity);
         // store result
         $files[$file] = array();
         $all['cfiles']++;
         $all['maintainability'][] = $scoreByFile[$file]['maintainability'] = $rMaintenability->getMaintainabilityIndex();
         $all['commentWeight'][] = $scoreByFile[$file]['commentWeight'] = $rMaintenability->getCommentWeight();
         $all['complexity'][] = $scoreByFile[$file]['complexity'] = $rComplexity->getCyclomaticComplexityNumber();
         $all['loc'][] = $scoreByFile[$file]['loc'] = $rLoc->getLoc();
         $all['lloc'][] = $scoreByFile[$file]['lloc'] = $rLoc->getLogicalLoc();
         $all['cloc'][] = $scoreByFile[$file]['cloc'] = $rLoc->getCommentLoc();
         $all['bugs'][] = $scoreByFile[$file]['bugs'] = $rHalstead->getBugs();
         $all['difficulty'][] = $scoreByFile[$file]['difficulty'] = $rHalstead->getDifficulty();
         $all['intelligentContent'][] = $scoreByFile[$file]['intelligentContent'] = $rHalstead->getIntelligentContent();
         $all['vocabulary'][] = $scoreByFile[$file]['vocabulary'] = $rHalstead->getVocabulary();
     }
     // average
     if ($all['cfiles'] > 0) {
         $average['maintainability'] = array_sum($all['maintainability']) / sizeof($all['maintainability']);
         $average['commentWeight'] = array_sum($all['commentWeight']) / sizeof($all['commentWeight']);
         $average['complexity'] = array_sum($all['complexity']) / sizeof($all['complexity']);
         $average['loc'] = array_sum($all['loc']);
         $average['lloc'] = array_sum($all['lloc']);
         $average['cloc'] = array_sum($all['cloc']);
         $average['bugs'] = array_sum($all['bugs']) / sizeof($all['bugs']);
         $average['difficulty'] = array_sum($all['difficulty']) / sizeof($all['difficulty']);
         $average['intelligentContent'] = array_sum($all['intelligentContent']) / sizeof($all['intelligentContent']);
         $average['vocabulary'] = array_sum($all['vocabulary']) / sizeof($all['vocabulary']);
     }
     $this->data = array('average' => $average, 'cfiles' => $all['cfiles'], 'files' => $scoreByFile);
 }
 /**
  * @dataProvider provideValues
  */
 public function testMaintainabilityIndexWithoutCommentIsWellCalculated($cc, $lloc, $cloc, $volume, $MIwoC)
 {
     $rLoc = $this->getMock('\\Hal\\Metrics\\Complexity\\Text\\Length\\Result');
     $rLoc->expects($this->once())->method('getLogicalLoc')->will($this->returnValue($lloc));
     $rHalstead = $this->getMock('\\Hal\\Metrics\\Complexity\\Text\\Halstead\\Result');
     $rHalstead->expects($this->once())->method('getVolume')->will($this->returnValue($volume));
     $rMcCabe = $this->getMock('\\Hal\\Metrics\\Complexity\\Component\\McCabe\\Result');
     $rMcCabe->expects($this->once())->method('getCyclomaticComplexityNumber')->will($this->returnValue($cc));
     $object = new MaintainabilityIndex();
     $result = $object->calculate($rHalstead, $rLoc, $rMcCabe);
     $this->assertEquals($MIwoC, $result->getMaintainabilityIndexWithoutComment());
 }
 public function collect($context, &$storage)
 {
     // filter loaded files
     $files = get_included_files();
     $files = array_filter($files, function ($v) {
         $excludes = array('zray', 'vendor', 'pear', '/usr/local/zend/var/plugins', '\\.phar', 'bootstrap\\.php', 'Test', '/app', 'AppKernel.php', 'autoload.php', 'cache/', 'app.php', 'app_dev.php', 'Form', 'PhpMetrics', 'classes.php');
         return !preg_match('!' . implode('|', $excludes) . '!', $v);
     });
     $scoreByFile = array();
     foreach ($files as $file) {
         // run PhpMetrics
         $tokenizer = new Tokenizer();
         $tokenType = new TokenType();
         // halstead
         $halstead = new Halstead($tokenizer, $tokenType);
         $rHalstead = $halstead->calculate($file);
         // loc
         $loc = new Loc($tokenizer);
         $rLoc = $loc->calculate($file);
         // complexity
         $complexity = new McCabe($tokenizer);
         $rComplexity = $complexity->calculate($file);
         // maintainability
         $maintainability = new MaintainabilityIndex();
         $rMaintenability = $maintainability->calculate($rHalstead, $rLoc, $rComplexity);
         // store result
         $row['maintainability'] = $rMaintenability->getMaintainabilityIndex();
         $row['commentWeight'] = $rMaintenability->getCommentWeight();
         $row['complexity'] = $rComplexity->getCyclomaticComplexityNumber();
         $row['loc'] = $rLoc->getLoc();
         $row['lloc'] = $rLoc->getLogicalLoc();
         $row['cloc'] = $rLoc->getCommentLoc();
         $row['bugs'] = $rHalstead->getBugs();
         $row['difficulty'] = $rHalstead->getDifficulty();
         $row['intelligentContent'] = $rHalstead->getIntelligentContent();
         $row['vocabulary'] = $rHalstead->getVocabulary();
         $row['filename'] = $file;
         $scoreByFile[] = $row;
     }
     $storage['files'] = $scoreByFile;
 }
Example #4
0
 /**
  * Run analyze
  *
  * @param string $filename
  * @param TokenCollection $tokens
  * @return \Hal\Component\Result\ResultSet
  */
 public function execute($filename, $tokens)
 {
     $rHalstead = $this->halstead->calculate($tokens);
     $rLoc = $this->loc->calculate($filename, $tokens);
     $rMcCabe = $this->mcCabe->calculate($tokens);
     $rMyer = $this->myer->calculate($tokens);
     $rMaintainability = $this->maintainabilityIndex->calculate($rHalstead, $rLoc, $rMcCabe);
     $resultSet = new \Hal\Component\Result\ResultSet($filename);
     $resultSet->setLoc($rLoc)->setMcCabe($rMcCabe)->setMyer($rMyer)->setHalstead($rHalstead)->setMaintainabilityIndex($rMaintainability);
     if ($this->withOOP) {
         $rOOP = $this->extractor->extract($tokens);
         $this->classMap->push($filename, $rOOP);
         $resultSet->setOOP($rOOP);
     }
     return $resultSet;
 }