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);
 }
Esempio n. 2
0
 public function testICanCountLoc()
 {
     $filename = __DIR__ . '/../../../../../resources/loc/f1.php';
     $loc = new Loc(new \Hal\Component\Token\Tokenizer());
     $r = $loc->calculate($filename);
     $this->assertEquals(14, $r->getCommentLoc());
     $this->assertEquals(33, $r->getLoc());
     $this->assertEquals(2, $r->getLogicalLoc());
 }
 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;
 }
Esempio n. 4
0
 /**
  * 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;
 }
Esempio n. 5
0
 /**
  * @param string $propertyName
  */
 public function __get($propertyName)
 {
     switch ($propertyName) {
         case 'absPath':
             $propertyValue = $this->getResource()->getPathname();
             break;
         case 'absDir':
             $propertyValue = dirname($this->getResource()->getPathname());
             break;
         case 'atime':
             $propertyValue = $this->getResource()->getATime();
             break;
         case 'contents':
             $propertyValue = $this->getResource()->getContents();
             break;
         case 'ctime':
             $propertyValue = $this->getResource()->getCTime();
             break;
         case 'depth':
             $relativePath = dirname($this->getRelativePathname());
             $parts = explode('/', $relativePath);
             $propertyValue = count($parts);
             break;
         case 'extension':
             $propertyValue = $this->getResource()->getExtension();
             break;
         case 'group':
             $groupData = posix_getgrgid($this->getResource()->getGroup());
             $propertyValue = $groupData['name'];
             break;
         case 'groupId':
             $propertyValue = $this->getResource()->getGroup();
             break;
         case 'loc':
             $resource = $this->getResource();
             if ('php' == $resource->getExtension()) {
                 $tokenizer = new Tokenizer();
                 $locAnalyzer = new Loc($tokenizer);
                 $info = $locAnalyzer->calculate($resource->getPathname());
                 $propertyValue = $info->getLoc();
             } else {
                 $propertyValue = null;
             }
             break;
         case 'mtime':
             $propertyValue = $this->getResource()->getMTime();
             break;
         case 'name':
             $propertyValue = $this->getResource()->getFilename();
             break;
         case 'owner':
             $ownerData = posix_getpwuid($this->getResource()->getOwner());
             $propertyValue = $ownerData['name'];
             break;
         case 'ownerId':
             $propertyValue = $this->getResource()->getOwner();
             break;
         case 'path':
             $propertyValue = $this->getRelativePathname();
             break;
         case 'perms':
             $propertyValue = $this->getResource()->getPerms();
             break;
         case 'size':
             $propertyValue = $this->getResource()->getSize();
             break;
         case 'type':
             $propertyValue = $this->getResource()->getType();
             break;
         default:
             throw new \InvalidArgumentException('Unknown property: ' . $propertyName);
     }
     return $propertyValue;
 }