Skip to content

Commit

Permalink
Initial work on chaining Exporter implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Mar 28, 2024
1 parent 6a7c780 commit aeb0007
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Util/Exporter/DefaultExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public function __construct()
$this->exporter = new ExporterImplementation;
}

public function handles(mixed $value): true

Check warning on line 26 in src/Util/Exporter/DefaultExporter.php

View check run for this annotation

Codecov / codecov/patch

src/Util/Exporter/DefaultExporter.php#L26

Added line #L26 was not covered by tests
{
return true;

Check warning on line 28 in src/Util/Exporter/DefaultExporter.php

View check run for this annotation

Codecov / codecov/patch

src/Util/Exporter/DefaultExporter.php#L28

Added line #L28 was not covered by tests
}

public function export(mixed $value): string
{
return $this->exporter->export($value);
Expand Down
2 changes: 2 additions & 0 deletions src/Util/Exporter/Exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@
*/
interface Exporter
{
public function handles(mixed $value): bool;

public function export(mixed $value): string;
}
60 changes: 60 additions & 0 deletions src/Util/Exporter/ExporterChain.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\Util;

use function assert;

/**
* @internal This class is not covered by the backward compatibility promise for PHPUnit
*/
final readonly class ExporterChain implements Exporter
{
/**
* @psalm-var non-empty-list<Exporter>
*/
private array $exporter;

/**
* @psalm-param non-empty-list<Exporter> $exporter
*/
public static function buildWith(array $exporter): self

Check warning on line 27 in src/Util/Exporter/ExporterChain.php

View check run for this annotation

Codecov / codecov/patch

src/Util/Exporter/ExporterChain.php#L27

Added line #L27 was not covered by tests
{
$exporter[] = new DefaultExporter;

Check warning on line 29 in src/Util/Exporter/ExporterChain.php

View check run for this annotation

Codecov / codecov/patch

src/Util/Exporter/ExporterChain.php#L29

Added line #L29 was not covered by tests

return new self($exporter);

Check warning on line 31 in src/Util/Exporter/ExporterChain.php

View check run for this annotation

Codecov / codecov/patch

src/Util/Exporter/ExporterChain.php#L31

Added line #L31 was not covered by tests
}

/**
* @psalm-param non-empty-list<Exporter> $exporter
*/
private function __construct(array $exporter)

Check warning on line 37 in src/Util/Exporter/ExporterChain.php

View check run for this annotation

Codecov / codecov/patch

src/Util/Exporter/ExporterChain.php#L37

Added line #L37 was not covered by tests
{
$this->exporter = $exporter;

Check warning on line 39 in src/Util/Exporter/ExporterChain.php

View check run for this annotation

Codecov / codecov/patch

src/Util/Exporter/ExporterChain.php#L39

Added line #L39 was not covered by tests
}

public function handles(mixed $value): true

Check warning on line 42 in src/Util/Exporter/ExporterChain.php

View check run for this annotation

Codecov / codecov/patch

src/Util/Exporter/ExporterChain.php#L42

Added line #L42 was not covered by tests
{
return true;

Check warning on line 44 in src/Util/Exporter/ExporterChain.php

View check run for this annotation

Codecov / codecov/patch

src/Util/Exporter/ExporterChain.php#L44

Added line #L44 was not covered by tests
}

public function export(mixed $value): string

Check warning on line 47 in src/Util/Exporter/ExporterChain.php

View check run for this annotation

Codecov / codecov/patch

src/Util/Exporter/ExporterChain.php#L47

Added line #L47 was not covered by tests
{
foreach ($this->exporter as $exporter) {
if (!$exporter->handles($value)) {

Check warning on line 50 in src/Util/Exporter/ExporterChain.php

View check run for this annotation

Codecov / codecov/patch

src/Util/Exporter/ExporterChain.php#L49-L50

Added lines #L49 - L50 were not covered by tests
/** @noinspection PhpUnnecessaryStopStatementInspection */
continue;

Check warning on line 52 in src/Util/Exporter/ExporterChain.php

View check run for this annotation

Codecov / codecov/patch

src/Util/Exporter/ExporterChain.php#L52

Added line #L52 was not covered by tests
}
}

assert(isset($exporter));

Check warning on line 56 in src/Util/Exporter/ExporterChain.php

View check run for this annotation

Codecov / codecov/patch

src/Util/Exporter/ExporterChain.php#L56

Added line #L56 was not covered by tests

return $exporter->export($value);

Check warning on line 58 in src/Util/Exporter/ExporterChain.php

View check run for this annotation

Codecov / codecov/patch

src/Util/Exporter/ExporterChain.php#L58

Added line #L58 was not covered by tests
}
}

0 comments on commit aeb0007

Please sign in to comment.