From 8bcb89cbd41d4e9249a82027a111234a33a9cf00 Mon Sep 17 00:00:00 2001 From: Priyadi Iman Nurcahyo <1102197+priyadi@users.noreply.github.com> Date: Thu, 22 Feb 2024 19:31:12 +0700 Subject: [PATCH] chore: Add `readonly` or implement `ResetInterface` to applicable classes. --- src/Debug/MapperDataCollector.php | 13 ++++++++++++- src/Debug/TraceableMappingFactory.php | 8 +++++++- src/Debug/TraceableTransformer.php | 13 ++++--------- .../Implementation/MainTransformer.php | 8 +++++++- .../Implementation/ObjectCacheFactory.php | 2 +- src/Proxy/ProxyNamer.php | 6 +++++- src/Serializer/DenormalizerContext.php | 2 +- src/Serializer/NormalizerContext.php | 2 +- src/Transformer/Util/ReaderWriter.php | 2 +- src/Util/TypeCheck.php | 2 +- src/Util/TypeGuesser.php | 2 +- src/Util/TypeUtil.php | 2 +- 12 files changed, 42 insertions(+), 20 deletions(-) diff --git a/src/Debug/MapperDataCollector.php b/src/Debug/MapperDataCollector.php index d392b41f..6697bd63 100644 --- a/src/Debug/MapperDataCollector.php +++ b/src/Debug/MapperDataCollector.php @@ -18,11 +18,12 @@ use Symfony\Bundle\FrameworkBundle\DataCollector\AbstractDataCollector; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Symfony\Contracts\Service\ResetInterface; /** * @internal */ -final class MapperDataCollector extends AbstractDataCollector +final class MapperDataCollector extends AbstractDataCollector implements ResetInterface { public function getName(): string { @@ -135,4 +136,14 @@ public function getTotalTime(): float $this->getMappings() )); } + + public function reset(): void + { + $this->data = []; + $this->totalMappings = null; + $this->totalMappingsIncludingSubMappings = null; + $this->totalTime = null; + + parent::reset(); + } } diff --git a/src/Debug/TraceableMappingFactory.php b/src/Debug/TraceableMappingFactory.php index 6ac85765..784f401c 100644 --- a/src/Debug/TraceableMappingFactory.php +++ b/src/Debug/TraceableMappingFactory.php @@ -15,11 +15,12 @@ use Rekalogika\Mapper\Mapping\Mapping; use Rekalogika\Mapper\Mapping\MappingFactoryInterface; +use Symfony\Contracts\Service\ResetInterface; /** * @internal */ -final class TraceableMappingFactory implements MappingFactoryInterface +final class TraceableMappingFactory implements MappingFactoryInterface, ResetInterface { private bool $mappingCollected = false; @@ -29,6 +30,11 @@ public function __construct( ) { } + public function reset(): void + { + $this->mappingCollected = false; + } + public function getMapping(): Mapping { if ($this->mappingCollected) { diff --git a/src/Debug/TraceableTransformer.php b/src/Debug/TraceableTransformer.php index 5e5d1eda..5ca70099 100644 --- a/src/Debug/TraceableTransformer.php +++ b/src/Debug/TraceableTransformer.php @@ -33,8 +33,6 @@ final class TraceableTransformer extends AbstractTransformerDecorator implements { use MainTransformerAwareTrait; - private bool $withMainTransformerCalled = false; - public function __construct( private TransformerInterface $decorated, private MapperDataCollector $dataCollector @@ -48,17 +46,14 @@ public function getDecorated(): TransformerInterface public function withMainTransformer(MainTransformerInterface $mainTransformer): static { - if ($this->withMainTransformerCalled) { + if (!$this->decorated instanceof MainTransformerAwareInterface) { return $this; } - $this->withMainTransformerCalled = true; - - if ($this->decorated instanceof MainTransformerAwareInterface) { - $this->decorated = $this->decorated->withMainTransformer($mainTransformer); - } + $clone = clone $this; + $clone->decorated = $this->decorated->withMainTransformer($mainTransformer); - return $this; + return $clone; } public function transform( diff --git a/src/MainTransformer/Implementation/MainTransformer.php b/src/MainTransformer/Implementation/MainTransformer.php index 8244a5a3..7d90748f 100644 --- a/src/MainTransformer/Implementation/MainTransformer.php +++ b/src/MainTransformer/Implementation/MainTransformer.php @@ -34,11 +34,12 @@ use Rekalogika\Mapper\Util\TypeCheck; use Rekalogika\Mapper\Util\TypeGuesser; use Symfony\Component\PropertyInfo\Type; +use Symfony\Contracts\Service\ResetInterface; /** * @internal */ -final class MainTransformer implements MainTransformerInterface +final class MainTransformer implements MainTransformerInterface, ResetInterface { public static int $manualGcInterval = 500; private static int $runCounter = 1; @@ -51,6 +52,11 @@ public function __construct( ) { } + public function reset(): void + { + self::$runCounter = 1; + } + private function processTransformer( TransformerInterface $transformer ): TransformerInterface { diff --git a/src/ObjectCache/Implementation/ObjectCacheFactory.php b/src/ObjectCache/Implementation/ObjectCacheFactory.php index 600167b5..794f47c6 100644 --- a/src/ObjectCache/Implementation/ObjectCacheFactory.php +++ b/src/ObjectCache/Implementation/ObjectCacheFactory.php @@ -17,7 +17,7 @@ use Rekalogika\Mapper\ObjectCache\ObjectCacheFactoryInterface; use Rekalogika\Mapper\TypeResolver\TypeResolverInterface; -final class ObjectCacheFactory implements ObjectCacheFactoryInterface +final readonly class ObjectCacheFactory implements ObjectCacheFactoryInterface { public function __construct( private TypeResolverInterface $typeResolver diff --git a/src/Proxy/ProxyNamer.php b/src/Proxy/ProxyNamer.php index dd2f06aa..aa42c8da 100644 --- a/src/Proxy/ProxyNamer.php +++ b/src/Proxy/ProxyNamer.php @@ -16,8 +16,12 @@ /** * @internal */ -class ProxyNamer +final readonly class ProxyNamer { + public function __construct() + { + } + /** * @param class-string $class */ diff --git a/src/Serializer/DenormalizerContext.php b/src/Serializer/DenormalizerContext.php index e6b674c7..45c3e5c6 100644 --- a/src/Serializer/DenormalizerContext.php +++ b/src/Serializer/DenormalizerContext.php @@ -17,6 +17,6 @@ * @deprecated * @phpstan-ignore-next-line */ -class DenormalizerContext extends AbstractSerializerContext +final class DenormalizerContext extends AbstractSerializerContext { } diff --git a/src/Serializer/NormalizerContext.php b/src/Serializer/NormalizerContext.php index 76745fe5..303451bd 100644 --- a/src/Serializer/NormalizerContext.php +++ b/src/Serializer/NormalizerContext.php @@ -17,6 +17,6 @@ * @deprecated * @phpstan-ignore-next-line */ -class NormalizerContext extends AbstractSerializerContext +final class NormalizerContext extends AbstractSerializerContext { } diff --git a/src/Transformer/Util/ReaderWriter.php b/src/Transformer/Util/ReaderWriter.php index faaa429d..5e1422f0 100644 --- a/src/Transformer/Util/ReaderWriter.php +++ b/src/Transformer/Util/ReaderWriter.php @@ -26,7 +26,7 @@ /** * @internal */ -class ReaderWriter +final readonly class ReaderWriter { /** * @throws UninitializedSourcePropertyException diff --git a/src/Util/TypeCheck.php b/src/Util/TypeCheck.php index e8b2a392..60a648b3 100644 --- a/src/Util/TypeCheck.php +++ b/src/Util/TypeCheck.php @@ -17,7 +17,7 @@ use Rekalogika\Mapper\Transformer\MixedType; use Symfony\Component\PropertyInfo\Type; -final class TypeCheck +final readonly class TypeCheck { private function __construct() { diff --git a/src/Util/TypeGuesser.php b/src/Util/TypeGuesser.php index b7422e08..9e7428b2 100644 --- a/src/Util/TypeGuesser.php +++ b/src/Util/TypeGuesser.php @@ -16,7 +16,7 @@ use Rekalogika\Mapper\Exception\InvalidArgumentException; use Symfony\Component\PropertyInfo\Type; -final class TypeGuesser +final readonly class TypeGuesser { private function __construct() { diff --git a/src/Util/TypeUtil.php b/src/Util/TypeUtil.php index 58e45fcb..5a009780 100644 --- a/src/Util/TypeUtil.php +++ b/src/Util/TypeUtil.php @@ -24,7 +24,7 @@ use Rekalogika\Mapper\TypeResolver\Implementation\TypeResolver; use Symfony\Component\PropertyInfo\Type; -final class TypeUtil +final readonly class TypeUtil { private function __construct() {