From 7ff739a0ef2f779db365688dfc6cd9e4ce3f680d Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Fri, 11 Mar 2022 23:40:38 +0100 Subject: [PATCH] [Serializer] Add types to private and internal properties --- .../DependencyInjection/MainConfiguration.php | 4 +-- .../Security/Factory/AccessTokenFactory.php | 2 +- .../Form/ChoiceList/View/ChoiceGroupView.php | 2 +- .../Serializer/Annotation/Context.php | 6 ++-- .../Annotation/DiscriminatorMap.php | 4 +-- .../Serializer/Annotation/Groups.php | 2 +- .../Serializer/Annotation/MaxDepth.php | 2 +- .../Serializer/Annotation/SerializedName.php | 2 +- .../CompiledClassMetadataCacheWarmer.php | 20 ++++--------- .../Serializer/Encoder/ChainDecoder.php | 13 ++++++--- .../Serializer/Encoder/ChainEncoder.php | 13 ++++++--- .../Serializer/Encoder/CsvEncoder.php | 2 +- .../Serializer/Encoder/JsonDecode.php | 2 +- .../Serializer/Encoder/JsonEncode.php | 2 +- .../Serializer/Encoder/XmlEncoder.php | 2 +- .../Serializer/Encoder/YamlEncoder.php | 6 ++-- .../Exception/ExtraAttributesException.php | 10 +++---- .../NotNormalizableValueException.php | 17 ++++++----- .../Extractor/ObjectPropertyListExtractor.php | 8 ++--- .../Serializer/Mapping/AttributeMetadata.php | 20 +++++-------- .../ClassDiscriminatorFromClassMetadata.php | 12 +++----- .../Mapping/ClassDiscriminatorMapping.php | 15 +++++----- .../Serializer/Mapping/ClassMetadata.php | 13 +++------ .../Mapping/ClassMetadataInterface.php | 2 +- .../Factory/CacheClassMetadataFactory.php | 19 ++++-------- .../Mapping/Factory/ClassMetadataFactory.php | 12 ++++---- .../Factory/CompiledClassMetadataFactory.php | 13 ++++----- .../Mapping/Loader/AnnotationLoader.php | 8 ++--- .../Serializer/Mapping/Loader/LoaderChain.php | 6 +--- .../Mapping/Loader/XmlFileLoader.php | 2 +- .../Mapping/Loader/YamlFileLoader.php | 6 ++-- .../CamelCaseToSnakeCaseNameConverter.php | 11 +++---- .../MetadataAwareNameConverter.php | 29 +++++++++++-------- .../Normalizer/AbstractObjectNormalizer.php | 28 +++++++++--------- .../ConstraintViolationListNormalizer.php | 11 +++---- .../Normalizer/DataUriNormalizer.php | 11 ++----- .../Normalizer/DateIntervalNormalizer.php | 2 +- .../Normalizer/DateTimeNormalizer.php | 2 +- .../Normalizer/MimeMessageNormalizer.php | 4 +-- .../Normalizer/ObjectNormalizer.php | 7 +++-- .../Serializer/Normalizer/UidNormalizer.php | 2 +- .../Normalizer/UnwrappingDenormalizer.php | 2 +- .../Component/Serializer/Serializer.php | 23 ++++++++++----- 43 files changed, 173 insertions(+), 206 deletions(-) diff --git a/src/Symfony/Bundle/SecurityBundle/DependencyInjection/MainConfiguration.php b/src/Symfony/Bundle/SecurityBundle/DependencyInjection/MainConfiguration.php index c6ba45edc2e47..e982fc1871940 100644 --- a/src/Symfony/Bundle/SecurityBundle/DependencyInjection/MainConfiguration.php +++ b/src/Symfony/Bundle/SecurityBundle/DependencyInjection/MainConfiguration.php @@ -40,7 +40,7 @@ class MainConfiguration implements ConfigurationInterface private array $userProviderFactories; /** - * @param array $factories + * @param array $factories */ public function __construct(array $factories, array $userProviderFactories) { @@ -173,7 +173,7 @@ private function addAccessControlSection(ArrayNodeDefinition $rootNode): void } /** - * @param array $factories + * @param array $factories */ private function addFirewallsSection(ArrayNodeDefinition $rootNode, array $factories): void { diff --git a/src/Symfony/Bundle/SecurityBundle/DependencyInjection/Security/Factory/AccessTokenFactory.php b/src/Symfony/Bundle/SecurityBundle/DependencyInjection/Security/Factory/AccessTokenFactory.php index 28d0beda0997e..04e419cd82d6b 100644 --- a/src/Symfony/Bundle/SecurityBundle/DependencyInjection/Security/Factory/AccessTokenFactory.php +++ b/src/Symfony/Bundle/SecurityBundle/DependencyInjection/Security/Factory/AccessTokenFactory.php @@ -30,7 +30,7 @@ final class AccessTokenFactory extends AbstractFactory implements StatelessAuthe private const PRIORITY = -40; /** - * @param array $tokenHandlerFactories + * @param array $tokenHandlerFactories */ public function __construct(private readonly array $tokenHandlerFactories) { diff --git a/src/Symfony/Component/Form/ChoiceList/View/ChoiceGroupView.php b/src/Symfony/Component/Form/ChoiceList/View/ChoiceGroupView.php index 453601a6f7221..64fe3baec3829 100644 --- a/src/Symfony/Component/Form/ChoiceList/View/ChoiceGroupView.php +++ b/src/Symfony/Component/Form/ChoiceList/View/ChoiceGroupView.php @@ -26,7 +26,7 @@ class ChoiceGroupView implements \IteratorAggregate /** * Creates a new choice group view. * - * @param array $choices the choice views in the group + * @param array $choices the choice views in the group */ public function __construct(string $label, array $choices = []) { diff --git a/src/Symfony/Component/Serializer/Annotation/Context.php b/src/Symfony/Component/Serializer/Annotation/Context.php index 908825047b001..e3878c7be5992 100644 --- a/src/Symfony/Component/Serializer/Annotation/Context.php +++ b/src/Symfony/Component/Serializer/Annotation/Context.php @@ -33,9 +33,9 @@ class Context * @throws InvalidArgumentException */ public function __construct( - private array $context = [], - private array $normalizationContext = [], - private array $denormalizationContext = [], + private readonly array $context = [], + private readonly array $normalizationContext = [], + private readonly array $denormalizationContext = [], string|array $groups = [], ) { if (!$context && !$normalizationContext && !$denormalizationContext) { diff --git a/src/Symfony/Component/Serializer/Annotation/DiscriminatorMap.php b/src/Symfony/Component/Serializer/Annotation/DiscriminatorMap.php index 2606a1e4f20b4..8e9ebb830250a 100644 --- a/src/Symfony/Component/Serializer/Annotation/DiscriminatorMap.php +++ b/src/Symfony/Component/Serializer/Annotation/DiscriminatorMap.php @@ -26,8 +26,8 @@ class DiscriminatorMap { public function __construct( - private string $typeProperty, - private array $mapping, + private readonly string $typeProperty, + private readonly array $mapping, ) { if (empty($typeProperty)) { throw new InvalidArgumentException(sprintf('Parameter "typeProperty" of annotation "%s" cannot be empty.', static::class)); diff --git a/src/Symfony/Component/Serializer/Annotation/Groups.php b/src/Symfony/Component/Serializer/Annotation/Groups.php index 2069d678c91b2..5b16f567c3547 100644 --- a/src/Symfony/Component/Serializer/Annotation/Groups.php +++ b/src/Symfony/Component/Serializer/Annotation/Groups.php @@ -28,7 +28,7 @@ class Groups /** * @var string[] */ - private array $groups; + private readonly array $groups; /** * @param string|string[] $groups diff --git a/src/Symfony/Component/Serializer/Annotation/MaxDepth.php b/src/Symfony/Component/Serializer/Annotation/MaxDepth.php index 43785b292a4fb..36afff6a7490c 100644 --- a/src/Symfony/Component/Serializer/Annotation/MaxDepth.php +++ b/src/Symfony/Component/Serializer/Annotation/MaxDepth.php @@ -25,7 +25,7 @@ #[\Attribute(\Attribute::TARGET_METHOD | \Attribute::TARGET_PROPERTY)] class MaxDepth { - public function __construct(private int $maxDepth) + public function __construct(private readonly int $maxDepth) { if ($maxDepth <= 0) { throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" must be a positive integer.', static::class)); diff --git a/src/Symfony/Component/Serializer/Annotation/SerializedName.php b/src/Symfony/Component/Serializer/Annotation/SerializedName.php index b6c6027e8568e..74df79cd27ea7 100644 --- a/src/Symfony/Component/Serializer/Annotation/SerializedName.php +++ b/src/Symfony/Component/Serializer/Annotation/SerializedName.php @@ -25,7 +25,7 @@ #[\Attribute(\Attribute::TARGET_METHOD | \Attribute::TARGET_PROPERTY)] final class SerializedName { - public function __construct(private string $serializedName) + public function __construct(private readonly string $serializedName) { if ('' === $serializedName) { throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" must be a non-empty string.', self::class)); diff --git a/src/Symfony/Component/Serializer/CacheWarmer/CompiledClassMetadataCacheWarmer.php b/src/Symfony/Component/Serializer/CacheWarmer/CompiledClassMetadataCacheWarmer.php index 565320af97b69..a43e9b4080cdb 100644 --- a/src/Symfony/Component/Serializer/CacheWarmer/CompiledClassMetadataCacheWarmer.php +++ b/src/Symfony/Component/Serializer/CacheWarmer/CompiledClassMetadataCacheWarmer.php @@ -21,20 +21,12 @@ */ final class CompiledClassMetadataCacheWarmer implements CacheWarmerInterface { - private $classesToCompile; - - private $classMetadataFactory; - - private $classMetadataFactoryCompiler; - - private $filesystem; - - public function __construct(array $classesToCompile, ClassMetadataFactoryInterface $classMetadataFactory, ClassMetadataFactoryCompiler $classMetadataFactoryCompiler, Filesystem $filesystem) - { - $this->classesToCompile = $classesToCompile; - $this->classMetadataFactory = $classMetadataFactory; - $this->classMetadataFactoryCompiler = $classMetadataFactoryCompiler; - $this->filesystem = $filesystem; + public function __construct( + private readonly array $classesToCompile, + private readonly ClassMetadataFactoryInterface $classMetadataFactory, + private readonly ClassMetadataFactoryCompiler $classMetadataFactoryCompiler, + private readonly Filesystem $filesystem, + ) { } public function warmUp(string $cacheDir): array diff --git a/src/Symfony/Component/Serializer/Encoder/ChainDecoder.php b/src/Symfony/Component/Serializer/Encoder/ChainDecoder.php index 469affdaa4434..f1b0cd2e267a3 100644 --- a/src/Symfony/Component/Serializer/Encoder/ChainDecoder.php +++ b/src/Symfony/Component/Serializer/Encoder/ChainDecoder.php @@ -24,12 +24,17 @@ */ class ChainDecoder implements ContextAwareDecoderInterface { - private array $decoders = []; + /** + * @var array + */ private array $decoderByFormat = []; - public function __construct(array $decoders = []) - { - $this->decoders = $decoders; + /** + * @param array $decoders + */ + public function __construct( + private readonly array $decoders = [] + ) { } final public function decode(string $data, string $format, array $context = []): mixed diff --git a/src/Symfony/Component/Serializer/Encoder/ChainEncoder.php b/src/Symfony/Component/Serializer/Encoder/ChainEncoder.php index 14aea51575ec5..731cfc6019df4 100644 --- a/src/Symfony/Component/Serializer/Encoder/ChainEncoder.php +++ b/src/Symfony/Component/Serializer/Encoder/ChainEncoder.php @@ -25,12 +25,17 @@ */ class ChainEncoder implements ContextAwareEncoderInterface { - private array $encoders = []; + /** + * @var array + */ private array $encoderByFormat = []; - public function __construct(array $encoders = []) - { - $this->encoders = $encoders; + /** + * @param array $encoders + */ + public function __construct( + private readonly array $encoders = [] + ) { } final public function encode(mixed $data, string $format, array $context = []): string diff --git a/src/Symfony/Component/Serializer/Encoder/CsvEncoder.php b/src/Symfony/Component/Serializer/Encoder/CsvEncoder.php index d8cc6e3f33afb..dc952b2e00e5c 100644 --- a/src/Symfony/Component/Serializer/Encoder/CsvEncoder.php +++ b/src/Symfony/Component/Serializer/Encoder/CsvEncoder.php @@ -38,7 +38,7 @@ class CsvEncoder implements EncoderInterface, DecoderInterface private const FORMULAS_START_CHARACTERS = ['=', '-', '+', '@', "\t", "\r"]; - private $defaultContext = [ + private array $defaultContext = [ self::DELIMITER_KEY => ',', self::ENCLOSURE_KEY => '"', self::ESCAPE_CHAR_KEY => '', diff --git a/src/Symfony/Component/Serializer/Encoder/JsonDecode.php b/src/Symfony/Component/Serializer/Encoder/JsonDecode.php index 059ed79273948..f17267fc95a6d 100644 --- a/src/Symfony/Component/Serializer/Encoder/JsonDecode.php +++ b/src/Symfony/Component/Serializer/Encoder/JsonDecode.php @@ -34,7 +34,7 @@ class JsonDecode implements DecoderInterface */ public const RECURSION_DEPTH = 'json_decode_recursion_depth'; - private $defaultContext = [ + private array $defaultContext = [ self::ASSOCIATIVE => false, self::OPTIONS => 0, self::RECURSION_DEPTH => 512, diff --git a/src/Symfony/Component/Serializer/Encoder/JsonEncode.php b/src/Symfony/Component/Serializer/Encoder/JsonEncode.php index 2f7c8157562e6..3e0ddced38a63 100644 --- a/src/Symfony/Component/Serializer/Encoder/JsonEncode.php +++ b/src/Symfony/Component/Serializer/Encoder/JsonEncode.php @@ -25,7 +25,7 @@ class JsonEncode implements EncoderInterface */ public const OPTIONS = 'json_encode_options'; - private $defaultContext = [ + private array $defaultContext = [ self::OPTIONS => \JSON_PRESERVE_ZERO_FRACTION, ]; diff --git a/src/Symfony/Component/Serializer/Encoder/XmlEncoder.php b/src/Symfony/Component/Serializer/Encoder/XmlEncoder.php index 965683c899fde..4b6f68921f42e 100644 --- a/src/Symfony/Component/Serializer/Encoder/XmlEncoder.php +++ b/src/Symfony/Component/Serializer/Encoder/XmlEncoder.php @@ -59,7 +59,7 @@ class XmlEncoder implements EncoderInterface, DecoderInterface, NormalizationAwa public const TYPE_CAST_ATTRIBUTES = 'xml_type_cast_attributes'; public const VERSION = 'xml_version'; - private $defaultContext = [ + private array $defaultContext = [ self::AS_COLLECTION => false, self::DECODER_IGNORED_NODE_TYPES => [\XML_PI_NODE, \XML_COMMENT_NODE], self::ENCODER_IGNORED_NODE_TYPES => [], diff --git a/src/Symfony/Component/Serializer/Encoder/YamlEncoder.php b/src/Symfony/Component/Serializer/Encoder/YamlEncoder.php index 8d559a57b90d5..031a3105e4582 100644 --- a/src/Symfony/Component/Serializer/Encoder/YamlEncoder.php +++ b/src/Symfony/Component/Serializer/Encoder/YamlEncoder.php @@ -42,9 +42,9 @@ class YamlEncoder implements EncoderInterface, DecoderInterface public const YAML_INDENT = 'yaml_indent'; public const YAML_FLAGS = 'yaml_flags'; - private $dumper; - private $parser; - private $defaultContext = [ + private readonly Dumper $dumper; + private readonly Parser $parser; + private array $defaultContext = [ self::YAML_INLINE => 0, self::YAML_INDENT => 0, self::YAML_FLAGS => 0, diff --git a/src/Symfony/Component/Serializer/Exception/ExtraAttributesException.php b/src/Symfony/Component/Serializer/Exception/ExtraAttributesException.php index 22fcc87b86775..d681de419cf8c 100644 --- a/src/Symfony/Component/Serializer/Exception/ExtraAttributesException.php +++ b/src/Symfony/Component/Serializer/Exception/ExtraAttributesException.php @@ -18,14 +18,12 @@ */ class ExtraAttributesException extends RuntimeException { - private $extraAttributes; - - public function __construct(array $extraAttributes, \Throwable $previous = null) - { + public function __construct( + private readonly array $extraAttributes, + \Throwable $previous = null, + ) { $msg = sprintf('Extra attributes are not allowed ("%s" %s unknown).', implode('", "', $extraAttributes), \count($extraAttributes) > 1 ? 'are' : 'is'); - $this->extraAttributes = $extraAttributes; - parent::__construct($msg, 0, $previous); } diff --git a/src/Symfony/Component/Serializer/Exception/NotNormalizableValueException.php b/src/Symfony/Component/Serializer/Exception/NotNormalizableValueException.php index e601e5043e2e9..148c60ada4e1c 100644 --- a/src/Symfony/Component/Serializer/Exception/NotNormalizableValueException.php +++ b/src/Symfony/Component/Serializer/Exception/NotNormalizableValueException.php @@ -16,17 +16,18 @@ */ class NotNormalizableValueException extends UnexpectedValueException { - private $currentType; - private $expectedTypes; - private $path; - private $useMessageForUser = false; + private ?string $currentType = null; + private ?array $expectedTypes = null; + private ?string $path = null; + private bool $useMessageForUser = false; /** - * @param bool $useMessageForUser If the message passed to this exception is something that can be shown - * safely to your user. In other words, avoid catching other exceptions and - * passing their message directly to this class. + * @param string[] $expectedTypes + * @param bool $useMessageForUser If the message passed to this exception is something that can be shown + * safely to your user. In other words, avoid catching other exceptions and + * passing their message directly to this class. */ - public static function createForUnexpectedDataType(string $message, $data, array $expectedTypes, string $path = null, bool $useMessageForUser = false, int $code = 0, \Throwable $previous = null): self + public static function createForUnexpectedDataType(string $message, mixed $data, array $expectedTypes, string $path = null, bool $useMessageForUser = false, int $code = 0, \Throwable $previous = null): self { $self = new self($message, $code, $previous); diff --git a/src/Symfony/Component/Serializer/Extractor/ObjectPropertyListExtractor.php b/src/Symfony/Component/Serializer/Extractor/ObjectPropertyListExtractor.php index 9b73446273726..c237e13a76344 100644 --- a/src/Symfony/Component/Serializer/Extractor/ObjectPropertyListExtractor.php +++ b/src/Symfony/Component/Serializer/Extractor/ObjectPropertyListExtractor.php @@ -18,18 +18,18 @@ */ final class ObjectPropertyListExtractor implements ObjectPropertyListExtractorInterface { - private $propertyListExtractor; - private $objectClassResolver; + private PropertyListExtractorInterface $propertyListExtractor; + private \Closure $objectClassResolver; public function __construct(PropertyListExtractorInterface $propertyListExtractor, callable $objectClassResolver = null) { $this->propertyListExtractor = $propertyListExtractor; - $this->objectClassResolver = $objectClassResolver; + $this->objectClassResolver = ($objectClassResolver ?? 'get_class')(...); } public function getProperties(object $object, array $context = []): ?array { - $class = $this->objectClassResolver ? ($this->objectClassResolver)($object) : $object::class; + $class = ($this->objectClassResolver)($object); return $this->propertyListExtractor->getProperties($class, $context); } diff --git a/src/Symfony/Component/Serializer/Mapping/AttributeMetadata.php b/src/Symfony/Component/Serializer/Mapping/AttributeMetadata.php index cb77b53d24286..c77d07e5e38d3 100644 --- a/src/Symfony/Component/Serializer/Mapping/AttributeMetadata.php +++ b/src/Symfony/Component/Serializer/Mapping/AttributeMetadata.php @@ -23,32 +23,28 @@ class AttributeMetadata implements AttributeMetadataInterface * class' serialized representation. Do not access it. Use * {@link getName()} instead. */ - public $name; + public string $name; /** * @internal This property is public in order to reduce the size of the * class' serialized representation. Do not access it. Use * {@link getGroups()} instead. */ - public $groups = []; + public array $groups = []; /** - * @var int|null - * * @internal This property is public in order to reduce the size of the * class' serialized representation. Do not access it. Use * {@link getMaxDepth()} instead. */ - public $maxDepth; + public ?int $maxDepth = null; /** - * @var string|null - * * @internal This property is public in order to reduce the size of the * class' serialized representation. Do not access it. Use * {@link getSerializedName()} instead. */ - public $serializedName; + public ?string $serializedName = null; /** * @internal This property is public in order to reduce the size of the @@ -58,13 +54,11 @@ class AttributeMetadata implements AttributeMetadataInterface public ?PropertyPath $serializedPath = null; /** - * @var bool - * * @internal This property is public in order to reduce the size of the * class' serialized representation. Do not access it. Use * {@link isIgnored()} instead. */ - public $ignore = false; + public bool $ignore = false; /** * @var array[] Normalization contexts per group name ("*" applies to all groups) @@ -73,7 +67,7 @@ class AttributeMetadata implements AttributeMetadataInterface * class' serialized representation. Do not access it. Use * {@link getNormalizationContexts()} instead. */ - public $normalizationContexts = []; + public array $normalizationContexts = []; /** * @var array[] Denormalization contexts per group name ("*" applies to all groups) @@ -82,7 +76,7 @@ class AttributeMetadata implements AttributeMetadataInterface * class' serialized representation. Do not access it. Use * {@link getDenormalizationContexts()} instead. */ - public $denormalizationContexts = []; + public array $denormalizationContexts = []; public function __construct(string $name) { diff --git a/src/Symfony/Component/Serializer/Mapping/ClassDiscriminatorFromClassMetadata.php b/src/Symfony/Component/Serializer/Mapping/ClassDiscriminatorFromClassMetadata.php index 708d58e03d2d2..d6f4bbe67f896 100644 --- a/src/Symfony/Component/Serializer/Mapping/ClassDiscriminatorFromClassMetadata.php +++ b/src/Symfony/Component/Serializer/Mapping/ClassDiscriminatorFromClassMetadata.php @@ -18,15 +18,11 @@ */ class ClassDiscriminatorFromClassMetadata implements ClassDiscriminatorResolverInterface { - /** - * @var ClassMetadataFactoryInterface - */ - private $classMetadataFactory; - private $mappingForMappedObjectCache = []; + private array $mappingForMappedObjectCache = []; - public function __construct(ClassMetadataFactoryInterface $classMetadataFactory) - { - $this->classMetadataFactory = $classMetadataFactory; + public function __construct( + private readonly ClassMetadataFactoryInterface $classMetadataFactory, + ) { } public function getMappingForClass(string $class): ?ClassDiscriminatorMapping diff --git a/src/Symfony/Component/Serializer/Mapping/ClassDiscriminatorMapping.php b/src/Symfony/Component/Serializer/Mapping/ClassDiscriminatorMapping.php index c21d777ec8c9c..260575a411599 100644 --- a/src/Symfony/Component/Serializer/Mapping/ClassDiscriminatorMapping.php +++ b/src/Symfony/Component/Serializer/Mapping/ClassDiscriminatorMapping.php @@ -16,14 +16,13 @@ */ class ClassDiscriminatorMapping { - private $typeProperty; - private $typesMapping; - - public function __construct(string $typeProperty, array $typesMapping = []) - { - $this->typeProperty = $typeProperty; - $this->typesMapping = $typesMapping; - + /** + * @param array $typesMapping + */ + public function __construct( + private readonly string $typeProperty, + private array $typesMapping = [], + ) { uasort($this->typesMapping, static function (string $a, string $b): int { if (is_a($a, $b, true)) { return -1; diff --git a/src/Symfony/Component/Serializer/Mapping/ClassMetadata.php b/src/Symfony/Component/Serializer/Mapping/ClassMetadata.php index 4a70ee0858b91..bcec5bf969e7d 100644 --- a/src/Symfony/Component/Serializer/Mapping/ClassMetadata.php +++ b/src/Symfony/Component/Serializer/Mapping/ClassMetadata.php @@ -21,7 +21,7 @@ class ClassMetadata implements ClassMetadataInterface * class' serialized representation. Do not access it. Use * {@link getName()} instead. */ - public $name; + public string $name; /** * @var AttributeMetadataInterface[] @@ -30,21 +30,16 @@ class ClassMetadata implements ClassMetadataInterface * class' serialized representation. Do not access it. Use * {@link getAttributesMetadata()} instead. */ - public $attributesMetadata = []; + public array $attributesMetadata = []; - /** - * @var \ReflectionClass - */ - private $reflClass; + private ?\ReflectionClass $reflClass = null; /** - * @var ClassDiscriminatorMapping|null - * * @internal This property is public in order to reduce the size of the * class' serialized representation. Do not access it. Use * {@link getClassDiscriminatorMapping()} instead. */ - public $classDiscriminatorMapping; + public ?ClassDiscriminatorMapping $classDiscriminatorMapping; /** * Constructs a metadata for the given class. diff --git a/src/Symfony/Component/Serializer/Mapping/ClassMetadataInterface.php b/src/Symfony/Component/Serializer/Mapping/ClassMetadataInterface.php index b19cb1a462068..3f380d2ff19d2 100644 --- a/src/Symfony/Component/Serializer/Mapping/ClassMetadataInterface.php +++ b/src/Symfony/Component/Serializer/Mapping/ClassMetadataInterface.php @@ -37,7 +37,7 @@ public function addAttributeMetadata(AttributeMetadataInterface $attributeMetada /** * Gets the list of {@link AttributeMetadataInterface}. * - * @return AttributeMetadataInterface[] + * @return array */ public function getAttributesMetadata(): array; diff --git a/src/Symfony/Component/Serializer/Mapping/Factory/CacheClassMetadataFactory.php b/src/Symfony/Component/Serializer/Mapping/Factory/CacheClassMetadataFactory.php index c3adbeec136d8..61c2fb6069913 100644 --- a/src/Symfony/Component/Serializer/Mapping/Factory/CacheClassMetadataFactory.php +++ b/src/Symfony/Component/Serializer/Mapping/Factory/CacheClassMetadataFactory.php @@ -24,21 +24,14 @@ class CacheClassMetadataFactory implements ClassMetadataFactoryInterface use ClassResolverTrait; /** - * @var ClassMetadataFactoryInterface + * @var array */ - private $decorated; + private array $loadedClasses = []; - /** - * @var CacheItemPoolInterface - */ - private $cacheItemPool; - - private $loadedClasses = []; - - public function __construct(ClassMetadataFactoryInterface $decorated, CacheItemPoolInterface $cacheItemPool) - { - $this->decorated = $decorated; - $this->cacheItemPool = $cacheItemPool; + public function __construct( + private readonly ClassMetadataFactoryInterface $decorated, + private readonly CacheItemPoolInterface $cacheItemPool, + ) { } public function getMetadataFor(string|object $value): ClassMetadataInterface diff --git a/src/Symfony/Component/Serializer/Mapping/Factory/ClassMetadataFactory.php b/src/Symfony/Component/Serializer/Mapping/Factory/ClassMetadataFactory.php index 9b2cf1cb7cbaa..fc5b2e71e5c16 100644 --- a/src/Symfony/Component/Serializer/Mapping/Factory/ClassMetadataFactory.php +++ b/src/Symfony/Component/Serializer/Mapping/Factory/ClassMetadataFactory.php @@ -24,16 +24,14 @@ class ClassMetadataFactory implements ClassMetadataFactoryInterface { use ClassResolverTrait; - private $loader; - /** - * @var array + * @var array */ - private $loadedClasses; + private array $loadedClasses; - public function __construct(LoaderInterface $loader) - { - $this->loader = $loader; + public function __construct( + private readonly LoaderInterface $loader, + ) { } public function getMetadataFor(string|object $value): ClassMetadataInterface diff --git a/src/Symfony/Component/Serializer/Mapping/Factory/CompiledClassMetadataFactory.php b/src/Symfony/Component/Serializer/Mapping/Factory/CompiledClassMetadataFactory.php index 670a935875d94..f4d41c1e68194 100644 --- a/src/Symfony/Component/Serializer/Mapping/Factory/CompiledClassMetadataFactory.php +++ b/src/Symfony/Component/Serializer/Mapping/Factory/CompiledClassMetadataFactory.php @@ -21,14 +21,14 @@ */ final class CompiledClassMetadataFactory implements ClassMetadataFactoryInterface { - private $compiledClassMetadata = []; + private array $compiledClassMetadata = []; - private $loadedClasses = []; + private array $loadedClasses = []; - private $classMetadataFactory; - - public function __construct(string $compiledClassMetadataFile, ClassMetadataFactoryInterface $classMetadataFactory) - { + public function __construct( + string $compiledClassMetadataFile, + private readonly ClassMetadataFactoryInterface $classMetadataFactory, + ) { if (!file_exists($compiledClassMetadataFile)) { throw new \RuntimeException("File \"{$compiledClassMetadataFile}\" could not be found."); } @@ -39,7 +39,6 @@ public function __construct(string $compiledClassMetadataFile, ClassMetadataFact } $this->compiledClassMetadata = $compiledClassMetadata; - $this->classMetadataFactory = $classMetadataFactory; } public function getMetadataFor(string|object $value): ClassMetadataInterface diff --git a/src/Symfony/Component/Serializer/Mapping/Loader/AnnotationLoader.php b/src/Symfony/Component/Serializer/Mapping/Loader/AnnotationLoader.php index cfcee8bd5013d..6d03511cf57e2 100644 --- a/src/Symfony/Component/Serializer/Mapping/Loader/AnnotationLoader.php +++ b/src/Symfony/Component/Serializer/Mapping/Loader/AnnotationLoader.php @@ -43,11 +43,9 @@ class AnnotationLoader implements LoaderInterface Context::class, ]; - private $reader; - - public function __construct(Reader $reader = null) - { - $this->reader = $reader; + public function __construct( + private readonly ?Reader $reader = null, + ) { } public function loadClassMetadata(ClassMetadataInterface $classMetadata): bool diff --git a/src/Symfony/Component/Serializer/Mapping/Loader/LoaderChain.php b/src/Symfony/Component/Serializer/Mapping/Loader/LoaderChain.php index c25f5fa967d9d..e93a0a7b0a7e2 100644 --- a/src/Symfony/Component/Serializer/Mapping/Loader/LoaderChain.php +++ b/src/Symfony/Component/Serializer/Mapping/Loader/LoaderChain.php @@ -27,8 +27,6 @@ */ class LoaderChain implements LoaderInterface { - private $loaders; - /** * Accepts a list of LoaderInterface instances. * @@ -36,15 +34,13 @@ class LoaderChain implements LoaderInterface * * @throws MappingException If any of the loaders does not implement LoaderInterface */ - public function __construct(array $loaders) + public function __construct(private readonly array $loaders) { foreach ($loaders as $loader) { if (!$loader instanceof LoaderInterface) { throw new MappingException(sprintf('Class "%s" is expected to implement LoaderInterface.', get_debug_type($loader))); } } - - $this->loaders = $loaders; } public function loadClassMetadata(ClassMetadataInterface $metadata): bool diff --git a/src/Symfony/Component/Serializer/Mapping/Loader/XmlFileLoader.php b/src/Symfony/Component/Serializer/Mapping/Loader/XmlFileLoader.php index ebaa4655dcc89..b36fb871d8f3a 100644 --- a/src/Symfony/Component/Serializer/Mapping/Loader/XmlFileLoader.php +++ b/src/Symfony/Component/Serializer/Mapping/Loader/XmlFileLoader.php @@ -31,7 +31,7 @@ class XmlFileLoader extends FileLoader * * @var \SimpleXMLElement[]|null */ - private $classes; + private ?array $classes = null; public function loadClassMetadata(ClassMetadataInterface $classMetadata): bool { diff --git a/src/Symfony/Component/Serializer/Mapping/Loader/YamlFileLoader.php b/src/Symfony/Component/Serializer/Mapping/Loader/YamlFileLoader.php index fd73864fdabe0..d47ad74301e66 100644 --- a/src/Symfony/Component/Serializer/Mapping/Loader/YamlFileLoader.php +++ b/src/Symfony/Component/Serializer/Mapping/Loader/YamlFileLoader.php @@ -27,14 +27,12 @@ */ class YamlFileLoader extends FileLoader { - private $yamlParser; + private ?Parser $yamlParser = null; /** * An array of YAML class descriptions. - * - * @var array */ - private $classes; + private ?array $classes = null; public function loadClassMetadata(ClassMetadataInterface $classMetadata): bool { diff --git a/src/Symfony/Component/Serializer/NameConverter/CamelCaseToSnakeCaseNameConverter.php b/src/Symfony/Component/Serializer/NameConverter/CamelCaseToSnakeCaseNameConverter.php index de34f5b57d2c9..ab6f99e13e0eb 100644 --- a/src/Symfony/Component/Serializer/NameConverter/CamelCaseToSnakeCaseNameConverter.php +++ b/src/Symfony/Component/Serializer/NameConverter/CamelCaseToSnakeCaseNameConverter.php @@ -18,17 +18,14 @@ */ class CamelCaseToSnakeCaseNameConverter implements NameConverterInterface { - private $attributes; - private $lowerCamelCase; - /** * @param array|null $attributes The list of attributes to rename or null for all attributes * @param bool $lowerCamelCase Use lowerCamelCase style */ - public function __construct(array $attributes = null, bool $lowerCamelCase = true) - { - $this->attributes = $attributes; - $this->lowerCamelCase = $lowerCamelCase; + public function __construct( + private ?array $attributes = null, + private bool $lowerCamelCase = true, + ) { } public function normalize(string $propertyName): string diff --git a/src/Symfony/Component/Serializer/NameConverter/MetadataAwareNameConverter.php b/src/Symfony/Component/Serializer/NameConverter/MetadataAwareNameConverter.php index 881d334bd94f2..0c97e227c47b0 100644 --- a/src/Symfony/Component/Serializer/NameConverter/MetadataAwareNameConverter.php +++ b/src/Symfony/Component/Serializer/NameConverter/MetadataAwareNameConverter.php @@ -20,23 +20,25 @@ */ final class MetadataAwareNameConverter implements AdvancedNameConverterInterface { - private $metadataFactory; - /** - * @var NameConverterInterface|AdvancedNameConverterInterface|null + * @var array> */ - private $fallbackNameConverter; - - private static $normalizeCache = []; + private static array $normalizeCache = []; - private static $denormalizeCache = []; + /** + * @var array> + */ + private static array $denormalizeCache = []; - private static $attributesMetadataCache = []; + /** + * @var array> + */ + private static array $attributesMetadataCache = []; - public function __construct(ClassMetadataFactoryInterface $metadataFactory, NameConverterInterface $fallbackNameConverter = null) - { - $this->metadataFactory = $metadataFactory; - $this->fallbackNameConverter = $fallbackNameConverter; + public function __construct( + private readonly ClassMetadataFactoryInterface $metadataFactory, + private readonly ?NameConverterInterface $fallbackNameConverter = null, + ) { } public function normalize(string $propertyName, string $class = null, string $format = null, array $context = []): string @@ -104,6 +106,9 @@ private function denormalizeFallback(string $propertyName, string $class = null, return $this->fallbackNameConverter ? $this->fallbackNameConverter->denormalize($propertyName, $class, $format, $context) : $propertyName; } + /** + * @return array + */ private function getCacheValueForAttributesMetadata(string $class, array $context): array { if (!$this->metadataFactory->hasMetadataFor($class)) { diff --git a/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php index 6f87c7d0a8ed1..88e3c3ea120bd 100644 --- a/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php @@ -105,19 +105,23 @@ abstract class AbstractObjectNormalizer extends AbstractNormalizer */ public const PRESERVE_EMPTY_OBJECTS = 'preserve_empty_objects'; - private $propertyTypeExtractor; - private $typesCache = []; - private $attributesCache = []; - - private $objectClassResolver; + private array $typesCache = []; + private array $attributesCache = []; + private readonly \Closure $objectClassResolver; /** * @var ClassDiscriminatorResolverInterface|null */ protected $classDiscriminatorResolver; - public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null, PropertyTypeExtractorInterface $propertyTypeExtractor = null, ClassDiscriminatorResolverInterface $classDiscriminatorResolver = null, callable $objectClassResolver = null, array $defaultContext = []) - { + public function __construct( + ClassMetadataFactoryInterface $classMetadataFactory = null, + NameConverterInterface $nameConverter = null, + private ?PropertyTypeExtractorInterface $propertyTypeExtractor = null, + ClassDiscriminatorResolverInterface $classDiscriminatorResolver = null, + callable $objectClassResolver = null, + array $defaultContext = [], + ) { parent::__construct($classMetadataFactory, $nameConverter, $defaultContext); if (isset($this->defaultContext[self::MAX_DEPTH_HANDLER]) && !\is_callable($this->defaultContext[self::MAX_DEPTH_HANDLER])) { @@ -126,13 +130,11 @@ public function __construct(ClassMetadataFactoryInterface $classMetadataFactory $this->defaultContext[self::EXCLUDE_FROM_CACHE_KEY] = array_merge($this->defaultContext[self::EXCLUDE_FROM_CACHE_KEY] ?? [], [self::CIRCULAR_REFERENCE_LIMIT_COUNTERS]); - $this->propertyTypeExtractor = $propertyTypeExtractor; - if (null === $classDiscriminatorResolver && null !== $classMetadataFactory) { $classDiscriminatorResolver = new ClassDiscriminatorFromClassMetadata($classMetadataFactory); } $this->classDiscriminatorResolver = $classDiscriminatorResolver; - $this->objectClassResolver = $objectClassResolver; + $this->objectClassResolver = ($objectClassResolver ?? 'get_class')(...); } /** @@ -158,7 +160,7 @@ public function normalize(mixed $object, string $format = null, array $context = $data = []; $stack = []; $attributes = $this->getAttributes($object, $format, $context); - $class = $this->objectClassResolver ? ($this->objectClassResolver)($object) : $object::class; + $class = ($this->objectClassResolver)($object); $classMetadata = $this->classMetadataFactory?->getMetadataFor($class); $attributesMetadata = $this->classMetadataFactory?->getMetadataFor($class)->getAttributesMetadata(); if (isset($context[self::MAX_DEPTH_HANDLER])) { @@ -251,7 +253,7 @@ protected function instantiateObject(array &$data, string $class, array &$contex */ protected function getAttributes(object $object, ?string $format, array $context): array { - $class = $this->objectClassResolver ? ($this->objectClassResolver)($object) : $object::class; + $class = ($this->objectClassResolver)($object); $key = $class.'-'.$context['cache_key']; if (isset($this->attributesCache[$key])) { @@ -321,7 +323,7 @@ public function denormalize(mixed $data, string $type, string $format = null, ar $reflectionClass = new \ReflectionClass($type); $object = $this->instantiateObject($normalizedData, $type, $context, $reflectionClass, $allowedAttributes, $format); - $resolvedClass = $this->objectClassResolver ? ($this->objectClassResolver)($object) : $object::class; + $resolvedClass = ($this->objectClassResolver)($object); $nestedAttributes = $this->getNestedAttributes($resolvedClass); $nestedData = []; diff --git a/src/Symfony/Component/Serializer/Normalizer/ConstraintViolationListNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/ConstraintViolationListNormalizer.php index 8816055a3c783..1fdf8420dbb9a 100644 --- a/src/Symfony/Component/Serializer/Normalizer/ConstraintViolationListNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/ConstraintViolationListNormalizer.php @@ -32,13 +32,10 @@ class ConstraintViolationListNormalizer implements NormalizerInterface, Cacheabl public const TYPE = 'type'; public const PAYLOAD_FIELDS = 'payload_fields'; - private $defaultContext; - private $nameConverter; - - public function __construct(array $defaultContext = [], NameConverterInterface $nameConverter = null) - { - $this->defaultContext = $defaultContext; - $this->nameConverter = $nameConverter; + public function __construct( + private readonly array $defaultContext = [], + private readonly ?NameConverterInterface $nameConverter = null, + ) { } public function getSupportedTypes(?string $format): array diff --git a/src/Symfony/Component/Serializer/Normalizer/DataUriNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/DataUriNormalizer.php index ff07c1f30fe53..1bcf81f9ba892 100644 --- a/src/Symfony/Component/Serializer/Normalizer/DataUriNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/DataUriNormalizer.php @@ -33,10 +33,7 @@ class DataUriNormalizer implements NormalizerInterface, DenormalizerInterface, C File::class => true, ]; - /** - * @var MimeTypeGuesserInterface|null - */ - private $mimeTypeGuesser; + private readonly ?MimeTypeGuesserInterface $mimeTypeGuesser; public function __construct(MimeTypeGuesserInterface $mimeTypeGuesser = null) { @@ -150,11 +147,7 @@ private function getMimeType(\SplFileInfo $object): string return $object->getMimeType(); } - if ($this->mimeTypeGuesser && $mimeType = $this->mimeTypeGuesser->guessMimeType($object->getPathname())) { - return $mimeType; - } - - return 'application/octet-stream'; + return $this->mimeTypeGuesser?->guessMimeType($object->getPathname()) ?: 'application/octet-stream'; } /** diff --git a/src/Symfony/Component/Serializer/Normalizer/DateIntervalNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/DateIntervalNormalizer.php index c9bc517727239..3cf5b887f9dbe 100644 --- a/src/Symfony/Component/Serializer/Normalizer/DateIntervalNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/DateIntervalNormalizer.php @@ -26,7 +26,7 @@ class DateIntervalNormalizer implements NormalizerInterface, DenormalizerInterfa { public const FORMAT_KEY = 'dateinterval_format'; - private $defaultContext = [ + private array $defaultContext = [ self::FORMAT_KEY => '%rP%yY%mM%dDT%hH%iM%sS', ]; diff --git a/src/Symfony/Component/Serializer/Normalizer/DateTimeNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/DateTimeNormalizer.php index b8d9ea2354573..aa7988d37ed55 100644 --- a/src/Symfony/Component/Serializer/Normalizer/DateTimeNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/DateTimeNormalizer.php @@ -28,7 +28,7 @@ class DateTimeNormalizer implements NormalizerInterface, DenormalizerInterface, public const FORMAT_KEY = 'datetime_format'; public const TIMEZONE_KEY = 'datetime_timezone'; - private $defaultContext = [ + private array $defaultContext = [ self::FORMAT_KEY => \DateTime::RFC3339, self::TIMEZONE_KEY => null, ]; diff --git a/src/Symfony/Component/Serializer/Normalizer/MimeMessageNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/MimeMessageNormalizer.php index 272495e68c012..4bf6f42c29460 100644 --- a/src/Symfony/Component/Serializer/Normalizer/MimeMessageNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/MimeMessageNormalizer.php @@ -32,13 +32,11 @@ final class MimeMessageNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface, CacheableSupportsMethodInterface { private NormalizerInterface&DenormalizerInterface $serializer; - private PropertyNormalizer $normalizer; private array $headerClassMap; private \ReflectionProperty $headersProperty; - public function __construct(PropertyNormalizer $normalizer) + public function __construct(private readonly PropertyNormalizer $normalizer) { - $this->normalizer = $normalizer; $this->headerClassMap = (new \ReflectionClassConstant(Headers::class, 'HEADER_CLASS_MAP'))->getValue(); $this->headersProperty = new \ReflectionProperty(Headers::class, 'headers'); } diff --git a/src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php index f4e50841e9c20..357c36426e50a 100644 --- a/src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php @@ -32,9 +32,10 @@ class ObjectNormalizer extends AbstractObjectNormalizer { protected $propertyAccessor; - private $discriminatorCache = []; + /** @var array */ + private array $discriminatorCache = []; - private $objectClassResolver; + private readonly \Closure $objectClassResolver; public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null, PropertyAccessorInterface $propertyAccessor = null, PropertyTypeExtractorInterface $propertyTypeExtractor = null, ClassDiscriminatorResolverInterface $classDiscriminatorResolver = null, callable $objectClassResolver = null, array $defaultContext = []) { @@ -46,7 +47,7 @@ public function __construct(ClassMetadataFactoryInterface $classMetadataFactory $this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor(); - $this->objectClassResolver = $objectClassResolver ?? fn ($class) => \is_object($class) ? $class::class : $class; + $this->objectClassResolver = ($objectClassResolver ?? static fn ($class) => \is_object($class) ? $class::class : $class)(...); } public function getSupportedTypes(?string $format): array diff --git a/src/Symfony/Component/Serializer/Normalizer/UidNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/UidNormalizer.php index 40559ec0b04e2..0dfeae7b83fd8 100644 --- a/src/Symfony/Component/Serializer/Normalizer/UidNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/UidNormalizer.php @@ -32,7 +32,7 @@ final class UidNormalizer implements NormalizerInterface, DenormalizerInterface, self::NORMALIZATION_FORMAT_RFC4122, ]; - private $defaultContext = [ + private array $defaultContext = [ self::NORMALIZATION_FORMAT_KEY => self::NORMALIZATION_FORMAT_CANONICAL, ]; diff --git a/src/Symfony/Component/Serializer/Normalizer/UnwrappingDenormalizer.php b/src/Symfony/Component/Serializer/Normalizer/UnwrappingDenormalizer.php index 31cc37be907b4..3708b1a08a78e 100644 --- a/src/Symfony/Component/Serializer/Normalizer/UnwrappingDenormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/UnwrappingDenormalizer.php @@ -26,7 +26,7 @@ final class UnwrappingDenormalizer implements DenormalizerInterface, SerializerA public const UNWRAP_PATH = 'unwrap_path'; - private $propertyAccessor; + private readonly PropertyAccessorInterface $propertyAccessor; public function __construct(PropertyAccessorInterface $propertyAccessor = null) { diff --git a/src/Symfony/Component/Serializer/Serializer.php b/src/Symfony/Component/Serializer/Serializer.php index e79ca7ba78412..c83da01fbe1f1 100644 --- a/src/Symfony/Component/Serializer/Serializer.php +++ b/src/Symfony/Component/Serializer/Serializer.php @@ -62,25 +62,33 @@ class Serializer implements SerializerInterface, ContextAwareNormalizerInterface ]; /** - * @var Encoder\ChainEncoder + * @var ChainEncoder */ protected $encoder; /** - * @var Encoder\ChainDecoder + * @var ChainDecoder */ protected $decoder; - private $normalizers = []; - private $denormalizerCache = []; - private $normalizerCache = []; + /** + * @var array>> + */ + private array $denormalizerCache = []; + + /** + * @var array>> + */ + private array $normalizerCache = []; /** * @param array $normalizers * @param array $encoders */ - public function __construct(array $normalizers = [], array $encoders = []) - { + public function __construct( + private array $normalizers = [], + array $encoders = [], + ) { foreach ($normalizers as $normalizer) { if ($normalizer instanceof SerializerAwareInterface) { $normalizer->setSerializer($this); @@ -98,7 +106,6 @@ public function __construct(array $normalizers = [], array $encoders = []) throw new InvalidArgumentException(sprintf('The class "%s" neither implements "%s" nor "%s".', get_debug_type($normalizer), NormalizerInterface::class, DenormalizerInterface::class)); } } - $this->normalizers = $normalizers; $decoders = []; $realEncoders = [];