Skip to content

Commit 42545dd

Browse files
1 parent c1888bb commit 42545dd

File tree

14 files changed

+35
-12
lines changed

14 files changed

+35
-12
lines changed

ChangeLog-12.5.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ All notable changes are documented in this file using the [Keep a CHANGELOG](htt
1414
* [#1107](https://github.com/sebastianbergmann/php-code-coverage/pull/1107): Do not sort code coverage data over and over
1515
* [#1108](https://github.com/sebastianbergmann/php-code-coverage/pull/1108): Do not sort covered files data over and over
1616
* [#1109](https://github.com/sebastianbergmann/php-code-coverage/pull/1109): Represent line coverage data using objects
17+
* [#1127](https://github.com/sebastianbergmann/php-code-coverage/issues/1127): Add SHA-1 hash of content of SUT source file to XML report
1718

1819
[12.5.0]: https://github.com/sebastianbergmann/php-code-coverage/compare/12.4.0...main

src/Node/Builder.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use function explode;
1818
use function implode;
1919
use function is_file;
20+
use function sha1_file;
2021
use function str_ends_with;
2122
use function str_replace;
2223
use function str_starts_with;
@@ -76,6 +77,7 @@ private function addItems(Directory $root, array $items, array $tests): void
7677
new File(
7778
$key,
7879
$root,
80+
sha1_file($filename),
7981
$value['lineCoverage'],
8082
$value['functionCoverage'],
8183
$tests,

src/Node/File.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@
3434
*/
3535
final class File extends AbstractNode
3636
{
37+
/**
38+
* @var non-empty-string
39+
*/
40+
private string $sha1;
41+
3742
/**
3843
* @var array<int, ?list<non-empty-string>>
3944
*/
@@ -80,16 +85,18 @@ final class File extends AbstractNode
8085
private array $codeUnitsByLine = [];
8186

8287
/**
88+
* @param non-empty-string $sha1
8389
* @param array<int, ?list<non-empty-string>> $lineCoverageData
8490
* @param array<string, TestType> $testData
8591
* @param array<string, Class_> $classes
8692
* @param array<string, Trait_> $traits
8793
* @param array<string, Function_> $functions
8894
*/
89-
public function __construct(string $name, AbstractNode $parent, array $lineCoverageData, array $functionCoverageData, array $testData, array $classes, array $traits, array $functions, LinesOfCode $linesOfCode)
95+
public function __construct(string $name, AbstractNode $parent, string $sha1, array $lineCoverageData, array $functionCoverageData, array $testData, array $classes, array $traits, array $functions, LinesOfCode $linesOfCode)
9096
{
9197
parent::__construct($name, $parent);
9298

99+
$this->sha1 = $sha1;
93100
$this->lineCoverageData = $lineCoverageData;
94101
$this->functionCoverageData = $functionCoverageData;
95102
$this->testData = $testData;
@@ -103,6 +110,14 @@ public function count(): int
103110
return 1;
104111
}
105112

113+
/**
114+
* @return non-empty-string
115+
*/
116+
public function sha1(): string
117+
{
118+
return $this->sha1;
119+
}
120+
106121
/**
107122
* @return array<int, ?list<non-empty-string>>
108123
*/

src/Report/Xml/Facade.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ private function processFile(FileNode $file, Directory $context): void
142142
$fileObject = $context->addFile(
143143
$file->name(),
144144
$file->id() . '.xml',
145+
$file->sha1(),
145146
);
146147

147148
$this->setTotals($file, $fileObject->totals());
@@ -151,7 +152,7 @@ private function processFile(FileNode $file, Directory $context): void
151152
strlen($this->project->projectSourceDirectory()),
152153
);
153154

154-
$fileReport = new Report($path);
155+
$fileReport = new Report($path, $file->sha1());
155156

156157
$this->setTotals($file, $fileReport->totals());
157158

src/Report/Xml/Node.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function addDirectory(string $name): Directory
5858
return new Directory($dirNode);
5959
}
6060

61-
public function addFile(string $name, string $href): File
61+
public function addFile(string $name, string $href, string $hash): File
6262
{
6363
$fileNode = $this->dom->createElementNS(
6464
Facade::XML_NAMESPACE,
@@ -67,6 +67,7 @@ public function addFile(string $name, string $href): File
6767

6868
$fileNode->setAttribute('name', $name);
6969
$fileNode->setAttribute('href', $href);
70+
$fileNode->setAttribute('hash', $hash);
7071
$this->contextNode()->appendChild($fileNode);
7172

7273
return new File($fileNode);

src/Report/Xml/Report.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@
2121
final class Report extends File
2222
{
2323
private readonly string $name;
24+
private readonly string $sha1;
2425

25-
public function __construct(string $name)
26+
public function __construct(string $name, string $sha1)
2627
{
2728
$dom = new DOMDocument;
2829
$dom->loadXML('<?xml version="1.0" ?><phpunit xmlns="https://schema.phpunit.de/coverage/1.0"><file /></phpunit>');
@@ -35,12 +36,14 @@ public function __construct(string $name)
3536
parent::__construct($contextNode);
3637

3738
$this->name = $name;
39+
$this->sha1 = $sha1;
3840
}
3941

4042
public function asDom(): DOMDocument
4143
{
4244
$this->contextNode()->setAttribute('name', basename($this->name));
4345
$this->contextNode()->setAttribute('path', dirname($this->name));
46+
$this->contextNode()->setAttribute('hash', $this->sha1);
4447

4548
return $this->dom;
4649
}

tests/_files/Report/XML/CoverageForBankAccount/BankAccount.php.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0"?>
22
<phpunit xmlns="https://schema.phpunit.de/coverage/1.0">
3-
<file name="BankAccount.php" path="%e">
3+
<file name="BankAccount.php" path="%e" hash="efd997ec85ed298e59743e5c354216b0ff22e2e5">
44
<totals>
55
<lines total="35" comments="0" code="35" executable="8" executed="5" percent="62.50"/>
66
<methods count="4" tested="3" percent="75.00"/>

tests/_files/Report/XML/CoverageForBankAccount/index.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<classes count="1" tested="0" percent="0.00"/>
2020
<traits count="0" tested="0" percent="0"/>
2121
</totals>
22-
<file name="BankAccount.php" href="BankAccount.php.xml">
22+
<file name="BankAccount.php" href="BankAccount.php.xml" hash="efd997ec85ed298e59743e5c354216b0ff22e2e5">
2323
<totals>
2424
<lines total="35" comments="0" code="35" executable="8" executed="5" percent="62.50"/>
2525
<methods count="4" tested="3" percent="75.00"/>

tests/_files/Report/XML/CoverageForBankAccountWithoutSource/BankAccount.php.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0"?>
22
<phpunit xmlns="https://schema.phpunit.de/coverage/1.0">
3-
<file name="BankAccount.php" path="%e">
3+
<file name="BankAccount.php" path="%e" hash="efd997ec85ed298e59743e5c354216b0ff22e2e5">
44
<totals>
55
<lines total="35" comments="0" code="35" executable="8" executed="5" percent="62.50"/>
66
<methods count="4" tested="3" percent="75.00"/>

tests/_files/Report/XML/CoverageForBankAccountWithoutSource/index.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<classes count="1" tested="0" percent="0.00"/>
2020
<traits count="0" tested="0" percent="0"/>
2121
</totals>
22-
<file name="BankAccount.php" href="BankAccount.php.xml">
22+
<file name="BankAccount.php" href="BankAccount.php.xml" hash="efd997ec85ed298e59743e5c354216b0ff22e2e5">
2323
<totals>
2424
<lines total="35" comments="0" code="35" executable="8" executed="5" percent="62.50"/>
2525
<methods count="4" tested="3" percent="75.00"/>

0 commit comments

Comments
 (0)