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

Adjust cc violation limits #358

Merged
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
10 changes: 5 additions & 5 deletions src/Hal/Metric/Class_/Complexity/CyclomaticComplexityVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@
/**
* Calculate cyclomatic complexity number and weighted method count.
*
* The cyclomatic complexity (CC) is a measure of control structure complexity of a function or procedure.
* The cyclomatic complexity (CCN) is a measure of control structure complexity of a function or procedure.
* We can calculate ccn in two ways (we choose the second):
*
* 1. Cyclomatic complexity (CC) = E - N + 2P
* 1. Cyclomatic complexity (CCN) = E - N + 2P
* Where:
* P = number of disconnected parts of the flow graph (e.g. a calling program and a subroutine)
* E = number of edges (transfers of control)
* N = number of nodes (sequential group of statements containing only one transfer of control)
*
* 2. CC = Number of each decision point
* 2. CCN = Number of each decision point
*
* The weighted method count (WMC) is count of methods parameterized by a algorithm to compute the weight of a method.
* Given a weight metric w and methods m it can be computed as
Expand All @@ -32,8 +32,8 @@
* - Lines of Code
* - 1 (unweighted WMC)
*
* This visitor provides two metrics, the maximal CC of all methods from one class (currently stored as ccnMethodMax)
* and the WMC using the CC as weight metric (currently stored as ccn).
* This visitor provides two metrics, the maximal CCN of all methods from one class (currently stored as ccnMethodMax)
* and the WMC using the CCN as weight metric (currently stored as ccn).
*
* @see https://en.wikipedia.org/wiki/Cyclomatic_complexity
* @see http://www.literateprogramming.com/mccabe.pdf
Expand Down
28 changes: 10 additions & 18 deletions src/Hal/Violation/Class_/TooComplexClassCode.php
Original file line number Diff line number Diff line change
@@ -1,51 +1,44 @@
<?php
namespace Hal\Violation\Class_;


use Hal\Metric\ClassMetric;
use Hal\Metric\Metric;
use Hal\Violation\Violation;

/**
* 50 as a threshold seems to be widely accepted in open source metric tools.
*
* @see http://staff.unak.is/andy/StaticAnalysis0809/metrics/wmc.html
* @see https://github.com/phpmd/phpmd/blob/f1c145e538d7cf8c2d1a45fd8fb723eca64005f4/src/main/resources/rulesets/codesize.xml#L390
*/
class TooComplexClassCode implements Violation
{
/** @var Metric|null */
private $metric;

/**
* @inheritdoc
*/
public function getName()
{
return 'Too complex class code';
}

/**
* @inheritdoc
*/
public function apply(Metric $metric)
{
if (!$metric instanceof ClassMetric) {
if (! $metric instanceof ClassMetric) {
return;
}

$this->metric = $metric;

if ($metric->get('ccn') >= 25) {
if ($metric->get('ccn') > 50) {
$metric->get('violations')->add($this);
return;
}

}

/**
* @inheritdoc
*/
public function getLevel()
{
return Violation::ERROR;
}

/**
* @inheritdoc
*/
public function getDescription()
{
return <<<EOT
Expand All @@ -56,6 +49,5 @@ public function getDescription()

Maybe you should delegate some code to other objects.
EOT;

}
}
28 changes: 12 additions & 16 deletions src/Hal/Violation/Class_/TooComplexMethodCode.php
Original file line number Diff line number Diff line change
@@ -1,51 +1,48 @@
<?php
namespace Hal\Violation\Class_;


use Hal\Metric\ClassMetric;
use Hal\Metric\Metric;
use Hal\Violation\Violation;

/**
* According to McCabe,
*
* The particular upper bound that has been used for cyclomatic complexity is 10
* which seems like a reasonable, but not magical, upper limit.
*
* @see http://www.literateprogramming.com/mccabe.pdf
*/
class TooComplexMethodCode implements Violation
{
/** @var Metric|null */
private $metric;

/**
* @inheritdoc
*/
public function getName()
{
return 'Too complex method code';
}

/**
* @inheritdoc
*/
public function apply(Metric $metric)
{
if (!$metric instanceof ClassMetric) {
if (! $metric instanceof ClassMetric) {
return;
}

$this->metric = $metric;

if ($metric->get('ccnMethodMax') >= 8) {
if ($metric->get('ccnMethodMax') > 10) {
$metric->get('violations')->add($this);
return;
}

}

/**
* @inheritdoc
*/
public function getLevel()
{
return Violation::ERROR;
}

/**
* @inheritdoc
*/
public function getDescription()
{
return <<<EOT
Expand All @@ -55,6 +52,5 @@ public function getDescription()

Maybe you should delegate some code to other objects or split complex method.
EOT;

}
}