Skip to content

Commit

Permalink
ReadOnlyClassRector
Browse files Browse the repository at this point in the history
  • Loading branch information
priyadi committed Jul 18, 2024
1 parent 9e5b8e1 commit 0d06f0e
Show file tree
Hide file tree
Showing 14 changed files with 41 additions and 88 deletions.
4 changes: 3 additions & 1 deletion rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/CustomMapper/Implementation/ObjectMapperTableFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
13 changes: 2 additions & 11 deletions src/MapperFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -167,15 +165,8 @@ class MapperFactory
/**
* @param array<string,TransformerInterface> $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())
{
}

/**
Expand Down
4 changes: 1 addition & 3 deletions src/Transformer/Implementation/ObjectToObjectTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -408,9 +408,7 @@ private function readSourcePropertyAndWriteTargetProperty(
target: $target,
context: $context
);
} catch (UninitializedSourcePropertyException) {
return;
} catch (UnsupportedPropertyMappingException) {
} catch (UninitializedSourcePropertyException|UnsupportedPropertyMappingException) {
return;
}

Expand Down
20 changes: 7 additions & 13 deletions src/Transformer/Implementation/ScalarToScalarTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 2 additions & 10 deletions src/Transformer/Util/ReaderWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
6 changes: 3 additions & 3 deletions tests/Common/IterableMapperDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
) {
}

Expand Down
6 changes: 3 additions & 3 deletions tests/Common/MapperDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
) {
}

Expand Down
21 changes: 6 additions & 15 deletions tests/Fixtures/Doctrine/EntityWithMultipleIdentifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 4 additions & 10 deletions tests/Fixtures/Doctrine/EntityWithSingleIdentifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}

Expand Down
6 changes: 3 additions & 3 deletions tests/Fixtures/MethodMapper/MoneyDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
) {
}

Expand Down
6 changes: 3 additions & 3 deletions tests/Fixtures/ObjectMapper/MoneyDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
) {
}

Expand Down

0 comments on commit 0d06f0e

Please sign in to comment.