From 0d06f0e877164c41b24fd780461d4cd22cbfdb36 Mon Sep 17 00:00:00 2001 From: Priyadi Iman Nurcahyo <1102197+priyadi@users.noreply.github.com> Date: Fri, 19 Jul 2024 00:44:33 +0700 Subject: [PATCH] ReadOnlyClassRector --- rector.php | 4 +++- .../ObjectMapperTableFactory.php | 4 ++-- src/MapperFactory.php | 13 ++---------- .../ObjectToObjectTransformer.php | 4 +--- .../ScalarToScalarTransformer.php | 20 +++++++----------- src/Transformer/Util/ReaderWriter.php | 12 ++--------- .../CachingTransformerRegistry.php | 6 +----- .../Implementation/TransformerRegistry.php | 7 +------ tests/Common/IterableMapperDecorator.php | 6 +++--- tests/Common/MapperDecorator.php | 6 +++--- .../Doctrine/EntityWithMultipleIdentifier.php | 21 ++++++------------- .../Doctrine/EntityWithSingleIdentifier.php | 14 ++++--------- tests/Fixtures/MethodMapper/MoneyDto.php | 6 +++--- tests/Fixtures/ObjectMapper/MoneyDto.php | 6 +++--- 14 files changed, 41 insertions(+), 88 deletions(-) diff --git a/rector.php b/rector.php index bbc974ce..5d607bca 100644 --- a/rector.php +++ b/rector.php @@ -15,6 +15,7 @@ use Rector\Php80\Rector\FuncCall\ClassOnObjectRector; use Rector\Php80\Rector\FunctionLike\MixedTypeRector; use Rector\Php81\Rector\Property\ReadOnlyPropertyRector; +use Rector\Php82\Rector\Class_\ReadOnlyClassRector; use Rector\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector; use Rector\Strict\Rector\Ternary\DisallowedShortTernaryRuleFixerRector; @@ -35,9 +36,10 @@ // doctrineCodeQuality: true, ) // uncomment to reach your current PHP version - // ->withPhpSets(php82: true) + ->withPhpSets(php82: true) ->withRules([ MixedTypeRector::class, + ReadOnlyClassRector::class, ReadOnlyPropertyRector::class, RemoveUnusedVariableInCatchRector::class, ClassOnObjectRector::class, diff --git a/src/CustomMapper/Implementation/ObjectMapperTableFactory.php b/src/CustomMapper/Implementation/ObjectMapperTableFactory.php index 47f17849..ba451432 100644 --- a/src/CustomMapper/Implementation/ObjectMapperTableFactory.php +++ b/src/CustomMapper/Implementation/ObjectMapperTableFactory.php @@ -20,9 +20,9 @@ /** * @internal */ -final class ObjectMapperTableFactory implements ObjectMapperTableFactoryInterface +final readonly class ObjectMapperTableFactory implements ObjectMapperTableFactoryInterface { - private readonly ObjectMapperTable $objectMapperTable; + private ObjectMapperTable $objectMapperTable; public function __construct() { diff --git a/src/MapperFactory.php b/src/MapperFactory.php index 8a22b2a2..17039bc1 100644 --- a/src/MapperFactory.php +++ b/src/MapperFactory.php @@ -134,8 +134,6 @@ class MapperFactory private ?SymfonyUidTransformer $symfonyUidTransformer = null; private ?RamseyUuidTransformer $ramseyUuidTransformer = null; private ?PresetTransformer $presetTransformer = null; - - private readonly CacheItemPoolInterface $propertyInfoExtractorCache; private null|(PropertyInfoExtractorInterface&PropertyInitializableExtractorInterface) $propertyInfoExtractor = null; private ?TypeResolverInterface $typeResolver = null; private ?ObjectToObjectMetadataFactoryInterface $objectToObjectMetadataFactory = null; @@ -167,15 +165,8 @@ class MapperFactory /** * @param array $additionalTransformers */ - public function __construct( - private readonly array $additionalTransformers = [], - private ?ReflectionExtractor $reflectionExtractor = null, - private ?PhpStanExtractor $phpStanExtractor = null, - private readonly ?NormalizerInterface $normalizer = null, - private readonly ?DenormalizerInterface $denormalizer = null, - ?CacheItemPoolInterface $propertyInfoExtractorCache = null, - ) { - $this->propertyInfoExtractorCache = $propertyInfoExtractorCache ?? new ArrayAdapter(); + public function __construct(private readonly array $additionalTransformers = [], private ?ReflectionExtractor $reflectionExtractor = null, private ?PhpStanExtractor $phpStanExtractor = null, private readonly ?NormalizerInterface $normalizer = null, private readonly ?DenormalizerInterface $denormalizer = null, private readonly CacheItemPoolInterface $propertyInfoExtractorCache = new ArrayAdapter()) + { } /** diff --git a/src/Transformer/Implementation/ObjectToObjectTransformer.php b/src/Transformer/Implementation/ObjectToObjectTransformer.php index 218482f5..ffbdc89e 100644 --- a/src/Transformer/Implementation/ObjectToObjectTransformer.php +++ b/src/Transformer/Implementation/ObjectToObjectTransformer.php @@ -408,9 +408,7 @@ private function readSourcePropertyAndWriteTargetProperty( target: $target, context: $context ); - } catch (UninitializedSourcePropertyException) { - return; - } catch (UnsupportedPropertyMappingException) { + } catch (UninitializedSourcePropertyException|UnsupportedPropertyMappingException) { return; } diff --git a/src/Transformer/Implementation/ScalarToScalarTransformer.php b/src/Transformer/Implementation/ScalarToScalarTransformer.php index 79d584ed..a5b9b4a3 100644 --- a/src/Transformer/Implementation/ScalarToScalarTransformer.php +++ b/src/Transformer/Implementation/ScalarToScalarTransformer.php @@ -34,19 +34,13 @@ public function transform( } $targetTypeBuiltIn = $targetType?->getBuiltinType(); - - switch ($targetTypeBuiltIn) { - case Type::BUILTIN_TYPE_INT: - return (int) $source; - case Type::BUILTIN_TYPE_FLOAT: - return (float) $source; - case Type::BUILTIN_TYPE_STRING: - return (string) $source; - case Type::BUILTIN_TYPE_BOOL: - return (bool) $source; - } - - throw new InvalidArgumentException(sprintf('Target must be scalar, "%s" given.', get_debug_type($targetType)), context: $context); + return match ($targetTypeBuiltIn) { + Type::BUILTIN_TYPE_INT => (int) $source, + Type::BUILTIN_TYPE_FLOAT => (float) $source, + Type::BUILTIN_TYPE_STRING => (string) $source, + Type::BUILTIN_TYPE_BOOL => (bool) $source, + default => throw new InvalidArgumentException(sprintf('Target must be scalar, "%s" given.', get_debug_type($targetType)), context: $context), + }; } public function getSupportedTransformation(): iterable diff --git a/src/Transformer/Util/ReaderWriter.php b/src/Transformer/Util/ReaderWriter.php index 503d9963..1ce3a2d5 100644 --- a/src/Transformer/Util/ReaderWriter.php +++ b/src/Transformer/Util/ReaderWriter.php @@ -61,11 +61,7 @@ public function readSourceProperty( /** @psalm-suppress MixedMethodCall */ return $source->{$accessorName}(); } elseif ($mode === ReadMode::DynamicProperty) { - if (isset($source->{$accessorName})) { - return $source->{$accessorName}; - } - - return null; + return $source->{$accessorName} ?? null; } return null; @@ -129,11 +125,7 @@ public function readTargetProperty( /** @psalm-suppress MixedMethodCall */ return $target->{$accessorName}(); } elseif ($readMode === ReadMode::DynamicProperty) { - if (isset($target->{$accessorName})) { - return $target->{$accessorName}; - } - - return null; + return $target->{$accessorName} ?? null; } return null; diff --git a/src/TransformerRegistry/Implementation/CachingTransformerRegistry.php b/src/TransformerRegistry/Implementation/CachingTransformerRegistry.php index 2ef9d58a..cc71c2c0 100644 --- a/src/TransformerRegistry/Implementation/CachingTransformerRegistry.php +++ b/src/TransformerRegistry/Implementation/CachingTransformerRegistry.php @@ -41,11 +41,7 @@ public function __construct( public function get(string $id): TransformerInterface { - if (isset($this->transformers[$id])) { - return $this->transformers[$id]; - } - - return $this->transformers[$id] = $this->decorated->get($id); + return $this->transformers[$id] ?? ($this->transformers[$id] = $this->decorated->get($id)); } public function findBySourceAndTargetTypes( diff --git a/src/TransformerRegistry/Implementation/TransformerRegistry.php b/src/TransformerRegistry/Implementation/TransformerRegistry.php index e9e1e13e..07b1af4a 100644 --- a/src/TransformerRegistry/Implementation/TransformerRegistry.php +++ b/src/TransformerRegistry/Implementation/TransformerRegistry.php @@ -148,12 +148,7 @@ public function findBySourceAndTargetTypes( } } - usort($searchResultEntries, function ( - SearchResultEntry $a, - SearchResultEntry $b - ) { - return $a->getMappingOrder() <=> $b->getMappingOrder(); - }); + usort($searchResultEntries, fn (SearchResultEntry $a, SearchResultEntry $b) => $a->getMappingOrder() <=> $b->getMappingOrder()); return new SearchResult($searchResultEntries); } diff --git a/tests/Common/IterableMapperDecorator.php b/tests/Common/IterableMapperDecorator.php index c21b1020..4bc34a94 100644 --- a/tests/Common/IterableMapperDecorator.php +++ b/tests/Common/IterableMapperDecorator.php @@ -16,11 +16,11 @@ use Rekalogika\Mapper\Context\Context; use Rekalogika\Mapper\IterableMapperInterface; -final class IterableMapperDecorator implements IterableMapperInterface +final readonly class IterableMapperDecorator implements IterableMapperInterface { public function __construct( - private readonly IterableMapperInterface $decorated, - private readonly Context $defaultContext + private IterableMapperInterface $decorated, + private Context $defaultContext ) { } diff --git a/tests/Common/MapperDecorator.php b/tests/Common/MapperDecorator.php index 26157b7c..049dc994 100644 --- a/tests/Common/MapperDecorator.php +++ b/tests/Common/MapperDecorator.php @@ -16,11 +16,11 @@ use Rekalogika\Mapper\Context\Context; use Rekalogika\Mapper\MapperInterface; -final class MapperDecorator implements MapperInterface +final readonly class MapperDecorator implements MapperInterface { public function __construct( - private readonly MapperInterface $decorated, - private readonly Context $defaultContext + private MapperInterface $decorated, + private Context $defaultContext ) { } diff --git a/tests/Fixtures/Doctrine/EntityWithMultipleIdentifier.php b/tests/Fixtures/Doctrine/EntityWithMultipleIdentifier.php index 3a42733e..d9dd1b18 100644 --- a/tests/Fixtures/Doctrine/EntityWithMultipleIdentifier.php +++ b/tests/Fixtures/Doctrine/EntityWithMultipleIdentifier.php @@ -18,22 +18,13 @@ #[ORM\Entity] class EntityWithMultipleIdentifier { - #[ORM\Id] - #[ORM\Column(name: 'id1', type: 'string', length: 255)] - private string $id1; - - #[ORM\Id] - #[ORM\Column(name: 'id2', type: 'string', length: 255)] - private string $id2; - - #[ORM\Column] - private string $name; - - public function __construct(string $id1, string $id2, string $name) + public function __construct(#[ORM\Id] + #[ORM\Column(name: 'id1', type: 'string', length: 255)] + private string $id1, #[ORM\Id] + #[ORM\Column(name: 'id2', type: 'string', length: 255)] + private string $id2, #[ORM\Column] + private string $name) { - $this->id1 = $id1; - $this->id2 = $id2; - $this->name = $name; } public function getId1(): string diff --git a/tests/Fixtures/Doctrine/EntityWithSingleIdentifier.php b/tests/Fixtures/Doctrine/EntityWithSingleIdentifier.php index af52f67f..fcd642c2 100644 --- a/tests/Fixtures/Doctrine/EntityWithSingleIdentifier.php +++ b/tests/Fixtures/Doctrine/EntityWithSingleIdentifier.php @@ -20,13 +20,6 @@ #[ORM\Entity] class EntityWithSingleIdentifier { - #[ORM\Id] - #[ORM\Column(name: 'my_identifier', type: 'string', length: 255)] - private string $myIdentifier; - - #[ORM\Column] - private string $name; - #[ORM\ManyToOne(targetEntity: self::class, inversedBy: 'children')] #[ORM\JoinColumn(name: 'parent_id', referencedColumnName: 'my_identifier')] private ?self $parent = null; @@ -37,10 +30,11 @@ class EntityWithSingleIdentifier #[ORM\OneToMany(targetEntity: self::class, mappedBy: 'parent')] private Collection $children; - public function __construct(string $myIdentifier, string $name) + public function __construct(#[ORM\Id] + #[ORM\Column(name: 'my_identifier', type: 'string', length: 255)] + private string $myIdentifier, #[ORM\Column] + private string $name) { - $this->myIdentifier = $myIdentifier; - $this->name = $name; $this->children = new ArrayCollection(); } diff --git a/tests/Fixtures/MethodMapper/MoneyDto.php b/tests/Fixtures/MethodMapper/MoneyDto.php index cc42cd1b..dd6f3e95 100644 --- a/tests/Fixtures/MethodMapper/MoneyDto.php +++ b/tests/Fixtures/MethodMapper/MoneyDto.php @@ -23,11 +23,11 @@ * @deprecated * @psalm-suppress DeprecatedInterface */ -final class MoneyDto implements MapToObjectInterface, MapFromObjectInterface +final readonly class MoneyDto implements MapToObjectInterface, MapFromObjectInterface { public function __construct( - private readonly string $amount, - private readonly string $currency, + private string $amount, + private string $currency, ) { } diff --git a/tests/Fixtures/ObjectMapper/MoneyDto.php b/tests/Fixtures/ObjectMapper/MoneyDto.php index 5d51739a..3d1d0f07 100644 --- a/tests/Fixtures/ObjectMapper/MoneyDto.php +++ b/tests/Fixtures/ObjectMapper/MoneyDto.php @@ -13,11 +13,11 @@ namespace Rekalogika\Mapper\Tests\Fixtures\ObjectMapper; -final class MoneyDto +final readonly class MoneyDto { public function __construct( - private readonly string $amount, - private readonly string $currency, + private string $amount, + private string $currency, ) { }