Skip to content

Commit eca5d61

Browse files
Closes #519
1 parent 02adea8 commit eca5d61

File tree

3 files changed

+119
-46
lines changed

3 files changed

+119
-46
lines changed

ChangeLog-12.0.md

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes are documented in this file using the [Keep a CHANGELOG](htt
44

55
## [12.0.0] - 2025-02-07
66

7+
### Changed
8+
9+
* `CodeCoverage::stop()` and `CodeCoverage::append()` now expect arguments of type `TargetCollection` instead of `array` to configure code coverage targets
10+
711
### Removed
812

913
* Methods `CodeCoverage::includeUncoveredFiles()` and `CodeCoverage::excludeUncoveredFiles()`

src/CodeCoverage.php

+24-5
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,11 @@ public function start(string $id, ?TestSize $size = null, bool $clear = false):
170170
$this->cachedReport = null;
171171
}
172172

173-
public function stop(bool $append = true, ?TestStatus $status = null, array|false $linesToBeCovered = [], array $linesToBeUsed = []): RawCodeCoverageData
173+
public function stop(bool $append = true, ?TestStatus $status = null, null|false|TargetCollection $covers = null, ?TargetCollection $uses = null): RawCodeCoverageData
174174
{
175175
$data = $this->driver->stop();
176176

177-
$this->append($data, null, $append, $status, $linesToBeCovered, $linesToBeUsed);
177+
$this->append($data, null, $append, $status, $covers, $uses);
178178

179179
$this->currentId = null;
180180
$this->currentSize = null;
@@ -188,7 +188,7 @@ public function stop(bool $append = true, ?TestStatus $status = null, array|fals
188188
* @throws TestIdMissingException
189189
* @throws UnintentionallyCoveredCodeException
190190
*/
191-
public function append(RawCodeCoverageData $rawData, ?string $id = null, bool $append = true, ?TestStatus $status = null, array|false $linesToBeCovered = [], array $linesToBeUsed = []): void
191+
public function append(RawCodeCoverageData $rawData, ?string $id = null, bool $append = true, ?TestStatus $status = null, null|false|TargetCollection $covers = null, ?TargetCollection $uses = null): void
192192
{
193193
if ($id === null) {
194194
$id = $this->currentId;
@@ -198,18 +198,26 @@ public function append(RawCodeCoverageData $rawData, ?string $id = null, bool $a
198198
throw new TestIdMissingException;
199199
}
200200

201-
$this->cachedReport = null;
202-
203201
if ($status === null) {
204202
$status = TestStatus::unknown();
205203
}
206204

205+
if ($covers === null) {
206+
$covers = TargetCollection::fromArray([]);
207+
}
208+
209+
if ($uses === null) {
210+
$uses = TargetCollection::fromArray([]);
211+
}
212+
207213
$size = $this->currentSize;
208214

209215
if ($size === null) {
210216
$size = TestSize::unknown();
211217
}
212218

219+
$this->cachedReport = null;
220+
213221
$this->applyFilter($rawData);
214222

215223
$this->applyExecutableLinesFilter($rawData);
@@ -228,6 +236,17 @@ public function append(RawCodeCoverageData $rawData, ?string $id = null, bool $a
228236
return;
229237
}
230238

239+
$linesToBeCovered = false;
240+
$linesToBeUsed = [];
241+
242+
if ($covers !== false) {
243+
$linesToBeCovered = $this->targetMapper()->mapTargets($covers);
244+
}
245+
246+
if ($linesToBeCovered !== false && $uses !== null) {
247+
$linesToBeUsed = $this->targetMapper()->mapTargets($uses);
248+
}
249+
231250
$this->applyCoversAndUsesFilter(
232251
$rawData,
233252
$linesToBeCovered,

tests/TestCase.php

+91-41
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010
namespace SebastianBergmann\CodeCoverage;
1111

1212
use SebastianBergmann\CodeCoverage\Data\RawCodeCoverageData;
13+
use SebastianBergmann\CodeCoverage\Test\Target\Target;
14+
use SebastianBergmann\CodeCoverage\Test\Target\TargetCollection;
1315
use function array_merge;
1416
use function range;
1517
use function rmdir;
1618
use function unlink;
17-
use BankAccountTest;
19+
use BankAccount;
1820
use RecursiveDirectoryIterator;
1921
use RecursiveIteratorIterator;
2022
use SebastianBergmann\CodeCoverage\Driver\Driver;
@@ -1029,7 +1031,11 @@ protected function getLineCoverageForBankAccount(): CodeCoverage
10291031
$coverage->stop(
10301032
true,
10311033
null,
1032-
[TEST_FILES_PATH . 'BankAccount.php' => range(6, 9)]
1034+
TargetCollection::fromArray(
1035+
[
1036+
Target::forMethod(BankAccount::class, 'getBalance'),
1037+
]
1038+
)
10331039
);
10341040

10351041
$coverage->start(
@@ -1039,7 +1045,11 @@ protected function getLineCoverageForBankAccount(): CodeCoverage
10391045
$coverage->stop(
10401046
true,
10411047
null,
1042-
[TEST_FILES_PATH . 'BankAccount.php' => range(27, 32)]
1048+
TargetCollection::fromArray(
1049+
[
1050+
Target::forMethod(BankAccount::class, 'withdrawMoney'),
1051+
]
1052+
)
10431053
);
10441054

10451055
$coverage->start(
@@ -1049,7 +1059,11 @@ protected function getLineCoverageForBankAccount(): CodeCoverage
10491059
$coverage->stop(
10501060
true,
10511061
null,
1052-
[TEST_FILES_PATH . 'BankAccount.php' => range(20, 25)]
1062+
TargetCollection::fromArray(
1063+
[
1064+
Target::forMethod(BankAccount::class, 'depositMoney'),
1065+
]
1066+
)
10531067
);
10541068

10551069
$coverage->start(
@@ -1059,13 +1073,13 @@ protected function getLineCoverageForBankAccount(): CodeCoverage
10591073
$coverage->stop(
10601074
true,
10611075
null,
1062-
[
1063-
TEST_FILES_PATH . 'BankAccount.php' => array_merge(
1064-
range(6, 9),
1065-
range(20, 25),
1066-
range(27, 32)
1067-
),
1068-
]
1076+
TargetCollection::fromArray(
1077+
[
1078+
Target::forMethod(BankAccount::class, 'getBalance'),
1079+
Target::forMethod(BankAccount::class, 'depositMoney'),
1080+
Target::forMethod(BankAccount::class, 'withdrawMoney'),
1081+
]
1082+
)
10691083
);
10701084

10711085
return $coverage;
@@ -1096,7 +1110,11 @@ protected function getPathCoverageForBankAccount(): CodeCoverage
10961110
$coverage->stop(
10971111
true,
10981112
null,
1099-
[TEST_FILES_PATH . 'BankAccount.php' => range(6, 9)]
1113+
TargetCollection::fromArray(
1114+
[
1115+
Target::forMethod(BankAccount::class, 'getBalance'),
1116+
]
1117+
)
11001118
);
11011119

11021120
$coverage->start(
@@ -1106,7 +1124,11 @@ protected function getPathCoverageForBankAccount(): CodeCoverage
11061124
$coverage->stop(
11071125
true,
11081126
null,
1109-
[TEST_FILES_PATH . 'BankAccount.php' => range(27, 32)]
1127+
TargetCollection::fromArray(
1128+
[
1129+
Target::forMethod(BankAccount::class, 'withdrawMoney'),
1130+
]
1131+
)
11101132
);
11111133

11121134
$coverage->start(
@@ -1116,7 +1138,11 @@ protected function getPathCoverageForBankAccount(): CodeCoverage
11161138
$coverage->stop(
11171139
true,
11181140
null,
1119-
[TEST_FILES_PATH . 'BankAccount.php' => range(20, 25)]
1141+
TargetCollection::fromArray(
1142+
[
1143+
Target::forMethod(BankAccount::class, 'depositMoney'),
1144+
]
1145+
)
11201146
);
11211147

11221148
$coverage->start(
@@ -1126,13 +1152,13 @@ protected function getPathCoverageForBankAccount(): CodeCoverage
11261152
$coverage->stop(
11271153
true,
11281154
null,
1129-
[
1130-
TEST_FILES_PATH . 'BankAccount.php' => array_merge(
1131-
range(6, 9),
1132-
range(20, 25),
1133-
range(27, 32)
1134-
),
1135-
]
1155+
TargetCollection::fromArray(
1156+
[
1157+
Target::forMethod(BankAccount::class, 'getBalance'),
1158+
Target::forMethod(BankAccount::class, 'depositMoney'),
1159+
Target::forMethod(BankAccount::class, 'withdrawMoney'),
1160+
]
1161+
)
11361162
);
11371163

11381164
return $coverage;
@@ -1188,7 +1214,11 @@ protected function getLineCoverageForBankAccountForFirstTwoTests(): CodeCoverage
11881214
$coverage->stop(
11891215
true,
11901216
null,
1191-
[TEST_FILES_PATH . 'BankAccount.php' => range(6, 9)]
1217+
TargetCollection::fromArray(
1218+
[
1219+
Target::forMethod(BankAccount::class, 'getBalance'),
1220+
]
1221+
)
11921222
);
11931223

11941224
$coverage->start(
@@ -1198,7 +1228,11 @@ protected function getLineCoverageForBankAccountForFirstTwoTests(): CodeCoverage
11981228
$coverage->stop(
11991229
true,
12001230
null,
1201-
[TEST_FILES_PATH . 'BankAccount.php' => range(27, 32)]
1231+
TargetCollection::fromArray(
1232+
[
1233+
Target::forMethod(BankAccount::class, 'withdrawMoney'),
1234+
]
1235+
)
12021236
);
12031237

12041238
return $coverage;
@@ -1225,7 +1259,11 @@ protected function getLineCoverageForBankAccountForLastTwoTests(): CodeCoverage
12251259
$coverage->stop(
12261260
true,
12271261
null,
1228-
[TEST_FILES_PATH . 'BankAccount.php' => range(20, 25)]
1262+
TargetCollection::fromArray(
1263+
[
1264+
Target::forMethod(BankAccount::class, 'depositMoney'),
1265+
]
1266+
)
12291267
);
12301268

12311269
$coverage->start(
@@ -1235,13 +1273,13 @@ protected function getLineCoverageForBankAccountForLastTwoTests(): CodeCoverage
12351273
$coverage->stop(
12361274
true,
12371275
null,
1238-
[
1239-
TEST_FILES_PATH . 'BankAccount.php' => array_merge(
1240-
range(6, 9),
1241-
range(20, 25),
1242-
range(27, 32)
1243-
),
1244-
]
1276+
TargetCollection::fromArray(
1277+
[
1278+
Target::forMethod(BankAccount::class, 'getBalance'),
1279+
Target::forMethod(BankAccount::class, 'depositMoney'),
1280+
Target::forMethod(BankAccount::class, 'withdrawMoney'),
1281+
]
1282+
)
12451283
);
12461284

12471285
return $coverage;
@@ -1330,7 +1368,11 @@ protected function getPathCoverageForBankAccountForFirstTwoTests(): CodeCoverage
13301368
$coverage->stop(
13311369
true,
13321370
null,
1333-
[TEST_FILES_PATH . 'BankAccount.php' => range(6, 9)]
1371+
TargetCollection::fromArray(
1372+
[
1373+
Target::forMethod(BankAccount::class, 'getBalance'),
1374+
]
1375+
)
13341376
);
13351377

13361378
$coverage->start(
@@ -1340,7 +1382,11 @@ protected function getPathCoverageForBankAccountForFirstTwoTests(): CodeCoverage
13401382
$coverage->stop(
13411383
true,
13421384
null,
1343-
[TEST_FILES_PATH . 'BankAccount.php' => range(27, 32)]
1385+
TargetCollection::fromArray(
1386+
[
1387+
Target::forMethod(BankAccount::class, 'withdrawMoney'),
1388+
]
1389+
)
13441390
);
13451391

13461392
return $coverage;
@@ -1367,7 +1413,11 @@ protected function getPathCoverageForBankAccountForLastTwoTests(): CodeCoverage
13671413
$coverage->stop(
13681414
true,
13691415
null,
1370-
[TEST_FILES_PATH . 'BankAccount.php' => range(20, 25)]
1416+
TargetCollection::fromArray(
1417+
[
1418+
Target::forMethod(BankAccount::class, 'depositMoney'),
1419+
]
1420+
)
13711421
);
13721422

13731423
$coverage->start(
@@ -1377,13 +1427,13 @@ protected function getPathCoverageForBankAccountForLastTwoTests(): CodeCoverage
13771427
$coverage->stop(
13781428
true,
13791429
null,
1380-
[
1381-
TEST_FILES_PATH . 'BankAccount.php' => array_merge(
1382-
range(6, 9),
1383-
range(20, 25),
1384-
range(27, 32)
1385-
),
1386-
]
1430+
TargetCollection::fromArray(
1431+
[
1432+
Target::forMethod(BankAccount::class, 'getBalance'),
1433+
Target::forMethod(BankAccount::class, 'depositMoney'),
1434+
Target::forMethod(BankAccount::class, 'withdrawMoney'),
1435+
]
1436+
)
13871437
);
13881438

13891439
return $coverage;

0 commit comments

Comments
 (0)