From d0d99db563f5e08e40634079ed9a038c93aa6ad5 Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Mon, 17 Jun 2024 09:39:18 +0200 Subject: [PATCH] Revert #56 --- ChangeLog.md | 7 -- src/Exporter.php | 22 +------ src/ObjectExporter.php | 17 ----- src/ObjectExporterChain.php | 51 --------------- src/exception/Exception.php | 16 ----- src/exception/ObjectNotSupportedException.php | 16 ----- tests/ExporterTest.php | 21 ------ tests/ObjectExporterChainTest.php | 64 ------------------- 8 files changed, 2 insertions(+), 212 deletions(-) delete mode 100644 src/ObjectExporter.php delete mode 100644 src/ObjectExporterChain.php delete mode 100644 src/exception/Exception.php delete mode 100644 src/exception/ObjectNotSupportedException.php delete mode 100644 tests/ObjectExporterChainTest.php diff --git a/ChangeLog.md b/ChangeLog.md index 97f1d23..c55c3d9 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -2,12 +2,6 @@ All notable changes are documented in this file using the [Keep a CHANGELOG](https://keepachangelog.com/) principles. -## [6.1.0] - 2024-MM-DD - -### Added - -* [#56](https://github.com/sebastianbergmann/exporter/pull/56): The export of objects can now be customized using `ObjectExporter` objects - ## [6.0.1] - 2024-03-02 ### Changed @@ -118,7 +112,6 @@ All notable changes are documented in this file using the [Keep a CHANGELOG](htt * Remove HHVM-specific code that is no longer needed -[6.1.0]: https://github.com/sebastianbergmann/exporter/compare/6.0.1...main [6.0.1]: https://github.com/sebastianbergmann/exporter/compare/6.0.0...6.0.1 [6.0.0]: https://github.com/sebastianbergmann/exporter/compare/5.1...6.0.0 [5.1.2]: https://github.com/sebastianbergmann/exporter/compare/5.1.1...5.1.2 diff --git a/src/Exporter.php b/src/Exporter.php index 9fea46b..cf7efc1 100644 --- a/src/Exporter.php +++ b/src/Exporter.php @@ -38,13 +38,6 @@ final readonly class Exporter { - private ?ObjectExporter $objectExporter; - - public function __construct(?ObjectExporter $objectExporter = null) - { - $this->objectExporter = $objectExporter; - } - /** * Exports a value as a string. * @@ -341,18 +334,7 @@ private function exportObject(object $value, RecursionContext $processed, int $i $processed->add($value); - if ($this->objectExporter !== null && $this->objectExporter->handles($value)) { - $buffer = $this->objectExporter->export($value, $this, $indentation); - } else { - $buffer = $this->defaultObjectExport($value, $processed, $indentation); - } - - return $class . ' Object #' . spl_object_id($value) . ' (' . $buffer . ')'; - } - - private function defaultObjectExport(object $object, RecursionContext $processed, int $indentation): string - { - $array = $this->toArray($object); + $array = $this->toArray($value); $buffer = ''; $whitespace = str_repeat(' ', 4 * $indentation); @@ -370,6 +352,6 @@ private function defaultObjectExport(object $object, RecursionContext $processed $buffer = "\n" . $buffer . $whitespace; } - return $buffer; + return $class . ' Object #' . spl_object_id($value) . ' (' . $buffer . ')'; } } diff --git a/src/ObjectExporter.php b/src/ObjectExporter.php deleted file mode 100644 index 535aa53..0000000 --- a/src/ObjectExporter.php +++ /dev/null @@ -1,17 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ -namespace SebastianBergmann\Exporter; - -interface ObjectExporter -{ - public function handles(object $object): bool; - - public function export(object $object, Exporter $exporter, int $indentation): string; -} diff --git a/src/ObjectExporterChain.php b/src/ObjectExporterChain.php deleted file mode 100644 index 2efd7f3..0000000 --- a/src/ObjectExporterChain.php +++ /dev/null @@ -1,51 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ -namespace SebastianBergmann\Exporter; - -final class ObjectExporterChain implements ObjectExporter -{ - /** - * @psalm-var non-empty-list - */ - private array $exporter; - - /** - * @psalm-param non-empty-list $exporter - */ - public function __construct(array $exporter) - { - $this->exporter = $exporter; - } - - public function handles(object $object): bool - { - foreach ($this->exporter as $exporter) { - if ($exporter->handles($object)) { - return true; - } - } - - return false; - } - - /** - * @throws ObjectNotSupportedException - */ - public function export(object $object, Exporter $exporter, int $indentation): string - { - foreach ($this->exporter as $objectExporter) { - if ($objectExporter->handles($object)) { - return $objectExporter->export($object, $exporter, $indentation); - } - } - - throw new ObjectNotSupportedException; - } -} diff --git a/src/exception/Exception.php b/src/exception/Exception.php deleted file mode 100644 index 279613b..0000000 --- a/src/exception/Exception.php +++ /dev/null @@ -1,16 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ -namespace SebastianBergmann\Exporter; - -use Throwable; - -interface Exception extends Throwable -{ -} diff --git a/src/exception/ObjectNotSupportedException.php b/src/exception/ObjectNotSupportedException.php deleted file mode 100644 index 4e0bf96..0000000 --- a/src/exception/ObjectNotSupportedException.php +++ /dev/null @@ -1,16 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ -namespace SebastianBergmann\Exporter; - -use RuntimeException; - -final class ObjectNotSupportedException extends RuntimeException implements Exception -{ -} diff --git a/tests/ExporterTest.php b/tests/ExporterTest.php index 7775884..b78e008 100644 --- a/tests/ExporterTest.php +++ b/tests/ExporterTest.php @@ -502,27 +502,6 @@ public function testShortenedRecursiveOccurredRecursion(): void $this->assertEquals('*RECURSION*', (new Exporter)->shortenedRecursiveExport($value, $context)); } - public function testExportOfObjectsCanBeCustomized(): void - { - $objectExporter = $this->createStub(ObjectExporter::class); - $objectExporter->method('handles')->willReturn(true); - $objectExporter->method('export')->willReturn('custom object export'); - - $exporter = new Exporter(new ObjectExporterChain([$objectExporter])); - - $this->assertStringMatchesFormat( - <<<'EOT' -Array &0 [ - 0 => stdClass Object #%d (custom object export), - 1 => stdClass Object #%d (custom object export), -] -EOT - , - $exporter->export([new stdClass, new stdClass]), - ); - - } - private function trimNewline(string $string): string { return preg_replace('/[ ]*\n/', "\n", $string); diff --git a/tests/ObjectExporterChainTest.php b/tests/ObjectExporterChainTest.php deleted file mode 100644 index bb901fc..0000000 --- a/tests/ObjectExporterChainTest.php +++ /dev/null @@ -1,64 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ -namespace SebastianBergmann\Exporter; - -use PHPUnit\Framework\Attributes\CoversClass; -use PHPUnit\Framework\Attributes\Small; -use PHPUnit\Framework\Attributes\UsesClass; -use PHPUnit\Framework\TestCase; -use stdClass; - -#[CoversClass(ObjectExporterChain::class)] -#[UsesClass(Exporter::class)] -#[Small] -final class ObjectExporterChainTest extends TestCase -{ - public function testCanBeQueriedWhetherChainedExporterHandlesAnObject(): void - { - $firstExporter = $this->createStub(ObjectExporter::class); - $firstExporter->method('handles')->willReturn(false); - - $secondExporter = $this->createStub(ObjectExporter::class); - $secondExporter->method('handles')->willReturn(true); - - $chain = new ObjectExporterChain([$firstExporter]); - $this->assertFalse($chain->handles(new stdClass)); - - $chain = new ObjectExporterChain([$firstExporter, $secondExporter]); - $this->assertTrue($chain->handles(new stdClass)); - } - - public function testDelegatesExportingToFirstExporterThatHandlesAnObject(): void - { - $firstExporter = $this->createStub(ObjectExporter::class); - $firstExporter->method('handles')->willReturn(false); - $firstExporter->method('export')->willThrowException(new ObjectNotSupportedException); - - $secondExporter = $this->createStub(ObjectExporter::class); - $secondExporter->method('handles')->willReturn(true); - $secondExporter->method('export')->willReturn('string'); - - $chain = new ObjectExporterChain([$firstExporter, $secondExporter]); - - $this->assertSame('string', $chain->export(new stdClass, new Exporter, 0)); - } - - public function testCannotExportObjectWhenNoExporterHandlesIt(): void - { - $firstExporter = $this->createStub(ObjectExporter::class); - $firstExporter->method('handles')->willReturn(false); - - $chain = new ObjectExporterChain([$firstExporter]); - - $this->expectException(ObjectNotSupportedException::class); - - $this->assertSame('string', $chain->export(new stdClass, new Exporter, 0)); - } -}