Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retain the old ccn and add wmc as new metric #362

Merged
merged 1 commit into from
Jul 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public function leaveNode(Node $node)
) {
$class = $this->metrics->get(MetricClassNameGenerator::getName($node));

$ccn = 0;
$ccn = 1;
$wmc = 0;
$ccnByMethod = [0]; // default maxMethodCcn if no methods are available

foreach ($node->stmts as $stmt) {
Expand Down Expand Up @@ -106,11 +107,13 @@ public function leaveNode(Node $node)

$methodCcn = $cb($stmt) + 1; // each method by default is CCN 1 even if it's empty

$ccn += $methodCcn;
$wmc += $methodCcn;
$ccn += $methodCcn - 1;
$ccnByMethod[] = $methodCcn;
}
}

$class->set('wmc', $wmc);
$class->set('ccn', $ccn);
$class->set('ccnMethodMax', max($ccnByMethod));
}
Expand Down
1 change: 1 addition & 0 deletions src/Hal/Metric/Consolidated.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public function __construct(Metrics $metrics)
'nbMethods' => 0,
];
$avg = (object)[
'wmc' => [],
'ccn' => [],
'bugs' => [],
'kanDefect' => [],
Expand Down
1 change: 1 addition & 0 deletions src/Hal/Report/Cli/Reporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public function generate(Metrics $metrics)

Complexity
Average Cyclomatic complexity by class {$avg->ccn}
Average Weighted method count by class {$avg->wmc}
Average Relative system complexity {$avg->relativeSystemComplexity}
Average Difficulty {$avg->difficulty}

Expand Down
22 changes: 15 additions & 7 deletions src/Hal/Report/Html/template/complexity.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@
require __DIR__ . '/_header.php'; ?>

<div class="row">
<div class="column">
<div class="bloc bloc-number">
<div class="label">Average weighted method count by class <small>(CC)</small></div>
<div class="number">
<?php echo $avg->wmc; ?>
</div>
<?php echo $this->getTrend('avg', 'wmc', true); ?>
</div>
</div>
<div class="column">
<div class="bloc bloc-number">
<div class="label">Average cyclomatic complexity by class</div>
Expand All @@ -22,34 +31,32 @@
</div>
<div class="column">
<div class="bloc bloc-number">
<div class="label">Average bugs by class
<small>(Halstead)</small> <?php echo $this->getTrend('avg', 'bugs', true); ?>
</div>
<div class="label">Average bugs by class<small>(Halstead)</small></div>
<div class="number">
<?php echo $avg->bugs; ?>
</div>
<?php echo $this->getTrend('avg', 'bugs', true); ?>
</div>
</div>
<div class="column">
<div class="bloc bloc-number">
<div class="label">average defects by class
<small>(Kan)</small> <?php echo $this->getTrend('avg', 'kanDefect', true); ?>
</div>
<div class="label">average defects by class <small>(Kan)</small></div>
<div class="number">
<?php echo $avg->kanDefect; ?>
</div>
<?php echo $this->getTrend('avg', 'kanDefect', true); ?>
</div>
</div>
</div>


<div class="row">
<div class="column">
<div class="bloc">
<table class="js-sort-table" id="table-length">
<thead>
<tr>
<th>Class</th>
<th class="js-sort-number">WMC</th>
<th class="js-sort-number">Class cycl.</th>
<th class="js-sort-number">Max method cycl.</th>
<?php if ($config->has('junit')) { ?>
Expand All @@ -65,6 +72,7 @@
foreach ($classes as $class) { ?>
<tr>
<td><?php echo $class['name']; ?></td>
<td><?php echo isset($class['wmc']) ? $class['wmc'] : ''; ?></td>
<td><?php echo isset($class['ccn']) ? $class['ccn'] : ''; ?></td>
<td><?php echo isset($class['ccnMethodMax']) ? $class['ccnMethodMax'] : ''; ?></td>
<?php if ($config->has('junit')) { ?>
Expand Down
2 changes: 1 addition & 1 deletion src/Hal/Report/Html/template/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
<div class="column">
<div class="bloc bloc-number">
<div class="label"><a href="loc.html">Lines of code</a></div>
<?php echo $this->getTrend('sum', 'loc'); ?>
<div class="number"><?php echo $sum->loc; ?></div>
<?php echo $this->getTrend('sum', 'loc'); ?>
</div>
</div>
<div class="column">
Expand Down
2 changes: 1 addition & 1 deletion src/Hal/Violation/Class_/TooComplexClassCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function apply(Metric $metric)

$this->metric = $metric;

if ($metric->get('ccn') > 50) {
if ($metric->get('wmc') > 50) {
$metric->get('violations')->add($this);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,26 @@

class CyclomaticComplexityVisitorTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider provideExamplesForCcn
*/
public function testCcnOfClassesIsWellCalculated($example, $classname, $expectedCcn)
{
$metrics = new Metrics();

$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
$traverser = new NodeTraverser();
$traverser->addVisitor(new NameResolver());
$traverser->addVisitor(new ClassEnumVisitor($metrics));
$traverser->addVisitor(new CyclomaticComplexityVisitor($metrics));

$code = file_get_contents($example);
$stmts = $parser->parse($code);
$traverser->traverse($stmts);

$this->assertSame($expectedCcn, $metrics->get($classname)->get('ccn'));
}

/**
* @dataProvider provideExamplesForWmc
*/
Expand All @@ -27,7 +47,7 @@ public function testWeightedMethodCountOfClassesIsWellCalculated($example, $clas
$stmts = $parser->parse($code);
$traverser->traverse($stmts);

$this->assertSame($expectedWmc, $metrics->get($classname)->get('ccn'));
$this->assertSame($expectedWmc, $metrics->get($classname)->get('wmc'));
}

/**
Expand Down Expand Up @@ -64,6 +84,20 @@ public static function provideExamplesForWmc()
];
}

public static function provideExamplesForCcn()
{
return [
'A' => [__DIR__ . '/../../examples/cyclomatic1.php', 'A', 8],
'B' => [__DIR__ . '/../../examples/cyclomatic1.php', 'B', 4],
'Foo\\C' => [__DIR__ . '/../../examples/cyclomatic_anon.php', 'Foo\\C', 1],
'SwitchCase' => [__DIR__ . '/../../examples/cyclomatic_full.php', 'SwitchCase', 4],
'IfElseif' => [__DIR__ . '/../../examples/cyclomatic_full.php', 'IfElseif', 7],
'Loops' => [__DIR__ . '/../../examples/cyclomatic_full.php', 'Loops', 5],
'CatchIt' => [__DIR__ . '/../../examples/cyclomatic_full.php', 'CatchIt', 3],
'Logical' => [__DIR__ . '/../../examples/cyclomatic_full.php', 'Logical', 11],
];
}

public static function provideExamplesForMaxCc()
{
return [
Expand Down