8000 minor #45721 [Serializer] Add types to private and internal propertie… · symfony/symfony@f709c6e · GitHub
[go: up one dir, main page]

Skip to content

Commit f709c6e

Browse files
minor #45721 [Serializer] Add types to private and internal properties (derrabus)
This PR was merged into the 6.3 branch. Discussion ---------- [Serializer] Add types to private and internal properties | Q | A | ------------- | --- | Branch? | 6.1 | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | N/A | License | MIT | Doc PR | N/A This PR adds types to private and ``@internal`` properties of the serializer component. This time, I've also applied CPP and `readonly` flags where possible. If this makes the review too hard or there are other reasons not to do that, please tell me and I'll skip it in future PRs. Commits ------- 7ff739a [Serializer] Add types to private and internal properties
2 parents bfd34b3 + 7ff739a commit f709c6e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+173
-206
lines changed

src/Symfony/Bundle/SecurityBundle/DependencyInjection/MainConfiguration.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class MainConfiguration implements ConfigurationInterface
4040
private array $userProviderFactories;
4141

4242
/**
43-
* @param array<array-key, AuthenticatorFactoryInterface> $factories
43+
* @param array<AuthenticatorFactoryInterface> $factories
4444
*/
4545
public function __construct(array $factories, array $userProviderFactories)
4646
{
@@ -173,7 +173,7 @@ private function addAccessControlSection(ArrayNodeDefinition $rootNode): void
173173
}
174174

175175
/**
176-
* @param array<array-key, AuthenticatorFactoryInterface> $factories
176+
* @param array<AuthenticatorFactoryInterface> $factories
177177
*/
178178
private function addFirewallsSection(ArrayNodeDefinition $rootNode, array $factories): void
179179
{

src/Symfony/Bundle/SecurityBundle/DependencyInjection/Security/Factory/AccessTokenFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ final class AccessTokenFactory extends AbstractFactory implements StatelessAuthe
3030
private const PRIORITY = -40;
3131

3232
/**
33-
* @param array<array-key, TokenHandlerFactoryInterface> $tokenHandlerFactories
33+
* @param array<TokenHandlerFactoryInterface> $tokenHandlerFactories
3434
*/
3535
public function __construct(private readonly array $tokenHandlerFactories)
3636
{

src/Symfony/Component/Form/ChoiceList/View/ChoiceGroupView.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class ChoiceGroupView implements \IteratorAggregate
2626
/**
2727
* Creates a new choice group view.
2828
*
29-
* @param array<array-key, ChoiceGroupView|ChoiceView> $choices the choice views in the group
29+
* @param array<ChoiceGroupView|ChoiceView> $choices the choice views in the group
3030
*/
3131
public function __construct(string $label, array $choices = [])
3232
{

src/Symfony/Component/Serializer/Annotation/Context.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ class Context
3333
* @throws InvalidArgumentException
3434
*/
3535
public function __construct(
36-
private array $context = [],
37-
private array $normalizationContext = [],
38-
private array $denormalizationContext = [],
36+
private readonly array $context = [],
37+
private readonly array $normalizationContext = [],
38+
private readonly array $denormalizationContext = [],
3939
string|array $groups = [],
4040
) {
4141
if (!$context && !$normalizationContext && !$denormalizationContext) {

src/Symfony/Component/Serializer/Annotation/DiscriminatorMap.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
class DiscriminatorMap
2727
{
2828
public function __construct(
29-
private string $typeProperty,
30-
private array $mapping,
29+
private readonly string $typeProperty,
30+
private readonly array $mapping,
3131
) {
3232
if (empty($typeProperty)) {
3333
throw new InvalidArgumentException(sprintf('Parameter "typeProperty" of annotation "%s" cannot be empty.', static::class));

src/Symfony/Component/Serializer/Annotation/Groups.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Groups
2828
/**
2929
* @var string[]
3030
*/
31-
private array $groups;
31+
private readonly array $groups;
3232

3333
/**
3434
* @param string|string[] $groups

src/Symfony/Component/Serializer/Annotation/MaxDepth.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::TARGET_PROPERTY)]
2626
class MaxDepth
2727
{
28-
public function __construct(private int $maxDepth)
28+
public function __construct(private readonly int $maxDepth)
2929
{
3030
if ($maxDepth <= 0) {
3131
throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" must be a positive integer.', static::class));

src/Symfony/Component/Serializer/Annotation/SerializedName.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::TARGET_PROPERTY)]
2626
final class SerializedName
2727
{
28-
public function __construct(private string $serializedName)
28+
public function __construct(private readonly string $serializedName)
2929
{
3030
if ('' === $serializedName) {
3131
throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" must be a non-empty string.', self::class));

src/Symfony/Component/Serializer/CacheWarmer/CompiledClassMetadataCacheWarmer.php

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,12 @@
2121
*/
2222
final class CompiledClassMetadataCacheWarmer implements CacheWarmerInterface
2323
{
24-
private $classesToCompile;
25-
26-
private $classMetadataFactory;
27-
28-
private $classMetadataFactoryCompiler;
29-
30-
private $filesystem;
31-
32-
public function __construct(array $classesToCompile, ClassMetadataFactoryInterface $classMetadataFactory, ClassMetadataFactoryCompiler $classMetadataFactoryCompiler, Filesystem $filesystem)
33-
{
34-
$this->classesToCompile = $classesToCompile;
35-
$this->classMetadataFactory = $classMetadataFactory;
36-
$this->classMetadataFactoryCompiler = $classMetadataFactoryCompiler;
37-
$this->filesystem = $filesystem;
24+
public function __construct(
25+
private readonly array $classesToCompile,
26+
private readonly ClassMetadataFactoryInterface $classMetadataFactory,
27+
private readonly ClassMetadataFactoryCompiler $classMetadataFactoryCompiler,
28+
private readonly Filesystem $filesystem,
29+
) {
3830
}
3931

4032
public function warmUp(string $cacheDir): array

src/Symfony/Component/Serializer/Encoder/ChainDecoder.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,17 @@
2424
*/
2525
class ChainDecoder implements ContextAwareDecoderInterface
2626
{
27-
private array $decoders = [];
27+
/**
28+
* @var array<string, array-key>
29+
*/
2830
private array $decoderByFormat = [];
2931

30-
public function __construct(array $decoders = [])
31-
{
32-
$this->decoders = $decoders;
32+
/**
33+
* @param array<DecoderInterface> $decoders
34+
*/
35+
public function __construct(
36+
private readonly array $decoders = []
37+
) {
3338
}
3439

3540
final public function decode(string $data, string $format, array $context = []): mixed

src/Symfony/Component/Serializer/Encoder/ChainEncoder.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,17 @@
2525
*/
2626
class ChainEncoder implements ContextAwareEncoderInterface
2727
{
28-
private array $encoders = [];
28+
/**
29+
* @var array<string, array-key>
30+
*/
2931
private array $encoderByFormat = [];
3032

31-
public function __construct(array $encoders = [])
32-
{
33-
$this->encoders = $encoders;
33+
/**
34+
* @param array<EncoderInterface> $encoders
35+
*/
36+
public function __construct(
37+
private readonly array $encoders = []
38+
) {
3439
}
3540

3641
final public function encode(mixed $data, string $format, array $context = []): string

src/Symfony/Component/Serializer/Encoder/CsvEncoder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class CsvEncoder implements EncoderInterface, DecoderInterface
3838

3939
private const FORMULAS_START_CHARACTERS = ['=', '-', '+', '@', "\t", "\r"];
4040

41-
private $defaultContext = [
41+
private array $defaultContext = [
4242
self::DELIMITER_KEY => ',',
4343
self::ENCLOSURE_KEY => '"',
4444
self::ESCAPE_CHAR_KEY => '',

src/Symfony/Component/Serializer/Encoder/JsonDecode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class JsonDecode implements DecoderInterface
3434
*/
3535
public const RECURSION_DEPTH = 'json_decode_recursion_depth';
3636

37-
private $defaultContext = [
37+
private array $defaultContext = [
3838
self::ASSOCIATIVE => false,
3939
self::OPTIONS => 0,
4040
self::RECURSION_DEPTH => 512,

src/Symfony/Component/Serializer/Encoder/JsonEncode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class JsonEncode implements EncoderInterface
2525
*/
2626
public const OPTIONS = 'json_encode_options';
2727

28-
private $defaultContext = [
28+
private array $defaultContext = [
2929
self::OPTIONS => \JSON_PRESERVE_ZERO_FRACTION,
3030
];
3131

src/Symfony/Component/Serializer/Encoder/XmlEncoder.php

Lines changed: 1 addition & 1 deletion
CDA2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class XmlEncoder implements EncoderInterface, DecoderInterface, NormalizationAwa
5959
public const TYPE_CAST_ATTRIBUTES = 'xml_type_cast_attributes';
6060
public const VERSION = 'xml_version';
6161

62-
private $defaultContext = [
62+
private array $defaultContext = [
6363
self::AS_COLLECTION => false,
6464
self::DECODER_IGNORED_NODE_TYPES => [\XML_PI_NODE, \XML_COMMENT_NODE],
6565
self::ENCODER_IGNORED_NODE_TYPES => [],

src/Symfony/Component/Serializer/Encoder/YamlEncoder.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ class YamlEncoder implements EncoderInterface, DecoderInterface
4242
public const YAML_INDENT = 'yaml_indent';
4343
public const YAML_FLAGS = 'yaml_flags';
4444

45-
private $dumper;
46-
private $parser;
47-
private $defaultContext = [
45+
private readonly Dumper $dumper;
46+
private readonly Parser $parser;
47+
private array $defaultContext = [
4848
self::YAML_INLINE => 0,
4949
self::YAML_INDENT => 0,
5050
self::YAML_FLAGS => 0,

src/Symfony/Component/Serializer/Exception/ExtraAttributesException.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,12 @@
1818
*/
1919
class ExtraAttributesException extends RuntimeException
2020
{
21-
private $extraAttributes;
22-
23-
public function __construct(array $extraAttributes, \Throwable $previous = null)
24-
{
21+
public function __construct(
22+
private readonly array $extraAttributes,
23+
\Throwable $previous = null,
24+
) {
2525
$msg = sprintf('Extra attributes are not allowed ("%s" %s unknown).', implode('", "', $extraAttributes), \count($extraAttributes) > 1 ? 'are' : 'is');
2626

27-
$this->extraAttributes = $extraAttributes;
28-
2927
parent::__construct($msg, 0, $previous);
3028
}
3129

src/Symfony/Component/Serializer/Exception/NotNormalizableValueException.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,18 @@
1616
*/
1717
class NotNormalizableValueException extends UnexpectedValueException
1818
{
19-
private $currentType;
20-
private $expectedTypes;
21-
private $path;
22-
private $useMessageForUser = false;
19+
private ?string $currentType = null;
20+
private ?array $expectedTypes = null;
21+
private ?string $path = null;
22+
private bool $useMessageForUser = false;
2323

2424
/**
25-
* @param bool $useMessageForUser If the message passed to this exception is something that can be shown
26-
* safely to your user. In other words, avoid catching other exceptions and
27-
* passing their message directly to this class.
25+
* @param string[] $expectedTypes
26+
* @param bool $useMessageForUser If the message passed to this exception is something that can be shown
27+
* safely to your user. In other words, avoid catching other exceptions and
28+
* passing their message directly to this class.
2829
*/
29-
public static function createForUnexpectedDataType(string $message, $data, array $expectedTypes, string $path = null, bool $useMessageForUser = false, int $code = 0, \Throwable $previous = null): self
30+
public static function createForUnexpectedDataType(string $message, mixed $data, array $expectedTypes, string $path = null, bool $useMessageForUser = false, int $code = 0, \Throwable $previous = null): self
3031
{
3132
$self = new self($message, $code, $previous);
3233

src/Symfony/Component/Serializer/Extractor/ObjectPropertyListExtractor.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@
1818
*/
1919
final class ObjectPropertyListExtractor implements ObjectPropertyListExtractorInterface
2020
{
21-
private $propertyListExtractor;
22-
private $objectClassResolver;
21+
private PropertyListExtractorInterface $propertyListExtractor;
22+
private \Closure $objectClassResolver;
2323

2424
public function __construct(PropertyListExtractorInterface $propertyListExtractor, callable $objectClassResolver = null)
2525
{
2626
$this->propertyListExtractor = $propertyListExtractor;
27-
$this->objectClassResolver = $objectClassResolver;
27+
$this->objectClassResolver = ($objectClassResolver ?? 'get_class')(...);
2828
}
2929

3030
public function getProperties(object $object, array $context = []): ?array
3131
{
32-
$class = $this->objectClassResolver ? ($this->objectClassResolver)($object) : $object::class;
32+
$class = ($this->objectClassResolver)($object);
3333

3434
return $this->propertyListExtractor->getProperties($class, $context);
3535
}

src/Symfony/Component/Serializer/Mapping/AttributeMetadata.php

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,32 +23,28 @@ class AttributeMetadata implements AttributeMetadataInterface
2323
* class' serialized representation. Do not access it. Use
2424
* {@link getName()} instead.
2525
*/
26-
public $name;
26+
public string $name;
2727

2828
/**
2929
* @internal This property is public in order to reduce the size of the
3030
* class' serialized representation. Do not access it. Use
3131
* {@link getGroups()} instead.
3232
*/
33-
public $groups = [];
33+
public array $groups = [];
3434

3535
/**
36-
* @var int|null
37-
*
3836
* @internal This property is public in order to reduce the size of the
3937
* class' serialized representation. Do not access it. Use
4038
* {@link getMaxDepth()} instead.
4139
*/
42-
public $maxDepth;
40+
public ?int $maxDepth = null;
4341

4442
/**
45-
* @var string|null
46-
*
4743
* @internal This property is public in order to reduce the size of the
4844
* class' serialized representation. Do not access it. Use
4945
* {@link getSerializedName()} instead.
5046
*/
51-
public $serializedName;
47+
public ?string $serializedName = null;
5248

5349
/**
5450
* @internal This property is public in order to reduce the size of the
@@ -58,13 +54,11 @@ class AttributeMetadata implements AttributeMetadataInterface
5854
public ?PropertyPath $serializedPath = null;
5955

6056
/**
61-
* @var bool
62-
*
6357
* @internal This property is public in order to reduce the size of the
6458
* class' serialized representation. Do not access it. Use
6559
* {@link isIgnored()} instead.
6660
*/
67-
public $ignore = false;
61+
public bool $ignore = false;
6862

6963
/**
7064
* @var array[] Normalization contexts per group name ("*" applies to all groups)
@@ -73,7 +67,7 @@ class AttributeMetadata implements AttributeMetadataInterface
7367
* class' serialized representation. Do not access it. Use
7468
* {@link getNormalizationContexts()} instead.
7569
*/
76-
public $normalizationContexts = [];
70+
public array $normalizationContexts = [];
7771

7872
/**
7973
* @var array[] Denormalization contexts per group name ("*" applies to all groups)
@@ -82,7 +76,7 @@ class AttributeMetadata implements AttributeMetadataInterface
8276
* class' serialized representation. Do not access it. Use
8377
* {@link getDenormalizationContexts()} instead.
8478
*/
85-
public $denormalizationContexts = [];
79+
public array $denormalizationContexts = [];
8680

8781
public function __construct(string $name)
8882
{

src/Symfony/Component/Serializer/Mapping/ClassDiscriminatorFromClassMetadata.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,11 @@
1818
*/
1919
class ClassDiscriminatorFromClassMetadata implements ClassDiscriminatorResolverInterface
2020
{
21-
/**
22-
* @var ClassMetadataFactoryInterface
23-
*/
24-
private $classMetadataFactory;
25-
private $mappingForMappedObjectCache = [];
21+
private array $mappingForMappedObjectCache = [];
2622

27-
public function __construct(ClassMetadataFactoryInterface $classMetadataFactory)
28-
{
29-
$this->classMetadataFactory = $classMetadataFactory;
23+
public function __construct(
24+
private readonly ClassMetadataFactoryInterface $classMetadataFactory,
25+
) {
3026
}
3127

3228
public function getMappingForClass(string $class): ?ClassDiscriminatorMapping

src/Symfony/Component/Serializer/Mapping/ClassDiscriminatorMapping.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,13 @@
1616
*/
1717
class ClassDiscriminatorMapping
1818
{
19-
private $typeProperty;
20-
private $typesMapping;
21-
22-
public function __construct(string $typeProperty, array $typesMapping = [])
23-
{
24-
$this->typeProperty = $typeProperty;
25-
$this->typesMapping = $typesMapping;
26-
19+
/**
20+
* @param array<string, string> $typesMapping
21+
*/
22+
public function __construct(
23+
private readonly string $typeProperty,
24+
private array $typesMapping = [],
25+
) {
2726
uasort($this->typesMapping, static function (string $a, string $b): int {
2827
if (is_a($a, $b, true)) {
2928
return -1;

0 commit comments

Comments
 (0)
0