10000 [Serializer] Add types to private and internal properties by derrabus · Pull Request #45721 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Serializer] Add typ 8000 es to private and internal properties #45721

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class MainConfiguration implements ConfigurationInterface
private array $userProviderFactories;

/**
* @param array<array-key, AuthenticatorFactoryInterface> $factories
* @param array<AuthenticatorFactoryInterface> $factories
*/
public function __construct(array $factories, array $userProviderFactories)
{
Expand Down Expand Up @@ -173,7 +173,7 @@ private function addAccessControlSection(ArrayNodeDefinition $rootNode): void
}

/**
* @param array<array-key, AuthenticatorFactoryInterface> $factories
* @param array<AuthenticatorFactoryInterface> $factories
*/
private function addFirewallsSection(ArrayNodeDefinition $rootNode, array $factories): void
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ final class AccessTokenFactory extends AbstractFactory implements StatelessAuthe
private const PRIORITY = -40;

/**
* @param array<array-key, TokenHandlerFactoryInterface> $tokenHandlerFactories
* @param array<TokenHandlerFactoryInterface> $tokenHandlerFactories
*/
public function __construct(private readonly array $tokenHandlerFactories)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class ChoiceGroupView implements \IteratorAggregate
/**
* Creates a new choice group view.
*
* @param array<array-key, ChoiceGroupView|ChoiceView> $choices the choice views in the group
* @param array<ChoiceGroupView|ChoiceView> $choices the choice views in the group
*/
public function __construct(string $label, array $choices = [])
{
Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/Serializer/Annotation/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Serializer/Annotation/Groups.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Groups
/**
* @var string[]
*/
private array $groups;
private readonly array $groups;

/**
* @param string|string[] $groups
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Serializer/Annotation/MaxDepth.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 9 additions & 4 deletions src/Symfony/Component/Serializer/Encoder/ChainDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,17 @@
*/
class ChainDecoder implements ContextAwareDecoderInterface
{
private array $decoders = [];
/**
* @var array<string, array-key>
*/
private array $decoderByFormat = [];

public function __construct(array $decoders = [])
{
$this->decoders = $decoders;
/**
* @param array<DecoderInterface> $decoders
*/
public function __construct(
private readonly array $decoders = []
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I've said this before, but strictly speaking this is a BC break: the $decoders property has no longer a default value (the default value is given to the parameter only).
This creates issues when overriding the constructor without calling the parent as the property becomes uninitialized: https://3v4l.org/DId0c

Is this something we consider too much of an edge case, or would this be a reason against transforming everything to CPP?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has already beaten us at least once so this is a valid concern to me.

) {
}

final public function decode(string $data, string $format, array $context = []): mixed
Expand Down
13 changes: 9 additions & 4 deletions src/Symfony/Component/Serializer/Encoder/ChainEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,17 @@
*/
class ChainEncoder implements ContextAwareEncoderInterface
{
private array $encoders = [];
/**
* @var array<string, array-key>
*/
private array $encoderByFormat = [];

public function __construct(array $encoders = [])
{
$this->encoders = $encoders;
/**
* @param array<EncoderInterface> $encoders
*/
public function __construct(
private readonly array $encoders = []
) {
}

final public function encode(mixed $data, string $format, array $context = []): string
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Serializer/Encoder/CsvEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 => '',
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Serializer/Encoder/JsonDecode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Serializer/Encoder/JsonEncode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Serializer/Encoder/XmlEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 => [],
Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/Serializer/Encoder/YamlEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
20 changes: 7 additions & 13 deletions src/Symfony/Component/Serializer/Mapping/AttributeMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string> $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;
Expand Down
Loading
0