コード例 #1
0
 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);
 }
コード例 #2
0
ファイル: HalsteadTest.php プロジェクト: truffo/PhpMetrics
 /**
  * @dataProvider provideFilesAndCounts
  */
 public function testHalsteadGiveValidValues($file, $N1, $N2, $n1, $n2, $N, $V, $L, $D, $E, $T, $I)
 {
     $tokenType = new TokenType();
     // please don't mock this: it make no sense else
     $tokenizer = new Tokenizer();
     $halstead = new Halstead($tokenizer, $tokenType);
     $r = $halstead->calculate($file);
     $this->assertEquals($N1, $r->getNumberOfOperators());
     $this->assertEquals($n1, $r->getNumberOfUniqueOperators());
     $this->assertEquals($N2, $r->getNumberOfOperands());
     $this->assertEquals($n2, $r->getNumberOfUniqueOperands());
     $this->assertEquals($N, $r->getLength(), 'length');
     $this->assertEquals($V, $r->getVolume(), 'volume');
     $this->assertEquals($L, $r->getLevel(), 'level');
     $this->assertEquals($D, $r->getDifficulty(), 'difficulty');
     $this->assertEquals($E, $r->getEffort(), 'effort');
     $this->assertEquals($E, $r->getEffort(), 'effort');
     $this->assertEquals($T, $r->getTime(), 'time');
     $this->assertEquals($I, $r->getIntelligentContent(), 'intelligent content');
 }
コード例 #3
0
 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;
 }
コード例 #4
0
ファイル: FileAnalyzer.php プロジェクト: aik099/PhpMetrics
 /**
  * Run analyze
  *
  * @param $filename
  * @return \Hal\Component\Result\ResultSet
  */
 public function execute($filename)
 {
     $rHalstead = $this->halstead->calculate($filename);
     $rLoc = $this->loc->calculate($filename);
     $rMcCabe = $this->mcCabe->calculate($filename);
     $rMyer = $this->myer->calculate($filename);
     $rMaintenability = $this->maintenabilityIndex->calculate($rHalstead, $rLoc, $rMcCabe);
     $resultSet = new \Hal\Component\Result\ResultSet($filename);
     $resultSet->setLoc($rLoc)->setMcCabe($rMcCabe)->setMyer($rMyer)->setHalstead($rHalstead)->setMaintenabilityIndex($rMaintenability);
     if ($this->withOOP) {
         $rOOP = $this->extractor->extract($filename);
         $this->classMap->push($filename, $rOOP);
         $resultSet->setOOP($rOOP);
     }
     return $resultSet;
 }