From 842f72662d6b9edda84c4b6f13885fd9cd53dc63 Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Sat, 2 Mar 2024 08:22:05 +0100 Subject: [PATCH 1/4] Do not use implicitly nullable parameters and prepare release --- ChangeLog-10.1.md | 7 +++++++ src/CodeCoverage.php | 6 +++--- src/Node/AbstractNode.php | 2 +- src/Version.php | 2 +- 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/ChangeLog-10.1.md b/ChangeLog-10.1.md index b7299e7b4..603efad46 100644 --- a/ChangeLog-10.1.md +++ b/ChangeLog-10.1.md @@ -2,6 +2,12 @@ All notable changes are documented in this file using the [Keep a CHANGELOG](http://keepachangelog.com/) principles. +## [10.1.12] - 2024-03-02 + +### Changed + +* Do not use implicitly nullable parameters + ## [10.1.11] - 2023-12-21 ### Changed @@ -83,6 +89,7 @@ All notable changes are documented in this file using the [Keep a CHANGELOG](htt * The `SebastianBergmann\CodeCoverage\Filter::includeDirectory()`, `SebastianBergmann\CodeCoverage\Filter::excludeDirectory()`, and `SebastianBergmann\CodeCoverage\Filter::excludeFile()` methods are now deprecated +[10.1.12]: https://github.com/sebastianbergmann/php-code-coverage/compare/10.1.11...10.1.12 [10.1.11]: https://github.com/sebastianbergmann/php-code-coverage/compare/10.1.10...10.1.11 [10.1.10]: https://github.com/sebastianbergmann/php-code-coverage/compare/10.1.9...10.1.10 [10.1.9]: https://github.com/sebastianbergmann/php-code-coverage/compare/10.1.8...10.1.9 diff --git a/src/CodeCoverage.php b/src/CodeCoverage.php index 0fe09e277..805fd8223 100644 --- a/src/CodeCoverage.php +++ b/src/CodeCoverage.php @@ -160,7 +160,7 @@ public function setTests(array $tests): void $this->tests = $tests; } - public function start(string $id, TestSize $size = null, bool $clear = false): void + public function start(string $id, ?TestSize $size = null, bool $clear = false): void { if ($clear) { $this->clear(); @@ -177,7 +177,7 @@ public function start(string $id, TestSize $size = null, bool $clear = false): v /** * @psalm-param array> $linesToBeIgnored */ - public function stop(bool $append = true, TestStatus $status = null, array|false $linesToBeCovered = [], array $linesToBeUsed = [], array $linesToBeIgnored = []): RawCodeCoverageData + public function stop(bool $append = true, ?TestStatus $status = null, array|false $linesToBeCovered = [], array $linesToBeUsed = [], array $linesToBeIgnored = []): RawCodeCoverageData { $data = $this->driver->stop(); @@ -202,7 +202,7 @@ public function stop(bool $append = true, TestStatus $status = null, array|false * @throws TestIdMissingException * @throws UnintentionallyCoveredCodeException */ - public function append(RawCodeCoverageData $rawData, string $id = null, bool $append = true, TestStatus $status = null, array|false $linesToBeCovered = [], array $linesToBeUsed = [], array $linesToBeIgnored = []): void + public function append(RawCodeCoverageData $rawData, ?string $id = null, bool $append = true, ?TestStatus $status = null, array|false $linesToBeCovered = [], array $linesToBeUsed = [], array $linesToBeIgnored = []): void { if ($id === null) { $id = $this->currentId; diff --git a/src/Node/AbstractNode.php b/src/Node/AbstractNode.php index 6338ed3d0..3f21a50e7 100644 --- a/src/Node/AbstractNode.php +++ b/src/Node/AbstractNode.php @@ -33,7 +33,7 @@ abstract class AbstractNode implements Countable private readonly ?AbstractNode $parent; private string $id; - public function __construct(string $name, self $parent = null) + public function __construct(string $name, ?self $parent = null) { if (str_ends_with($name, DIRECTORY_SEPARATOR)) { $name = substr($name, 0, -1); diff --git a/src/Version.php b/src/Version.php index c7739dc6e..41f657710 100644 --- a/src/Version.php +++ b/src/Version.php @@ -19,7 +19,7 @@ final class Version public static function id(): string { if (self::$version === '') { - self::$version = (new VersionId('10.1.11', dirname(__DIR__)))->asString(); + self::$version = (new VersionId('10.1.12', dirname(__DIR__)))->asString(); } return self::$version; From c600ab6c54ca4f4f4b2b23c4ebd4c5a930d3b7ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=B6ller?= Date: Sat, 9 Mar 2024 15:44:42 +0100 Subject: [PATCH 2/4] Fix: Do not pad lines of code coverage report --- src/Report/Text.php | 33 ++++++++----------- tests/_files/BankAccount-text-line.txt | 6 ++-- tests/_files/BankAccount-text-path.txt | 6 ++-- tests/_files/BankAccount-text-summary.txt | 6 ++-- .../BankAccountWithUncovered-text-line.txt | 10 +++--- .../BankAccountWithoutUncovered-text-line.txt | 6 ++-- tests/_files/NamespacedBankAccount-text.txt | 6 ++-- .../class-with-anonymous-function-text.txt | 6 ++-- tests/_files/ignored-lines-text.txt | 10 +++--- 9 files changed, 41 insertions(+), 48 deletions(-) diff --git a/src/Report/Text.php b/src/Report/Text.php index ab8daae73..56b237465 100644 --- a/src/Report/Text.php +++ b/src/Report/Text.php @@ -10,13 +10,9 @@ namespace SebastianBergmann\CodeCoverage\Report; use const PHP_EOL; -use function array_map; use function date; use function ksort; -use function max; use function sprintf; -use function str_pad; -use function strlen; use SebastianBergmann\CodeCoverage\CodeCoverage; use SebastianBergmann\CodeCoverage\Node\File; use SebastianBergmann\CodeCoverage\Util\Percentage; @@ -160,31 +156,28 @@ public function process(CodeCoverage $coverage, bool $showColors = false): strin $report->numberOfExecutableLines(), ); - $padding = max(array_map('strlen', [$classes, $methods, $lines])); - if ($this->showOnlySummary) { - $title = 'Code Coverage Report Summary:'; - $padding = max($padding, strlen($title)); + $title = 'Code Coverage Report Summary:'; - $output .= $this->format($colors['header'], $padding, $title); + $output .= $this->format($colors['header'], $title); } else { $date = date(' Y-m-d H:i:s'); $title = 'Code Coverage Report:'; - $output .= $this->format($colors['header'], $padding, $title); - $output .= $this->format($colors['header'], $padding, $date); - $output .= $this->format($colors['header'], $padding, ''); - $output .= $this->format($colors['header'], $padding, ' Summary:'); + $output .= $this->format($colors['header'], $title); + $output .= $this->format($colors['header'], $date); + $output .= $this->format($colors['header'], ''); + $output .= $this->format($colors['header'], ' Summary:'); } - $output .= $this->format($colors['classes'], $padding, $classes); - $output .= $this->format($colors['methods'], $padding, $methods); + $output .= $this->format($colors['classes'], $classes); + $output .= $this->format($colors['methods'], $methods); if ($hasBranchCoverage) { - $output .= $this->format($colors['paths'], $padding, $paths); - $output .= $this->format($colors['branches'], $padding, $branches); + $output .= $this->format($colors['paths'], $paths); + $output .= $this->format($colors['branches'], $branches); } - $output .= $this->format($colors['lines'], $padding, $lines); + $output .= $this->format($colors['lines'], $lines); if ($this->showOnlySummary) { return $output . PHP_EOL; @@ -304,10 +297,10 @@ private function printCoverageCounts(int $numberOfCoveredElements, int $totalNum sprintf($format, $totalNumberOfElements) . ')'; } - private function format(string $color, int $padding, false|string $string): string + private function format(string $color, false|string $string): string { $reset = $color ? self::COLOR_RESET : ''; - return $color . str_pad((string) $string, $padding) . $reset . PHP_EOL; + return $color . (string) $string . $reset . PHP_EOL; } } diff --git a/tests/_files/BankAccount-text-line.txt b/tests/_files/BankAccount-text-line.txt index 4f188c76c..cbc92cdab 100644 --- a/tests/_files/BankAccount-text-line.txt +++ b/tests/_files/BankAccount-text-line.txt @@ -1,9 +1,9 @@ -Code Coverage Report: +Code Coverage Report: %s - - Summary: + + Summary: Classes: 0.00% (0/1) Methods: 75.00% (3/4) Lines: 62.50% (5/8) diff --git a/tests/_files/BankAccount-text-path.txt b/tests/_files/BankAccount-text-path.txt index 488e34ff5..b18ac71cf 100644 --- a/tests/_files/BankAccount-text-path.txt +++ b/tests/_files/BankAccount-text-path.txt @@ -1,9 +1,9 @@ -Code Coverage Report: +Code Coverage Report: %s - - Summary: + + Summary: Classes: 0.00% (0/1) Methods: 75.00% (3/4) Paths: 60.00% (3/5) diff --git a/tests/_files/BankAccount-text-summary.txt b/tests/_files/BankAccount-text-summary.txt index 952abd17b..cf8e52385 100644 --- a/tests/_files/BankAccount-text-summary.txt +++ b/tests/_files/BankAccount-text-summary.txt @@ -1,7 +1,7 @@ Code Coverage Report Summary: - Classes: 0.00% (0/1) - Methods: 75.00% (3/4) - Lines: 62.50% (5/8) + Classes: 0.00% (0/1) + Methods: 75.00% (3/4) + Lines: 62.50% (5/8) diff --git a/tests/_files/BankAccountWithUncovered-text-line.txt b/tests/_files/BankAccountWithUncovered-text-line.txt index 85693d8af..ee7757bad 100644 --- a/tests/_files/BankAccountWithUncovered-text-line.txt +++ b/tests/_files/BankAccountWithUncovered-text-line.txt @@ -1,11 +1,11 @@ -Code Coverage Report: +Code Coverage Report: %s - - Summary: - Classes: 0.00% (0/2) - Methods: 37.50% (3/8) + + Summary: + Classes: 0.00% (0/2) + Methods: 37.50% (3/8) Lines: 31.25% (5/16) BankAccount diff --git a/tests/_files/BankAccountWithoutUncovered-text-line.txt b/tests/_files/BankAccountWithoutUncovered-text-line.txt index 4f188c76c..cbc92cdab 100644 --- a/tests/_files/BankAccountWithoutUncovered-text-line.txt +++ b/tests/_files/BankAccountWithoutUncovered-text-line.txt @@ -1,9 +1,9 @@ -Code Coverage Report: +Code Coverage Report: %s - - Summary: + + Summary: Classes: 0.00% (0/1) Methods: 75.00% (3/4) Lines: 62.50% (5/8) diff --git a/tests/_files/NamespacedBankAccount-text.txt b/tests/_files/NamespacedBankAccount-text.txt index 7cede93cb..f535d120b 100644 --- a/tests/_files/NamespacedBankAccount-text.txt +++ b/tests/_files/NamespacedBankAccount-text.txt @@ -1,9 +1,9 @@ -Code Coverage Report: +Code Coverage Report: %s - - Summary: + + Summary: Classes: 0.00% (0/1) Methods: 75.00% (3/4) Lines: 62.50% (5/8) diff --git a/tests/_files/class-with-anonymous-function-text.txt b/tests/_files/class-with-anonymous-function-text.txt index 6d7e2a07e..905bdd1a2 100644 --- a/tests/_files/class-with-anonymous-function-text.txt +++ b/tests/_files/class-with-anonymous-function-text.txt @@ -1,9 +1,9 @@ -Code Coverage Report: +Code Coverage Report: %s - - Summary: + + Summary: Classes: 100.00% (1/1) Methods: 100.00% (1/1) Lines: 100.00% (8/8) diff --git a/tests/_files/ignored-lines-text.txt b/tests/_files/ignored-lines-text.txt index 55f57afbc..8592fb471 100644 --- a/tests/_files/ignored-lines-text.txt +++ b/tests/_files/ignored-lines-text.txt @@ -1,10 +1,10 @@ -Code Coverage Report:%w +Code Coverage Report: %s -%w - Summary:%w - Classes: (0/0) - Methods: (0/0) + + Summary: + Classes: (0/0) + Methods: (0/0) Lines: 100.00% (1/1) From aa7e0c8aef912e8a073422e80787afd089e460f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=B6ller?= Date: Sat, 9 Mar 2024 16:02:32 +0100 Subject: [PATCH 3/4] Fix: Pad lines in code coverage report only when colors are shown --- src/Report/Text.php | 37 ++++++++++++------- ...NamespacedBankAccount-text-with-colors.txt | 14 +++++++ tests/tests/Report/TextTest.php | 10 +++++ 3 files changed, 47 insertions(+), 14 deletions(-) create mode 100644 tests/_files/NamespacedBankAccount-text-with-colors.txt diff --git a/src/Report/Text.php b/src/Report/Text.php index 56b237465..a307aa4b0 100644 --- a/src/Report/Text.php +++ b/src/Report/Text.php @@ -10,9 +10,13 @@ namespace SebastianBergmann\CodeCoverage\Report; use const PHP_EOL; +use function array_map; use function date; use function ksort; +use function max; use function sprintf; +use function str_pad; +use function strlen; use SebastianBergmann\CodeCoverage\CodeCoverage; use SebastianBergmann\CodeCoverage\Node\File; use SebastianBergmann\CodeCoverage\Util\Percentage; @@ -156,28 +160,31 @@ public function process(CodeCoverage $coverage, bool $showColors = false): strin $report->numberOfExecutableLines(), ); + $padding = max(array_map('strlen', [$classes, $methods, $lines])); + if ($this->showOnlySummary) { - $title = 'Code Coverage Report Summary:'; + $title = 'Code Coverage Report Summary:'; + $padding = max($padding, strlen($title)); - $output .= $this->format($colors['header'], $title); + $output .= $this->format($colors['header'], $padding, $title); } else { $date = date(' Y-m-d H:i:s'); $title = 'Code Coverage Report:'; - $output .= $this->format($colors['header'], $title); - $output .= $this->format($colors['header'], $date); - $output .= $this->format($colors['header'], ''); - $output .= $this->format($colors['header'], ' Summary:'); + $output .= $this->format($colors['header'], $padding, $title); + $output .= $this->format($colors['header'], $padding, $date); + $output .= $this->format($colors['header'], $padding, ''); + $output .= $this->format($colors['header'], $padding, ' Summary:'); } - $output .= $this->format($colors['classes'], $classes); - $output .= $this->format($colors['methods'], $methods); + $output .= $this->format($colors['classes'], $padding, $classes); + $output .= $this->format($colors['methods'], $padding, $methods); if ($hasBranchCoverage) { - $output .= $this->format($colors['paths'], $paths); - $output .= $this->format($colors['branches'], $branches); + $output .= $this->format($colors['paths'], $padding, $paths); + $output .= $this->format($colors['branches'], $padding, $branches); } - $output .= $this->format($colors['lines'], $lines); + $output .= $this->format($colors['lines'], $padding, $lines); if ($this->showOnlySummary) { return $output . PHP_EOL; @@ -297,10 +304,12 @@ private function printCoverageCounts(int $numberOfCoveredElements, int $totalNum sprintf($format, $totalNumberOfElements) . ')'; } - private function format(string $color, false|string $string): string + private function format(string $color, int $padding, false|string $string): string { - $reset = $color ? self::COLOR_RESET : ''; + if ($color === '') { + return (string) $string . PHP_EOL; + } - return $color . (string) $string . $reset . PHP_EOL; + return $color . str_pad((string) $string, $padding) . self::COLOR_RESET . PHP_EOL; } } diff --git a/tests/_files/NamespacedBankAccount-text-with-colors.txt b/tests/_files/NamespacedBankAccount-text-with-colors.txt new file mode 100644 index 000000000..83f3901e1 --- /dev/null +++ b/tests/_files/NamespacedBankAccount-text-with-colors.txt @@ -0,0 +1,14 @@ + + +Code Coverage Report:  + %s  +  + Summary:  + Classes: 0.00% (0/1) + Methods: 75.00% (3/4) + Lines: 62.50% (5/8) + +SomeNamespace\BankAccount + Methods: ( 0/ 0) Lines: ( 0/ 0) +SomeNamespace\BankAccountTrait + Methods: 75.00% ( 3/ 4) Lines: 62.50% ( 5/ 8) diff --git a/tests/tests/Report/TextTest.php b/tests/tests/Report/TextTest.php index 16a99d369..8cefd57c9 100644 --- a/tests/tests/Report/TextTest.php +++ b/tests/tests/Report/TextTest.php @@ -57,6 +57,16 @@ public function testTextForNamespacedBankAccountTest(): void ); } + public function testTextForNamespacedBankAccountTestWhenColorsAreEnabled(): void + { + $text = new Text(Thresholds::default(), true, false); + + $this->assertStringMatchesFormatFile( + TEST_FILES_PATH . 'NamespacedBankAccount-text-with-colors.txt', + str_replace(PHP_EOL, "\n", $text->process($this->getLineCoverageForNamespacedBankAccount(), true)), + ); + } + public function testTextForFileWithIgnoredLines(): void { $text = new Text(Thresholds::default(), false, false); From d51c3aec14896d5e80b354fad58e998d1980f8f8 Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Sat, 9 Mar 2024 17:54:15 +0100 Subject: [PATCH 4/4] Prepare release --- ChangeLog-10.1.md | 7 +++++++ src/Version.php | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/ChangeLog-10.1.md b/ChangeLog-10.1.md index 603efad46..8aa1b5f32 100644 --- a/ChangeLog-10.1.md +++ b/ChangeLog-10.1.md @@ -2,6 +2,12 @@ All notable changes are documented in this file using the [Keep a CHANGELOG](http://keepachangelog.com/) principles. +## [10.1.13] - 2024-03-09 + +### Changed + +* [#1032](https://github.com/sebastianbergmann/php-code-coverage/pull/1032): Pad lines in code coverage report only when colors are shown + ## [10.1.12] - 2024-03-02 ### Changed @@ -89,6 +95,7 @@ All notable changes are documented in this file using the [Keep a CHANGELOG](htt * The `SebastianBergmann\CodeCoverage\Filter::includeDirectory()`, `SebastianBergmann\CodeCoverage\Filter::excludeDirectory()`, and `SebastianBergmann\CodeCoverage\Filter::excludeFile()` methods are now deprecated +[10.1.13]: https://github.com/sebastianbergmann/php-code-coverage/compare/10.1.12...10.1.13 [10.1.12]: https://github.com/sebastianbergmann/php-code-coverage/compare/10.1.11...10.1.12 [10.1.11]: https://github.com/sebastianbergmann/php-code-coverage/compare/10.1.10...10.1.11 [10.1.10]: https://github.com/sebastianbergmann/php-code-coverage/compare/10.1.9...10.1.10 diff --git a/src/Version.php b/src/Version.php index 41f657710..9c77357ce 100644 --- a/src/Version.php +++ b/src/Version.php @@ -19,7 +19,7 @@ final class Version public static function id(): string { if (self::$version === '') { - self::$version = (new VersionId('10.1.12', dirname(__DIR__)))->asString(); + self::$version = (new VersionId('10.1.13', dirname(__DIR__)))->asString(); } return self::$version;