Skip to content

Commit

Permalink
chore: reworks code
Browse files Browse the repository at this point in the history
  • Loading branch information
nunomaduro committed Dec 26, 2023
1 parent 74a05e9 commit dabda51
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 147 deletions.
17 changes: 6 additions & 11 deletions src/Contracts/Logger.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
<?php

declare( strict_types=1 );
declare(strict_types=1);

namespace Pest\TypeCoverage\Contracts;

/**
* @internal
*
* @final
*/
interface Logger
{
/**
* @param string $outputPath
* @param array<string, string|float|int|null> $pluginSettings
*/
public function __construct( string $outputPath, array $pluginSettings );

/**
* @param array<int, string> $uncoveredLines
* @param array<int, string> $uncoveredLinesIgnored
*/
public function append(string $path, array $uncoveredLines, array $uncoveredLinesIgnored, float $percentage):void;
public function append(string $path, array $uncoveredLines, array $uncoveredLinesIgnored, float $percentage): void;

public function output():void;
/**
* Outputs the coverage report.
*/
public function output(): void;
}
48 changes: 26 additions & 22 deletions src/Logging/JsonLogger.php
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
<?php

declare( strict_types=1 );
declare(strict_types=1);

namespace Pest\TypeCoverage\Logging;

use Pest\TypeCoverage\Contracts\Logger;

/**
* @internal
*
* @final
*/
class JsonLogger implements Logger
final class JsonLogger implements Logger
{
private string $outputPath;

/** @var array<string, string|float|int|null> */
private array $pluginSettings;

/** @var array<int, array<string, string|float|int|array<int,string>>> */
private array $logs = [];

public function __construct( string $outputPath, array $pluginSettings ) {
$this->outputPath = $outputPath;
$this->pluginSettings = $pluginSettings;
/**
* Creates a new Logger instance.
*
* @param array<int, array<string, mixed>> $logs
*/
public function __construct(// @phpstan-ignore-line
private readonly string $outputPath,
private readonly float $coverageMin,
private array $logs = [],
) {
//
}

public function append(string $path, array $uncoveredLines, array $uncoveredLinesIgnored, float $percentage):void
/**
* {@inheritDoc}
*/
public function append(string $path, array $uncoveredLines, array $uncoveredLinesIgnored, float $percentage): void
{
$this->logs[] = [
'file' => $path,
Expand All @@ -36,14 +37,17 @@ public function append(string $path, array $uncoveredLines, array $uncoveredLine
];
}

/**
* {@inheritDoc}
*/
public function output(): void
{
$json = json_encode( [
'format' => 'pest',
'settings' => $this->pluginSettings,
'result' => $this->logs,
'total' => round( array_sum(array_column($this->logs, 'percentage')) / count($this->logs), 2 )
], JSON_THROW_ON_ERROR );
$json = json_encode([
'format' => 'pest',
'coverage-min' => $this->coverageMin,
'result' => $this->logs,
'total' => round(array_sum(array_column($this->logs, 'percentage')) / count($this->logs), 2),
], JSON_THROW_ON_ERROR);
file_put_contents($this->outputPath, $json);
}
}
19 changes: 7 additions & 12 deletions src/Logging/NullLogger.php
Original file line number Diff line number Diff line change
@@ -1,32 +1,27 @@
<?php

declare( strict_types=1 );
declare(strict_types=1);

namespace Pest\TypeCoverage\Logging;

use Pest\TypeCoverage\Contracts\Logger;

/**
* @internal
*
* @final
*/
class NullLogger implements Logger
final class NullLogger implements Logger
{
/**
* @param string $outputPath
* @param array<string, string|float|int|null> $pluginSettings
* {@inheritDoc}
*/
public function __construct( string $outputPath, array $pluginSettings )
{
//
}

public function append(string $path, array $uncoveredLines, array $uncoveredLinesIgnored, float $percentage):void
public function append(string $path, array $uncoveredLines, array $uncoveredLinesIgnored, float $percentage): void
{
//
}

/**
* {@inheritDoc}
*/
public function output(): void
{
//
Expand Down
8 changes: 4 additions & 4 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
use Pest\Plugins\Concerns\HandleArguments;
use Pest\Support\View;
use Pest\TestSuite;
use Pest\TypeCoverage\Logging\JsonLogger;
use Pest\TypeCoverage\Contracts\Logger;
use Pest\TypeCoverage\Logging\JsonLogger;
use Pest\TypeCoverage\Logging\NullLogger;
use Pest\TypeCoverage\Support\ConfigurationSourceDetector;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -44,7 +44,7 @@ class Plugin implements HandlesArguments
public function __construct(
private readonly OutputInterface $output
) {
$this->coverageLogger = new NullLogger('', []);
$this->coverageLogger = new NullLogger();
}

/**
Expand All @@ -64,7 +64,7 @@ public function handleArguments(array $arguments): array

if (str_starts_with($argument, '--type-coverage-json')) {
// grab the value of the --type-coverage-json argument
$this->coverageLogger = new JsonLogger(explode('=', $argument)[1], ['coverageMin' => $this->coverageMin]);
$this->coverageLogger = new JsonLogger(explode('=', $argument)[1], $this->coverageMin);
}
}

Expand Down Expand Up @@ -109,7 +109,7 @@ function (Result $result) use (&$totals): void {

$color = $uncoveredLines === [] ? 'green' : 'yellow';

$this->coverageLogger->append( $path, $uncoveredLines, $uncoveredLinesIgnored, $result->totalCoverage );
$this->coverageLogger->append($path, $uncoveredLines, $uncoveredLinesIgnored, $result->totalCoverage);

$uncoveredLines = implode(', ', $uncoveredLines);
$uncoveredLinesIgnored = implode(', ', $uncoveredLinesIgnored);
Expand Down
195 changes: 97 additions & 98 deletions tests/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,111 +23,110 @@ public function exit(int $code): never
);
});

test( 'it can output to json', function () {
test('it can output to json', function () {
$output = new BufferedOutput();
$plugin = new class( $output ) extends Plugin {
public function exit( int $code ): never
$plugin = new class($output) extends Plugin
{
public function exit(int $code): never
{
throw new Exception( $code );
throw new Exception($code);
}
};

expect( fn() => $plugin->handleArguments( [ '--type-coverage', '--type-coverage-json=test.json' ] ) )->toThrow( Exception::class, 0 );
expect(fn () => $plugin->handleArguments(['--type-coverage', '--type-coverage-json=test.json']))->toThrow(Exception::class, 0);

expect( __DIR__ . '/../test.json' )->toBeReadableFile();
expect( file_get_contents( __DIR__ . '/../test.json' ) )->json()->toMatchArray( [
"format" => "pest",
'settings' => [
'coverageMin' => 0,
expect(__DIR__.'/../test.json')->toBeReadableFile();
expect(file_get_contents(__DIR__.'/../test.json'))->json()->toMatchArray([
'format' => 'pest',
'coverage-min' => 0,
'result' => [
[
'file' => 'src/PHPStanAnalyser.php',
'uncoveredLines' => [],
'uncoveredLinesIgnored' => [],
'percentage' => 100,
],
[
'file' => 'src/TestCaseForTypeCoverage.php',
'uncoveredLines' => [],
'uncoveredLinesIgnored' => [],
'percentage' => 100,
],
[
'file' => 'src/Contracts/Logger.php',
'uncoveredLines' => [],
'uncoveredLinesIgnored' => [],
'percentage' => 100,
],
[
'file' => 'src/Plugin.php',
'uncoveredLines' => [],
'uncoveredLinesIgnored' => [],
'percentage' => 100,
],
[
'file' => 'src/Result.php',
'uncoveredLines' => [],
'uncoveredLinesIgnored' => [],
'percentage' => 100,
],
[
'file' => 'src/Error.php',
'uncoveredLines' => [],
'uncoveredLinesIgnored' => [],
'percentage' => 100,
],
"result" => [
[
"file" => "src/PHPStanAnalyser.php",
"uncoveredLines" => [],
"uncoveredLinesIgnored" => [],
"percentage" => 100,
],
[
"file" => "src/TestCaseForTypeCoverage.php",
"uncoveredLines" => [],
"uncoveredLinesIgnored" => [],
"percentage" => 100,
],
[
"file" => "src/Contracts/Logger.php",
"uncoveredLines" => [],
"uncoveredLinesIgnored" => [],
"percentage" => 100,
],
[
"file" => "src/Plugin.php",
"uncoveredLines" => [],
"uncoveredLinesIgnored" => [],
"percentage" => 100,
],
[
"file" => "src/Result.php",
"uncoveredLines" => [],
"uncoveredLinesIgnored" => [],
"percentage" => 100,
],
[
"file" => "src/Error.php",
"uncoveredLines" => [],
"uncoveredLinesIgnored" => [],
"percentage" => 100,
],
[
"file" => "src/Support/ConfigurationSourceDetector.php",
"uncoveredLines" => [],
"uncoveredLinesIgnored" => [],
"percentage" => 100,
],
[
"file" => "src/Analyser.php",
"uncoveredLines" => [],
"uncoveredLinesIgnored" => [],
"percentage" => 100,
],
[
"file" => "src/Logging/NullLogger.php",
"uncoveredLines" => [],
"uncoveredLinesIgnored" => [],
"percentage" => 100,
],
[
"file" => "src/Logging/JsonLogger.php",
"uncoveredLines" => [],
"uncoveredLinesIgnored" => [],
"percentage" => 100,
],
[
"file" => "tests/Fixtures/Properties.php",
"uncoveredLines" => ["pr12"],
"uncoveredLinesIgnored" => [],
"percentage" => 83,
],
[
"file" => "tests/Fixtures/All.php",
"uncoveredLines" => ["pr12", "pa14", "pa14", "rt14"],
"uncoveredLinesIgnored" => [],
"percentage" => 0,
],
[
"file" => "tests/Fixtures/ReturnType.php",
"uncoveredLines" => ["rt12"],
"uncoveredLinesIgnored" => [],
"percentage" => 67,
],
[
"file" => "tests/Fixtures/Parameters.php",
"uncoveredLines" => ["pa12"],
"uncoveredLinesIgnored" => [],
"percentage" => 83,
],
[
'file' => 'src/Support/ConfigurationSourceDetector.php',
'uncoveredLines' => [],
'uncoveredLinesIgnored' => [],
'percentage' => 100,
],
'total' => 88.07,
[
'file' => 'src/Analyser.php',
'uncoveredLines' => [],
'uncoveredLinesIgnored' => [],
'percentage' => 100,
],
[
'file' => 'src/Logging/NullLogger.php',
'uncoveredLines' => [],
'uncoveredLinesIgnored' => [],
'percentage' => 100,
],
[
'file' => 'src/Logging/JsonLogger.php',
'uncoveredLines' => [],
'uncoveredLinesIgnored' => [],
'percentage' => 100,
],
[
'file' => 'tests/Fixtures/Properties.php',
'uncoveredLines' => ['pr12'],
'uncoveredLinesIgnored' => [],
'percentage' => 83,
],
[
'file' => 'tests/Fixtures/All.php',
'uncoveredLines' => ['pr12', 'pa14', 'pa14', 'rt14'],
'uncoveredLinesIgnored' => [],
'percentage' => 0,
],
[
'file' => 'tests/Fixtures/ReturnType.php',
'uncoveredLines' => ['rt12'],
'uncoveredLinesIgnored' => [],
'percentage' => 67,
],
[
'file' => 'tests/Fixtures/Parameters.php',
'uncoveredLines' => ['pa12'],
'uncoveredLinesIgnored' => [],
'percentage' => 83,
],
],
'total' => 88.07,
]);

unlink( __DIR__ . '/../test.json' );
} );
unlink(__DIR__.'/../test.json');
});

0 comments on commit dabda51

Please sign in to comment.