Skip to content

Commit

Permalink
Implement CCN/LOC and CCN/NOM metrics.
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Sep 14, 2009
1 parent 0aa84e6 commit 18806ec
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 26 deletions.
40 changes: 35 additions & 5 deletions PHPLOC/Analyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class PHPLOC_Analyser
'cloc' => 0,
'ncloc' => 0,
'eloc' => 0,
'ccn' => 0,
'interfaces' => 0,
'classes' => 0,
'abstractClasses' => 0,
Expand All @@ -73,6 +74,8 @@ class PHPLOC_Analyser
'classConstants' => 0,
'testClasses' => 0,
'testMethods' => 0,
'ccnByLoc' => 0,
'ccnByNom' => 0,
'locByNoc' => 0,
'locByNom' => 0,
);
Expand Down Expand Up @@ -120,17 +123,23 @@ public function countFiles(array $files, $countTests)
}

$count['directories'] = count($directories) - 1;
$count['classes'] = $count['abstractClasses'] +
$count['concreteClasses'];
$count['methods'] = $count['staticMethods'] +
$count['nonStaticMethods'];

$count['classes'] = $count['abstractClasses'] +
$count['concreteClasses'];
if ($count['loc'] > 0) {
$count['ccnByLoc'] = $count['ccn'] / $count['loc'];
}

if ($count['methods'] > 0) {
$count['ccnByNom'] = $count['ccn'] / $count['methods'];
}

if ($count['classes'] > 0) {
$count['locByNoc'] = $count['loc'] / $count['classes'];
}

$count['methods'] = $count['staticMethods'] +
$count['nonStaticMethods'];

if ($count['methods'] > 0) {
$count['locByNom'] = $count['loc'] / $count['methods'];
}
Expand Down Expand Up @@ -193,6 +202,10 @@ public function countFile($file, $countTests)

for ($i = 0; $i < $numTokens; $i++) {
if (is_string($tokens[$i])) {
if (trim($token) == '?') {
$this->count['ccn']++;
}

if ($className !== NULL) {
if ($tokens[$i] == '{') {
$braces++;
Expand All @@ -213,6 +226,23 @@ public function countFile($file, $countTests)

list ($token, $value) = $tokens[$i];

switch ($token) {
case T_IF:
case T_ELSEIF:
case T_FOR:
case T_FOREACH:
case T_WHILE:
case T_CASE:
case T_CATCH:
case T_BOOLEAN_AND:
case T_LOGICAL_AND:
case T_BOOLEAN_OR:
case T_LOGICAL_OR: {
$this->count['ccn']++;
}
break;
}

if ($token == T_COMMENT || $token == T_DOC_COMMENT) {
$cloc += substr_count($value, "\n") + 1;
}
Expand Down
45 changes: 24 additions & 21 deletions PHPLOC/TextUI/ResultPrinter/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,35 +64,37 @@ public function printResult(array $count)
$format = '';

if ($count['directories'] > 0) {
$format .= "Directories: %10d\n" .
"Files: %10d\n\n";
$args[] = $count['directories'];
$args[] = $count['files'];

$format .= "Directories: %10d\n" .
"Files: %10d\n\n";
}

$format .= "Lines of Code (LOC): %10d\n" .
" Cyclomatic Complexity / Lines of Code: %10.2f\n";
$args[] = $count['loc'];
$format .= "Lines of Code (LOC): %10d\n";
$args[] = $count['ccnByLoc'];

if (isset($count['eloc'])) {
$format .= "Executable Lines of Code (ELOC): %10d\n";
$args[] = $count['eloc'];
$format .= "Executable Lines of Code (ELOC): %10d\n";
}

$format .= "Comment Lines of Code (CLOC): %10d\n" .
"Non-Comment Lines of Code (NCLOC): %10d\n\n" .
"Interfaces: %10d\n" .
"Classes: %10d\n" .
" Abstract Classes: %10d\n" .
" Concrete Classes: %10d\n" .
" Lines of Code / Number of Classes: %10d\n" .
"Methods: %10d\n" .
" Non-Static Methods: %10d\n" .
" Static Methods: %10d\n" .
" Lines of Code / Number of Methods: %10d\n" .
"Functions: %10d\n" .
"Constants: %10d\n" .
"Class constants: %10d\n";
$format .= "Comment Lines of Code (CLOC): %10d\n" .
"Non-Comment Lines of Code (NCLOC): %10d\n\n" .
"Interfaces: %10d\n" .
"Classes: %10d\n" .
" Abstract Classes: %10d\n" .
" Concrete Classes: %10d\n" .
" Lines of Code / Number of Classes: %10d\n" .
"Methods: %10d\n" .
" Non-Static Methods: %10d\n" .
" Static Methods: %10d\n" .
" Lines of Code / Number of Methods: %10d\n" .
" Cyclomatic Complexity / Number of Methods: %10.2f\n" .
"Functions: %10d\n" .
"Constants: %10d\n" .
"Class constants: %10d\n";

$args[] = $count['cloc'];
$args[] = $count['ncloc'];
Expand All @@ -105,6 +107,7 @@ public function printResult(array $count)
$args[] = $count['nonStaticMethods'];
$args[] = $count['staticMethods'];
$args[] = $count['locByNom'];
$args[] = $count['ccnByNom'];
$args[] = $count['functions'];
$args[] = $count['constants'];
$args[] = $count['classConstants'];
Expand All @@ -113,8 +116,8 @@ public function printResult(array $count)
$args[] = $count['testClasses'];
$args[] = $count['testMethods'];

$format .= "\nTest classes: %10d\n" .
"Test methods: %10d\n";
$format .= "\nTest classes: %10d\n" .
"Test methods: %10d\n";
}

vprintf($format, $args);
Expand Down

0 comments on commit 18806ec

Please sign in to comment.