Skip to content
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
1 change: 1 addition & 0 deletions src/Node/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,7 @@ private function processFunctions(array $functions): void
'namespace' => $function['namespace'],
'signature' => $function['signature'],
'startLine' => $function['startLine'],
'endLine' => $function['endLine'],
'executableLines' => 0,
'executedLines' => 0,
'executableBranches' => 0,
Expand Down
93 changes: 93 additions & 0 deletions src/Report/Cobertura.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,100 @@ public function process(CodeCoverage $coverage, ?string $target = null, ?string
}
}

if ($report->numberOfFunctions() === 0) {
$packageElement->setAttribute('complexity', (string) $packageComplexity);

continue;
}

$functionsComplexity = 0;
$functionsLinesValid = 0;
$functionsLinesCovered = 0;
$functionsBranchesValid = 0;
$functionsBranchesCovered = 0;

$classElement = $document->createElement('class');
$classElement->setAttribute('name', basename($item->pathAsString()));
$classElement->setAttribute('filename', str_replace($report->pathAsString() . '/', '', $item->pathAsString()));

$methodsElement = $document->createElement('methods');

$classElement->appendChild($methodsElement);

$classLinesElement = $document->createElement('lines');

$classElement->appendChild($classLinesElement);

$functions = $report->functions();

foreach ($functions as $functionName => $function) {
if ($function['executableLines'] === 0) {
continue;
}

$complexity += $function['ccn'];
$packageComplexity += $function['ccn'];
$functionsComplexity += $function['ccn'];

$linesValid = $function['executableLines'];
$linesCovered = $function['executedLines'];
$lineRate = $linesValid === 0 ? 0 : ($linesCovered / $linesValid);

$functionsLinesValid += $linesValid;
$functionsLinesCovered += $linesCovered;

$branchesValid = $function['executableBranches'];
$branchesCovered = $function['executedBranches'];
$branchRate = $branchesValid === 0 ? 0 : ($branchesCovered / $branchesValid);

$functionsBranchesValid += $branchesValid;
$functionsBranchesCovered += $branchesValid;

$methodElement = $document->createElement('method');

$methodElement->setAttribute('name', $functionName);
$methodElement->setAttribute('signature', $function['signature']);
$methodElement->setAttribute('line-rate', (string) $lineRate);
$methodElement->setAttribute('branch-rate', (string) $branchRate);
$methodElement->setAttribute('complexity', (string) $function['ccn']);

$methodLinesElement = $document->createElement('lines');

$methodElement->appendChild($methodLinesElement);

foreach (range($function['startLine'], $function['endLine']) as $line) {
if (!isset($coverageData[$line]) || $coverageData[$line] === null) {
continue;
}
$methodLineElement = $document->createElement('line');

$methodLineElement->setAttribute('number', (string) $line);
$methodLineElement->setAttribute('hits', (string) count($coverageData[$line]));

$methodLinesElement->appendChild($methodLineElement);

$classLineElement = $methodLineElement->cloneNode();

$classLinesElement->appendChild($classLineElement);
}

$methodsElement->appendChild($methodElement);
}

$packageElement->setAttribute('complexity', (string) $packageComplexity);

if ($functionsLinesValid === 0) {
continue;
}

$lineRate = $functionsLinesCovered / $functionsLinesValid;
$branchRate = $functionsBranchesValid === 0 ? 0 : ($functionsBranchesCovered / $functionsBranchesValid);

$classElement->setAttribute('line-rate', (string) $lineRate);
$classElement->setAttribute('branch-rate', (string) $branchRate);
$classElement->setAttribute('complexity', (string) $functionsComplexity);

$classesElement->appendChild($classElement);
}

$coverageElement->setAttribute('complexity', (string) $complexity);
Expand Down
35 changes: 35 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -1728,6 +1728,41 @@ protected function setUpXdebugStubForClassWithAnonymousFunction(): Driver
return $stub;
}

protected function getCoverageForClassWithOutsideFunction(): CodeCoverage
{
$filter = new Filter;
$filter->includeFile(TEST_FILES_PATH . 'source_with_class_and_outside_function.php');

$coverage = new CodeCoverage(
$this->setUpXdebugStubForClassWithOutsideFunction(),
$filter
);

$coverage->start('ClassWithOutsideFunction', true);
$coverage->stop();

return $coverage;
}

protected function setUpXdebugStubForClassWithOutsideFunction(): Driver
{
$stub = $this->createStub(Driver::class);

$stub->method('stop')
->willReturn(RawCodeCoverageData::fromXdebugWithoutPathCoverage(
[
TEST_FILES_PATH . 'source_with_class_and_outside_function.php' => [
6 => 1,
12 => 1,
13 => 1,
16 => -1,
],
]
));

return $stub;
}

protected function removeTemporaryFiles(): void
{
$tmpFilesIterator = new RecursiveIteratorIterator(
Expand Down
41 changes: 41 additions & 0 deletions tests/_files/class-with-outside-function-cobertura.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE coverage SYSTEM "http://cobertura.sourceforge.net/xml/coverage-04.dtd">
<coverage line-rate="1.3333333333333" branch-rate="0" lines-covered="3" lines-valid="4" branches-covered="0" branches-valid="0" complexity="3" version="0.4" timestamp="%i">
<sources>
<source>%s</source>
</sources>
<packages>
<package name="" line-rate="0.75" branch-rate="0" complexity="3">
<classes>
<class name="ClassInFileWithOutsideFunction" filename="source_with_class_and_outside_function.php" line-rate="1" branch-rate="0" complexity="1">
<methods>
<method name="classMethod" signature="classMethod(): string" line-rate="1" branch-rate="0" complexity="1">
<lines>
<line number="6" hits="1"/>
</lines>
</method>
</methods>
<lines>
<line number="6" hits="1"/>
</lines>
</class>
<class name="source_with_class_and_outside_function.php" filename="source_with_class_and_outside_function.php" line-rate="0.66666666666667" branch-rate="0" complexity="2">
<methods>
<method name="outsideFunction" signature="outsideFunction(bool $test): int" line-rate="0.66666666666667" branch-rate="0" complexity="2">
<lines>
<line number="12" hits="1"/>
<line number="13" hits="1"/>
<line number="16" hits="0"/>
</lines>
</method>
</methods>
<lines>
<line number="12" hits="1"/>
<line number="13" hits="1"/>
<line number="16" hits="0"/>
</lines>
</class>
</classes>
</package>
</packages>
</coverage>
17 changes: 17 additions & 0 deletions tests/_files/source_with_class_and_outside_function.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php declare(strict_types=1);
class ClassInFileWithOutsideFunction
{
public static function classMethod(): string
{
return self::class;
}
}

function outsideFunction(bool $test): int
{
if ($test) {
return 1;
}

return 0;
}
10 changes: 10 additions & 0 deletions tests/tests/CoberturaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,14 @@ public function testCoberturaForClassWithAnonymousFunction(): void
$cobertura->process($this->getCoverageForClassWithAnonymousFunction())
);
}

public function testCoberturaForClassAndOutsideFunction(): void
{
$cobertura = new Cobertura;

$this->assertStringMatchesFormatFile(
TEST_FILES_PATH . 'class-with-outside-function-cobertura.xml',
$cobertura->process($this->getCoverageForClassWithOutsideFunction())
);
}
}