Skip to content

Commit 118ec2d

Browse files
committed
Use named constructors as requested
1 parent ea95590 commit 118ec2d

9 files changed

+42
-84
lines changed

src/CodeCoverage.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ private function addUncoveredFilesFromWhitelist(): void
517517
}
518518
}
519519

520-
$this->append(new RawCodeCoverageData($data), 'UNCOVERED_FILES_FROM_WHITELIST');
520+
$this->append(RawCodeCoverageData::fromXdebugWithoutPathCoverage($data), 'UNCOVERED_FILES_FROM_WHITELIST');
521521
}
522522

523523
private function getLinesToBeIgnored(string $fileName): array
@@ -887,7 +887,7 @@ private function initializeData(): void
887887
$data[$file] = $fileCoverage;
888888
}
889889

890-
$this->append(new RawCodeCoverageData($data), 'UNCOVERED_FILES_FROM_WHITELIST');
890+
$this->append(RawCodeCoverageData::fromXdebugWithoutPathCoverage($data), 'UNCOVERED_FILES_FROM_WHITELIST');
891891
}
892892
}
893893

src/Driver/PCOV.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,6 @@ public function stop(): RawCodeCoverageData
4646

4747
\pcov\clear();
4848

49-
return new RawCodeCoverageData($collect);
49+
return RawCodeCoverageData::fromXdebugWithoutPathCoverage($collect);
5050
}
5151
}

src/Driver/PHPDBG.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function stop(): RawCodeCoverageData
7272

7373
$fetchedLines = \array_merge($fetchedLines, $sourceLines);
7474

75-
return new RawCodeCoverageData($this->detectExecutedLines($fetchedLines, $dbgData));
75+
return RawCodeCoverageData::fromXdebugWithoutPathCoverage($this->detectExecutedLines($fetchedLines, $dbgData));
7676
}
7777

7878
/**

src/Driver/Xdebug.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,6 @@ public function stop(): RawCodeCoverageData
5555

5656
\xdebug_stop_code_coverage();
5757

58-
return new RawCodeCoverageData($data);
58+
return RawCodeCoverageData::fromXdebugWithoutPathCoverage($data);
5959
}
6060
}

src/Exception/UnknownCoverageDataFormatException.php

-26
This file was deleted.

src/RawCodeCoverageData.php

+18-10
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,27 @@ final class RawCodeCoverageData
2121
*/
2222
private $lineCoverage = [];
2323

24-
public function __construct(array $rawCoverage = [])
24+
public static function fromXdebugWithoutPathCoverage(array $rawCoverage): self
2525
{
26-
foreach ($rawCoverage as $file => $fileCoverageData) {
27-
$hasOnlyIntegerKeys = \count(\array_filter(\array_keys($fileCoverageData), 'is_int')) === \count($fileCoverageData);
26+
$coverage = new self();
27+
$coverage->lineCoverage = $rawCoverage;
28+
29+
return $coverage;
30+
}
31+
32+
public static function fromXdebugWithPathCoverage(array $rawCoverage): self
33+
{
34+
$coverage = new self();
2835

29-
if ($hasOnlyIntegerKeys) {
30-
$this->lineCoverage[$file] = $fileCoverageData;
31-
} elseif (\count($fileCoverageData) === 2 && isset($fileCoverageData['lines'], $fileCoverageData['functions'])) {
32-
$this->lineCoverage[$file] = $fileCoverageData['lines'];
33-
} else {
34-
throw UnknownCoverageDataFormatException::create($file);
35-
}
36+
foreach ($rawCoverage as $file => $fileCoverageData) {
37+
$coverage->lineCoverage[$file] = $fileCoverageData['lines'];
3638
}
39+
40+
return $coverage;
41+
}
42+
43+
private function __construct()
44+
{
3745
}
3846

3947
public function clear(): void

tests/TestCase.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public static function setUpBeforeClass(): void
2323
protected function getXdebugDataForBankAccount()
2424
{
2525
return [
26-
new RawCodeCoverageData([
26+
RawCodeCoverageData::fromXdebugWithoutPathCoverage([
2727
TEST_FILES_PATH . 'BankAccount.php' => [
2828
8 => 1,
2929
9 => -2,
@@ -40,23 +40,23 @@ protected function getXdebugDataForBankAccount()
4040
32 => -2,
4141
],
4242
]),
43-
new RawCodeCoverageData([
43+
RawCodeCoverageData::fromXdebugWithoutPathCoverage([
4444
TEST_FILES_PATH . 'BankAccount.php' => [
4545
8 => 1,
4646
13 => 1,
4747
16 => 1,
4848
29 => 1,
4949
],
5050
]),
51-
new RawCodeCoverageData([
51+
RawCodeCoverageData::fromXdebugWithoutPathCoverage([
5252
TEST_FILES_PATH . 'BankAccount.php' => [
5353
8 => 1,
5454
13 => 1,
5555
16 => 1,
5656
22 => 1,
5757
],
5858
]),
59-
new RawCodeCoverageData([
59+
RawCodeCoverageData::fromXdebugWithoutPathCoverage([
6060
TEST_FILES_PATH . 'BankAccount.php' => [
6161
8 => 1,
6262
13 => 1,
@@ -314,7 +314,7 @@ protected function setUpXdebugStubForFileWithIgnoredLines(): Driver
314314
$stub->expects($this->any())
315315
->method('stop')
316316
->will($this->returnValue(
317-
new RawCodeCoverageData(
317+
RawCodeCoverageData::fromXdebugWithoutPathCoverage(
318318
[
319319
TEST_FILES_PATH . 'source_with_ignore.php' => [
320320
2 => 1,
@@ -352,7 +352,7 @@ protected function setUpXdebugStubForClassWithAnonymousFunction(): Driver
352352
$stub->expects($this->any())
353353
->method('stop')
354354
->will($this->returnValue(
355-
new RawCodeCoverageData(
355+
RawCodeCoverageData::fromXdebugWithoutPathCoverage(
356356
[
357357
TEST_FILES_PATH . 'source_with_class_and_anonymous_function.php' => [
358358
7 => 1,
@@ -390,7 +390,7 @@ protected function setUpXdebugStubForCrashParsing(): Driver
390390

391391
$stub->expects($this->any())
392392
->method('stop')
393-
->will($this->returnValue(new RawCodeCoverageData([])));
393+
->will($this->returnValue(RawCodeCoverageData::fromXdebugWithoutPathCoverage([])));
394394

395395
return $stub;
396396
}

tests/tests/CodeCoverageTest.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function testCannotAppendWithInvalidArgument(): void
4444
{
4545
$this->expectException(Exception::class);
4646

47-
$this->coverage->append(new RawCodeCoverageData([]), null);
47+
$this->coverage->append(RawCodeCoverageData::fromXdebugWithoutPathCoverage([]), null);
4848
}
4949

5050
public function testCollect(): void
@@ -71,7 +71,7 @@ public function testWhitelistFiltering(): void
7171
{
7272
$this->coverage->filter()->addFileToWhitelist(TEST_FILES_PATH . 'BankAccount.php');
7373

74-
$data = new RawCodeCoverageData([
74+
$data = RawCodeCoverageData::fromXdebugWithoutPathCoverage([
7575
TEST_FILES_PATH . 'BankAccount.php' => [
7676
29 => -1,
7777
31 => -1,
@@ -310,7 +310,7 @@ public function testAppendThrowsExceptionIfCoveredCodeWasNotExecuted(): void
310310
$this->coverage->filter()->addDirectoryToWhitelist(TEST_FILES_PATH);
311311
$this->coverage->setCheckForUnexecutedCoveredCode(true);
312312

313-
$data = new RawCodeCoverageData([
313+
$data = RawCodeCoverageData::fromXdebugWithoutPathCoverage([
314314
TEST_FILES_PATH . 'BankAccount.php' => [
315315
29 => -1,
316316
31 => -1,
@@ -336,7 +336,7 @@ public function testAppendThrowsExceptionIfUsedCodeWasNotExecuted(): void
336336
$this->coverage->filter()->addDirectoryToWhitelist(TEST_FILES_PATH);
337337
$this->coverage->setCheckForUnexecutedCoveredCode(true);
338338

339-
$data = new RawCodeCoverageData([
339+
$data = RawCodeCoverageData::fromXdebugWithoutPathCoverage([
340340
TEST_FILES_PATH . 'BankAccount.php' => [
341341
29 => -1,
342342
31 => -1,

tests/tests/RawCodeCoverageDataTest.php

+8-32
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ public function testLineDataFromStandardXDebugFormat(): void
2424
],
2525
];
2626

27-
$dataObject = new RawCodeCoverageData($lineDataFromDriver);
27+
$dataObject = RawCodeCoverageData::fromXdebugWithoutPathCoverage($lineDataFromDriver);
2828
$this->assertEquals($lineDataFromDriver, $dataObject->getLineCoverage());
2929
}
3030

3131
/**
32-
* In the branch-check XDebug format, the line data exists inside a "lines" array key.
32+
* In the path-coverage XDebug format, the line data exists inside a "lines" array key.
3333
*/
34-
public function testLineDataFromBranchCheckXDebugFormat(): void
34+
public function testLineDataFromPathCoverageXDebugFormat(): void
3535
{
3636
$rawDataFromDriver = [
3737
'/some/path/SomeClass.php' => [
@@ -54,34 +54,10 @@ public function testLineDataFromBranchCheckXDebugFormat(): void
5454
],
5555
];
5656

57-
$dataObject = new RawCodeCoverageData($rawDataFromDriver);
57+
$dataObject = RawCodeCoverageData::fromXdebugWithPathCoverage($rawDataFromDriver);
5858
$this->assertEquals($lineData, $dataObject->getLineCoverage());
5959
}
6060

61-
/**
62-
* Coverage data that does not match a known format should throw an exception.
63-
*/
64-
public function testDataFromUnknownFormat(): void
65-
{
66-
$this->expectException(UnknownCoverageDataFormatException::class);
67-
68-
$lineDataFromDriver = [
69-
'/some/path/SomeClass.php' => [
70-
'executedLines' => [
71-
8,
72-
],
73-
'unExecutedLines' => [
74-
13,
75-
],
76-
'nonExecutableLines' => [
77-
9,
78-
],
79-
],
80-
];
81-
82-
$dataObject = new RawCodeCoverageData($lineDataFromDriver);
83-
}
84-
8561
public function testClear(): void
8662
{
8763
$lineDataFromDriver = [
@@ -92,7 +68,7 @@ public function testClear(): void
9268
],
9369
];
9470

95-
$dataObject = new RawCodeCoverageData($lineDataFromDriver);
71+
$dataObject = RawCodeCoverageData::fromXdebugWithoutPathCoverage($lineDataFromDriver);
9672
$dataObject->clear();
9773
$this->assertEmpty($dataObject->getLineCoverage());
9874
}
@@ -130,7 +106,7 @@ public function testRemoveCoverageDataForFile(): void
130106
],
131107
];
132108

133-
$dataObject = new RawCodeCoverageData($lineDataFromDriver);
109+
$dataObject = RawCodeCoverageData::fromXdebugWithoutPathCoverage($lineDataFromDriver);
134110
$dataObject->removeCoverageDataForFile('/some/path/SomeOtherClass.php');
135111
$this->assertEquals($expectedFilterResult, $dataObject->getLineCoverage());
136112
}
@@ -167,7 +143,7 @@ public function testKeepCoverageDataOnlyForLines(): void
167143
],
168144
];
169145

170-
$dataObject = new RawCodeCoverageData($lineDataFromDriver);
146+
$dataObject = RawCodeCoverageData::fromXdebugWithoutPathCoverage($lineDataFromDriver);
171147
$dataObject->keepCoverageDataOnlyForLines('/some/path/SomeClass.php', [9, 13]);
172148
$dataObject->keepCoverageDataOnlyForLines('/some/path/SomeOtherClass.php', [999]);
173149
$dataObject->keepCoverageDataOnlyForLines('/some/path/AnotherClass.php', [28]);
@@ -209,7 +185,7 @@ public function testRemoveCoverageDataForLines(): void
209185
],
210186
];
211187

212-
$dataObject = new RawCodeCoverageData($lineDataFromDriver);
188+
$dataObject = RawCodeCoverageData::fromXdebugWithoutPathCoverage($lineDataFromDriver);
213189
$dataObject->removeCoverageDataForLines('/some/path/SomeClass.php', [9, 13]);
214190
$dataObject->removeCoverageDataForLines('/some/path/SomeOtherClass.php', [999]);
215191
$dataObject->removeCoverageDataForLines('/some/path/AnotherClass.php', [28]);

0 commit comments

Comments
 (0)