From 01033e44eac3f1f0024243b7cd006ea7f7f4b7db Mon Sep 17 00:00:00 2001 From: Doug Wright Date: Thu, 14 May 2020 20:56:19 +0100 Subject: [PATCH] Use named constructors as requested --- src/CodeCoverage.php | 4 +- src/Driver/PCOV.php | 2 +- src/Driver/PHPDBG.php | 2 +- src/Driver/Xdebug.php | 2 +- .../UnknownCoverageDataFormatException.php | 26 ------------ src/RawCodeCoverageData.php | 26 +++++++----- tests/TestCase.php | 14 +++---- tests/tests/CodeCoverageTest.php | 8 ++-- tests/tests/RawCodeCoverageDataTest.php | 40 ++++--------------- 9 files changed, 40 insertions(+), 84 deletions(-) delete mode 100644 src/Exception/UnknownCoverageDataFormatException.php diff --git a/src/CodeCoverage.php b/src/CodeCoverage.php index e7dcaccef..9c26e729b 100644 --- a/src/CodeCoverage.php +++ b/src/CodeCoverage.php @@ -517,7 +517,7 @@ private function addUncoveredFilesFromWhitelist(): void } } - $this->append(new RawCodeCoverageData($data), 'UNCOVERED_FILES_FROM_WHITELIST'); + $this->append(RawCodeCoverageData::fromXdebugWithoutPathCoverage($data), 'UNCOVERED_FILES_FROM_WHITELIST'); } private function getLinesToBeIgnored(string $fileName): array @@ -887,7 +887,7 @@ private function initializeData(): void $data[$file] = $fileCoverage; } - $this->append(new RawCodeCoverageData($data), 'UNCOVERED_FILES_FROM_WHITELIST'); + $this->append(RawCodeCoverageData::fromXdebugWithoutPathCoverage($data), 'UNCOVERED_FILES_FROM_WHITELIST'); } } diff --git a/src/Driver/PCOV.php b/src/Driver/PCOV.php index af830c34c..379625494 100644 --- a/src/Driver/PCOV.php +++ b/src/Driver/PCOV.php @@ -46,6 +46,6 @@ public function stop(): RawCodeCoverageData \pcov\clear(); - return new RawCodeCoverageData($collect); + return RawCodeCoverageData::fromXdebugWithoutPathCoverage($collect); } } diff --git a/src/Driver/PHPDBG.php b/src/Driver/PHPDBG.php index 57871d6cb..126e846e7 100644 --- a/src/Driver/PHPDBG.php +++ b/src/Driver/PHPDBG.php @@ -72,7 +72,7 @@ public function stop(): RawCodeCoverageData $fetchedLines = \array_merge($fetchedLines, $sourceLines); - return new RawCodeCoverageData($this->detectExecutedLines($fetchedLines, $dbgData)); + return RawCodeCoverageData::fromXdebugWithoutPathCoverage($this->detectExecutedLines($fetchedLines, $dbgData)); } /** diff --git a/src/Driver/Xdebug.php b/src/Driver/Xdebug.php index 1264b21eb..672ce0e70 100644 --- a/src/Driver/Xdebug.php +++ b/src/Driver/Xdebug.php @@ -55,6 +55,6 @@ public function stop(): RawCodeCoverageData \xdebug_stop_code_coverage(); - return new RawCodeCoverageData($data); + return RawCodeCoverageData::fromXdebugWithoutPathCoverage($data); } } diff --git a/src/Exception/UnknownCoverageDataFormatException.php b/src/Exception/UnknownCoverageDataFormatException.php deleted file mode 100644 index f8cd7dc5a..000000000 --- a/src/Exception/UnknownCoverageDataFormatException.php +++ /dev/null @@ -1,26 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ -namespace SebastianBergmann\CodeCoverage; - -/** - * Exception that is raised when a driver supplies coverage data in a format that cannot be handled. - */ -final class UnknownCoverageDataFormatException extends RuntimeException -{ - public static function create(string $filename): self - { - return new self( - \sprintf( - 'Coverage data for file "%s" must be in Xdebug-compatible format, see https://xdebug.org/docs/code_coverage', - $filename - ) - ); - } -} diff --git a/src/RawCodeCoverageData.php b/src/RawCodeCoverageData.php index 6f3af62a8..bd2ab653d 100644 --- a/src/RawCodeCoverageData.php +++ b/src/RawCodeCoverageData.php @@ -21,19 +21,25 @@ final class RawCodeCoverageData */ private $lineCoverage = []; - public function __construct(array $rawCoverage = []) + public static function fromXdebugWithoutPathCoverage(array $rawCoverage): self { - foreach ($rawCoverage as $file => $fileCoverageData) { - $hasOnlyIntegerKeys = \count(\array_filter(\array_keys($fileCoverageData), 'is_int')) === \count($fileCoverageData); + return new self($rawCoverage); + } - if ($hasOnlyIntegerKeys) { - $this->lineCoverage[$file] = $fileCoverageData; - } elseif (\count($fileCoverageData) === 2 && isset($fileCoverageData['lines'], $fileCoverageData['functions'])) { - $this->lineCoverage[$file] = $fileCoverageData['lines']; - } else { - throw UnknownCoverageDataFormatException::create($file); - } + public static function fromXdebugWithPathCoverage(array $rawCoverage): self + { + $lineCoverage = []; + + foreach ($rawCoverage as $file => $fileCoverageData) { + $lineCoverage[$file] = $fileCoverageData['lines']; } + + return new self($lineCoverage); + } + + private function __construct(array $lineCoverage) + { + $this->lineCoverage = $lineCoverage; } public function clear(): void diff --git a/tests/TestCase.php b/tests/TestCase.php index 0120f2e71..c6c68df59 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -23,7 +23,7 @@ public static function setUpBeforeClass(): void protected function getXdebugDataForBankAccount() { return [ - new RawCodeCoverageData([ + RawCodeCoverageData::fromXdebugWithoutPathCoverage([ TEST_FILES_PATH . 'BankAccount.php' => [ 8 => 1, 9 => -2, @@ -40,7 +40,7 @@ protected function getXdebugDataForBankAccount() 32 => -2, ], ]), - new RawCodeCoverageData([ + RawCodeCoverageData::fromXdebugWithoutPathCoverage([ TEST_FILES_PATH . 'BankAccount.php' => [ 8 => 1, 13 => 1, @@ -48,7 +48,7 @@ protected function getXdebugDataForBankAccount() 29 => 1, ], ]), - new RawCodeCoverageData([ + RawCodeCoverageData::fromXdebugWithoutPathCoverage([ TEST_FILES_PATH . 'BankAccount.php' => [ 8 => 1, 13 => 1, @@ -56,7 +56,7 @@ protected function getXdebugDataForBankAccount() 22 => 1, ], ]), - new RawCodeCoverageData([ + RawCodeCoverageData::fromXdebugWithoutPathCoverage([ TEST_FILES_PATH . 'BankAccount.php' => [ 8 => 1, 13 => 1, @@ -314,7 +314,7 @@ protected function setUpXdebugStubForFileWithIgnoredLines(): Driver $stub->expects($this->any()) ->method('stop') ->will($this->returnValue( - new RawCodeCoverageData( + RawCodeCoverageData::fromXdebugWithoutPathCoverage( [ TEST_FILES_PATH . 'source_with_ignore.php' => [ 2 => 1, @@ -352,7 +352,7 @@ protected function setUpXdebugStubForClassWithAnonymousFunction(): Driver $stub->expects($this->any()) ->method('stop') ->will($this->returnValue( - new RawCodeCoverageData( + RawCodeCoverageData::fromXdebugWithoutPathCoverage( [ TEST_FILES_PATH . 'source_with_class_and_anonymous_function.php' => [ 7 => 1, @@ -390,7 +390,7 @@ protected function setUpXdebugStubForCrashParsing(): Driver $stub->expects($this->any()) ->method('stop') - ->will($this->returnValue(new RawCodeCoverageData([]))); + ->will($this->returnValue(RawCodeCoverageData::fromXdebugWithoutPathCoverage([]))); return $stub; } diff --git a/tests/tests/CodeCoverageTest.php b/tests/tests/CodeCoverageTest.php index da5944cb3..5574d360d 100644 --- a/tests/tests/CodeCoverageTest.php +++ b/tests/tests/CodeCoverageTest.php @@ -44,7 +44,7 @@ public function testCannotAppendWithInvalidArgument(): void { $this->expectException(Exception::class); - $this->coverage->append(new RawCodeCoverageData([]), null); + $this->coverage->append(RawCodeCoverageData::fromXdebugWithoutPathCoverage([]), null); } public function testCollect(): void @@ -71,7 +71,7 @@ public function testWhitelistFiltering(): void { $this->coverage->filter()->addFileToWhitelist(TEST_FILES_PATH . 'BankAccount.php'); - $data = new RawCodeCoverageData([ + $data = RawCodeCoverageData::fromXdebugWithoutPathCoverage([ TEST_FILES_PATH . 'BankAccount.php' => [ 29 => -1, 31 => -1, @@ -310,7 +310,7 @@ public function testAppendThrowsExceptionIfCoveredCodeWasNotExecuted(): void $this->coverage->filter()->addDirectoryToWhitelist(TEST_FILES_PATH); $this->coverage->setCheckForUnexecutedCoveredCode(true); - $data = new RawCodeCoverageData([ + $data = RawCodeCoverageData::fromXdebugWithoutPathCoverage([ TEST_FILES_PATH . 'BankAccount.php' => [ 29 => -1, 31 => -1, @@ -336,7 +336,7 @@ public function testAppendThrowsExceptionIfUsedCodeWasNotExecuted(): void $this->coverage->filter()->addDirectoryToWhitelist(TEST_FILES_PATH); $this->coverage->setCheckForUnexecutedCoveredCode(true); - $data = new RawCodeCoverageData([ + $data = RawCodeCoverageData::fromXdebugWithoutPathCoverage([ TEST_FILES_PATH . 'BankAccount.php' => [ 29 => -1, 31 => -1, diff --git a/tests/tests/RawCodeCoverageDataTest.php b/tests/tests/RawCodeCoverageDataTest.php index d21d52e50..8f7dfef5e 100644 --- a/tests/tests/RawCodeCoverageDataTest.php +++ b/tests/tests/RawCodeCoverageDataTest.php @@ -24,14 +24,14 @@ public function testLineDataFromStandardXDebugFormat(): void ], ]; - $dataObject = new RawCodeCoverageData($lineDataFromDriver); + $dataObject = RawCodeCoverageData::fromXdebugWithoutPathCoverage($lineDataFromDriver); $this->assertEquals($lineDataFromDriver, $dataObject->getLineCoverage()); } /** - * In the branch-check XDebug format, the line data exists inside a "lines" array key. + * In the path-coverage XDebug format, the line data exists inside a "lines" array key. */ - public function testLineDataFromBranchCheckXDebugFormat(): void + public function testLineDataFromPathCoverageXDebugFormat(): void { $rawDataFromDriver = [ '/some/path/SomeClass.php' => [ @@ -54,34 +54,10 @@ public function testLineDataFromBranchCheckXDebugFormat(): void ], ]; - $dataObject = new RawCodeCoverageData($rawDataFromDriver); + $dataObject = RawCodeCoverageData::fromXdebugWithPathCoverage($rawDataFromDriver); $this->assertEquals($lineData, $dataObject->getLineCoverage()); } - /** - * Coverage data that does not match a known format should throw an exception. - */ - public function testDataFromUnknownFormat(): void - { - $this->expectException(UnknownCoverageDataFormatException::class); - - $lineDataFromDriver = [ - '/some/path/SomeClass.php' => [ - 'executedLines' => [ - 8, - ], - 'unExecutedLines' => [ - 13, - ], - 'nonExecutableLines' => [ - 9, - ], - ], - ]; - - $dataObject = new RawCodeCoverageData($lineDataFromDriver); - } - public function testClear(): void { $lineDataFromDriver = [ @@ -92,7 +68,7 @@ public function testClear(): void ], ]; - $dataObject = new RawCodeCoverageData($lineDataFromDriver); + $dataObject = RawCodeCoverageData::fromXdebugWithoutPathCoverage($lineDataFromDriver); $dataObject->clear(); $this->assertEmpty($dataObject->getLineCoverage()); } @@ -130,7 +106,7 @@ public function testRemoveCoverageDataForFile(): void ], ]; - $dataObject = new RawCodeCoverageData($lineDataFromDriver); + $dataObject = RawCodeCoverageData::fromXdebugWithoutPathCoverage($lineDataFromDriver); $dataObject->removeCoverageDataForFile('/some/path/SomeOtherClass.php'); $this->assertEquals($expectedFilterResult, $dataObject->getLineCoverage()); } @@ -167,7 +143,7 @@ public function testKeepCoverageDataOnlyForLines(): void ], ]; - $dataObject = new RawCodeCoverageData($lineDataFromDriver); + $dataObject = RawCodeCoverageData::fromXdebugWithoutPathCoverage($lineDataFromDriver); $dataObject->keepCoverageDataOnlyForLines('/some/path/SomeClass.php', [9, 13]); $dataObject->keepCoverageDataOnlyForLines('/some/path/SomeOtherClass.php', [999]); $dataObject->keepCoverageDataOnlyForLines('/some/path/AnotherClass.php', [28]); @@ -209,7 +185,7 @@ public function testRemoveCoverageDataForLines(): void ], ]; - $dataObject = new RawCodeCoverageData($lineDataFromDriver); + $dataObject = RawCodeCoverageData::fromXdebugWithoutPathCoverage($lineDataFromDriver); $dataObject->removeCoverageDataForLines('/some/path/SomeClass.php', [9, 13]); $dataObject->removeCoverageDataForLines('/some/path/SomeOtherClass.php', [999]); $dataObject->removeCoverageDataForLines('/some/path/AnotherClass.php', [28]);