Esempio n. 1
0
 /**
  * @group php7
  */
 public function testSpaceshipOperatorIsWellConsidered()
 {
     $filename = __DIR__ . '/../../../../../resources/mccaybe/php7-spaceship-operator.php';
     $loc = new McCabe(new \Hal\Component\Token\Tokenizer());
     $r = $loc->calculate($filename);
     $this->assertEquals(2, $r->getCyclomaticComplexityNumber());
 }
 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. 3
0
 /**
  * Calculates Myer's interval
  *
  *      Cyclomatic complexity : Cyclomatic complexity + L
  *      where L is the number of logical operators
  *
  * @param TokenCollection $tokens
  * @return Result
  */
 public function calculate($tokens)
 {
     $mcCabe = new McCabe();
     $result = new Result();
     // Cyclomatic complexity
     $cc = $mcCabe->calculate($tokens);
     // Number of operator
     $L = 0;
     $logicalOperators = array(T_BOOLEAN_AND => T_BOOLEAN_AND, T_LOGICAL_AND => T_LOGICAL_AND, T_BOOLEAN_OR => T_BOOLEAN_OR, T_LOGICAL_OR => T_LOGICAL_OR);
     foreach ($tokens as $token) {
         if (isset($logicalOperators[$token->getType()])) {
             $L++;
         }
     }
     $result->setNumberOfOperators($L)->setMcCabe($cc);
     return $result;
 }
 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. 5
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. 6
0
 /**
  * @dataProvider provideCCN
  */
 public function testICanCountComplexity($filename, $expectedCCN)
 {
     $loc = new McCabe(new \Hal\Component\Token\Tokenizer());
     $r = $loc->calculate($filename);
     $this->assertEquals($expectedCCN, $r->getCyclomaticComplexityNumber());
 }