forked from sebastianbergmann/php-code-coverage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMerger.php
More file actions
123 lines (104 loc) · 4.4 KB
/
Copy pathMerger.php
File metadata and controls
123 lines (104 loc) · 4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php declare(strict_types=1);
/*
* This file is part of phpunit/php-code-coverage.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\CodeCoverage\Serialization;
use function array_key_exists;
use function array_merge;
use function array_slice;
use DateTimeImmutable;
use SebastianBergmann\CodeCoverage\Version;
/**
* @phpstan-import-type SerializedCoverage from Serializer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for phpunit/php-code-coverage
*/
final readonly class Merger
{
/**
* @param list<non-empty-string> $paths
*
* @throws DriverMismatchException
* @throws EmptyPathListException
* @throws GitInformationMismatchException
* @throws MixedGitInformationException
* @throws RuntimeMismatchException
*
* @return SerializedCoverage
*/
public function merge(array $paths, bool $requireMatchingGitInformation = true, bool $requireMatchingPhpVersion = true, bool $requireMatchingCodeCoverageDriver = true): array
{
if ($paths === []) {
throw new EmptyPathListException;
}
$unserializer = new Unserializer;
$first = $unserializer->unserialize($paths[0]);
$refRuntime = $first['buildInformation']['runtime'];
$refDriver = $first['buildInformation']['phpCodeCoverage']['driverInformation'];
$refHasGit = array_key_exists('git', $first['buildInformation']);
$refGit = $first['buildInformation']['git'] ?? null;
$mergedCoverage = clone $first['codeCoverage'];
$mergedTestResults = [$first['testResults']];
foreach (array_slice($paths, 1) as $path) {
$item = $unserializer->unserialize($path);
$runtime = $item['buildInformation']['runtime'];
if ($requireMatchingPhpVersion) {
if ($runtime['name'] !== $refRuntime['name'] || $runtime['version'] !== $refRuntime['version']) {
throw new RuntimeMismatchException;
}
}
if ($requireMatchingCodeCoverageDriver) {
$driver = $item['buildInformation']['phpCodeCoverage']['driverInformation'];
if ($driver['name'] !== $refDriver['name'] || $driver['version'] !== $refDriver['version']) {
throw new DriverMismatchException;
}
}
if ($requireMatchingGitInformation) {
$hasGit = array_key_exists('git', $item['buildInformation']);
if ($hasGit !== $refHasGit) {
throw new MixedGitInformationException;
}
if ($hasGit) {
$git = $item['buildInformation']['git'];
foreach (['originUrl', 'branch', 'commit', 'status'] as $field) {
if ($git[$field] !== $refGit[$field]) {
throw new GitInformationMismatchException($field, (string) $refGit[$field], (string) $git[$field]);
}
}
if ($git['isClean'] !== $refGit['isClean']) {
throw new GitInformationMismatchException(
'isClean',
$refGit['isClean'] ? 'true' : 'false',
$git['isClean'] ? 'true' : 'false',
);
}
}
}
$mergedCoverage->merge($item['codeCoverage']);
$mergedTestResults[] = $item['testResults'];
}
$buildInformation = [
'timestamp' => (new DateTimeImmutable)->format('D M j G:i:s T Y'),
'runtime' => $refRuntime,
'phpCodeCoverage' => [
'version' => Version::id(),
'serializationFormat' => Serializer::SERIALIZATION_FORMAT,
'driverInformation' => $refDriver,
],
];
if ($refHasGit) {
$buildInformation['git'] = $refGit;
}
return [
'buildInformation' => $buildInformation,
'basePath' => $first['basePath'],
'codeCoverage' => $mergedCoverage,
'testResults' => array_merge(...$mergedTestResults),
];
}
}