Skip to content

Update the CRAP of global functions and count them in HTML report #469

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

Closed
wants to merge 1 commit into from
Closed
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
64 changes: 63 additions & 1 deletion src/Node/AbstractNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public function getTestedTraitsPercent($asString = true)
}

/**
* Returns the percentage of traits that has been tested.
* Returns the percentage of classes and traits that has been tested.
*
* @param bool $asString
*
Expand All @@ -180,6 +180,22 @@ public function getTestedClassesAndTraitsPercent($asString = true)
);
}

/**
* Returns the percentage of functions that has been tested.
*
* @param bool $asString
*
* @return int
*/
public function getTestedFunctionsPercent($asString = true)
{
return Util::percent(
$this->getNumTestedFunctions(),
$this->getNumFunctions(),
$asString
);
}

/**
* Returns the percentage of methods that has been tested.
*
Expand All @@ -196,6 +212,22 @@ public function getTestedMethodsPercent($asString = true)
);
}

/**
* Returns the percentage of functions and methods that has been tested.
*
* @param bool $asString
*
* @return int
*/
public function getTestedFunctionsAndMethodsPercent($asString = true)
{
return Util::percent(
$this->getNumTestedFunctionsAndMethods(),
$this->getNumFunctionsAndMethods(),
$asString
);
}

/**
* Returns the percentage of executed lines.
*
Expand Down Expand Up @@ -242,6 +274,36 @@ public function getClassesAndTraits()
return array_merge($this->getClasses(), $this->getTraits());
}

/**
* Returns the number of functions and methods.
*
* @return int
*/
public function getNumFunctionsAndMethods()
{
return $this->getNumFunctions() + $this->getNumMethods();
}

/**
* Returns the number of tested functions and methods.
*
* @return int
*/
public function getNumTestedFunctionsAndMethods()
{
return $this->getNumTestedFunctions() + $this->getNumTestedMethods();
}

/**
* Returns the functions and methods of this node.
*
* @return array
*/
public function getFunctionsAndMethods()
{
return array_merge($this->getFunctions(), $this->getMethods());
}

/**
* Returns the classes of this node.
*
Expand Down
18 changes: 18 additions & 0 deletions src/Node/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,24 @@ protected function calculateStatistics()
$class['coverage']
);
}

foreach ($this->functions as &$function) {
if ($function['executableLines'] > 0) {
$function['coverage'] = ($function['executedLines'] /
$function['executableLines']) * 100;
} else {
$function['coverage'] = 100;
}

if ($function['coverage'] == 100) {
$this->numTestedFunctions++;
}

$function['crap'] = $this->crap(
$function['ccn'],
$function['coverage']
);
}
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/Report/Html/Renderer/Directory.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ protected function renderItem(Node $node, $total = false)
$data = [
'numClasses' => $node->getNumClassesAndTraits(),
'numTestedClasses' => $node->getNumTestedClassesAndTraits(),
'numMethods' => $node->getNumMethods(),
'numTestedMethods' => $node->getNumTestedMethods(),
'numMethods' => $node->getNumFunctionsAndMethods(),
'numTestedMethods' => $node->getNumTestedFunctionsAndMethods(),
'linesExecutedPercent' => $node->getLineExecutedPercent(false),
'linesExecutedPercentAsString' => $node->getLineExecutedPercent(),
'numExecutedLines' => $node->getNumExecutedLines(),
'numExecutableLines' => $node->getNumExecutableLines(),
'testedMethodsPercent' => $node->getTestedMethodsPercent(false),
'testedMethodsPercentAsString' => $node->getTestedMethodsPercent(),
'testedMethodsPercent' => $node->getTestedFunctionsAndMethodsPercent(false),
'testedMethodsPercentAsString' => $node->getTestedFunctionsAndMethodsPercent(),
'testedClassesPercent' => $node->getTestedClassesAndTraitsPercent(false),
'testedClassesPercentAsString' => $node->getTestedClassesAndTraitsPercent()
];
Expand Down
8 changes: 4 additions & 4 deletions src/Report/Html/Renderer/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,14 @@ protected function renderItems(FileNode $node)
'name' => 'Total',
'numClasses' => $node->getNumClassesAndTraits(),
'numTestedClasses' => $node->getNumTestedClassesAndTraits(),
'numMethods' => $node->getNumMethods(),
'numTestedMethods' => $node->getNumTestedMethods(),
'numMethods' => $node->getNumFunctionsAndMethods(),
'numTestedMethods' => $node->getNumTestedFunctionsAndMethods(),
'linesExecutedPercent' => $node->getLineExecutedPercent(false),
'linesExecutedPercentAsString' => $node->getLineExecutedPercent(),
'numExecutedLines' => $node->getNumExecutedLines(),
'numExecutableLines' => $node->getNumExecutableLines(),
'testedMethodsPercent' => $node->getTestedMethodsPercent(false),
'testedMethodsPercentAsString' => $node->getTestedMethodsPercent(),
'testedMethodsPercent' => $node->getTestedFunctionsAndMethodsPercent(false),
'testedMethodsPercentAsString' => $node->getTestedFunctionsAndMethodsPercent(),
'testedClassesPercent' => $node->getTestedClassesAndTraitsPercent(false),
'testedClassesPercentAsString' => $node->getTestedClassesAndTraitsPercent(),
'crap' => '<abbr title="Change Risk Anti-Patterns (CRAP) Index">CRAP</abbr>'
Expand Down