Skip to content

Commit feee7b7

Browse files
minor #61449 [Serializer][Validator] Make internal properties private (nicolas-grekas)
This PR was merged into the 7.4 branch. Discussion ---------- [Serializer][Validator] Make internal properties private | Q | A | ------------- | --- | Branch? | 7.4 | Bug fix? | no | New feature? | no | Deprecations? | no | Issues | - | License | MIT With the new `__serialize` implementations, we can finally replace those ``@internal`` properties by private ones, without breaking either child classes still implementing `__sleep` nor payloads created before. Commits ------- 6388b67 [Validator][Serializer] Make internal properties private
2 parents 489512f + 6388b67 commit feee7b7

File tree

6 files changed

+61
-194
lines changed

6 files changed

+61
-194
lines changed

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

Lines changed: 8 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -20,65 +20,22 @@
2020
*/
2121
class AttributeMetadata implements AttributeMetadataInterface
2222
{
23-
/**
24-
* @internal This property is public in order to reduce the size of the
25-
* class' serialized representation. Do not access it. Use
26-
* {@link getName()} instead.
27-
*/
28-
public string $name;
29-
30-
/**
31-
* @internal This property is public in order to reduce the size of the
32-
* class' serialized representation. Do not access it. Use
33-
* {@link getGroups()} instead.
34-
*/
35-
public array $groups = [];
36-
37-
/**
38-
* @internal This property is public in order to reduce the size of the
39-
* class' serialized representation. Do not access it. Use
40-
* {@link getMaxDepth()} instead.
41-
*/
42-
public ?int $maxDepth = null;
43-
44-
/**
45-
* @internal This property is public in order to reduce the size of the
46-
* class' serialized representation. Do not access it. Use
47-
* {@link getSerializedName()} instead.
48-
*/
49-
public ?string $serializedName = null;
50-
51-
/**
52-
* @internal This property is public in order to reduce the size of the
53-
* class' serialized representation. Do not access it. Use
54-
* {@link getSerializedPath()} instead.
55-
*/
56-
public ?PropertyPath $serializedPath = null;
57-
58-
/**
59-
* @internal This property is public in order to reduce the size of the
60-
* class' serialized representation. Do not access it. Use
61-
* {@link isIgnored()} instead.
62-
*/
63-
public bool $ignore = false;
23+
private string $name;
24+
private array $groups = [];
25+
private ?int $maxDepth = null;
26+
private ?string $serializedName = null;
27+
private ?PropertyPath $serializedPath = null;
28+
private bool $ignore = false;
6429

6530
/**
6631
* @var array[] Normalization contexts per group name ("*" applies to all groups)
67-
*
68-
* @internal This property is public in order to reduce the size of the
69-
* class' serialized representation. Do not access it. Use
70-
* {@link getNormalizationContexts()} instead.
7132
*/
72-
public array $normalizationContexts = [];
33+
private array $normalizationContexts = [];
7334

7435
/**
7536
* @var array[] Denormalization contexts per group name ("*" applies to all groups)
76-
*
77-
* @internal This property is public in order to reduce the size of the
78-
* class' serialized representation. Do not access it. Use
79-
* {@link getDenormalizationContexts()} instead.
8037
*/
81-
public array $denormalizationContexts = [];
38+
private array $denormalizationContexts = [];
8239

8340
public function __construct(string $name)
8441
{

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

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,34 +18,16 @@
1818
*/
1919
class ClassMetadata implements ClassMetadataInterface
2020
{
21-
/**
22-
* @internal This property is public in order to reduce the size of the
23-
* class' serialized representation. Do not access it. Use
24-
* {@link getName()} instead.
25-
*/
26-
public string $name;
21+
private string $name;
2722

2823
/**
2924
* @var AttributeMetadataInterface[]
30-
*
31-
* @internal This property is public in order to reduce the size of the
32-
* class' serialized representation. Do not access it. Use
33-
* {@link getAttributesMetadata()} instead.
3425
*/
35-
public array $attributesMetadata = [];
26+
private array $attributesMetadata = [];
3627

3728
private ?\ReflectionClass $reflClass = null;
29+
private ?ClassDiscriminatorMapping $classDiscriminatorMapping = null;
3830

39-
/**
40-
* @internal This property is public in order to reduce the size of the
41-
* class' serialized representation. Do not access it. Use
42-
* {@link getClassDiscriminatorMapping()} instead.
43-
*/
44-
public ?ClassDiscriminatorMapping $classDiscriminatorMapping = null;
45-
46-
/**
47-
* Constructs a metadata for the given class.
48-
*/
4931
public function __construct(string $class, ?ClassDiscriminatorMapping $classDiscriminatorMapping = null)
5032
{
5133
$this->name = $class;

src/Symfony/Component/Serializer/Mapping/Factory/CompiledClassMetadataFactory.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,17 @@ public function getMetadataFor(string|object $value): ClassMetadataInterface
5656
if (!isset($this->loadedClasses[$className])) {
5757
$classMetadata = new ClassMetadata($className);
5858
foreach ($this->compiledClassMetadata[$className][0] as $name => $compiledAttributesMetadata) {
59-
$classMetadata->attributesMetadata[$name] = $attributeMetadata = new AttributeMetadata($name);
60-
[$attributeMetadata->groups, $attributeMetadata->maxDepth, $attributeMetadata->serializedName] = $compiledAttributesMetadata;
59+
$classMetadata->addAttributeMetadata($attributeMetadata = new AttributeMetadata($name));
60+
foreach ($compiledAttributesMetadata[0] as $group) {
61+
$attributeMetadata->addGroup($group);
62+
}
63+
$attributeMetadata->setMaxDepth($compiledAttributesMetadata[1]);
64+
$attributeMetadata->setSerializedName($compiledAttributesMetadata[2]);
6165
}
62-
$classMetadata->classDiscriminatorMapping = $this->compiledClassMetadata[$className][1]
66+
$classMetadata->setClassDiscriminatorMapping($this->compiledClassMetadata[$className][1]
6367
? new ClassDiscriminatorMapping(...$this->compiledClassMetadata[$className][1])
6468
: null
65-
;
69+
);
6670

6771
$this->loadedClasses[$className] = $classMetadata;
6872
}

src/Symfony/Component/Validator/Mapping/ClassMetadata.php

Lines changed: 30 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -31,80 +31,45 @@
3131
*/
3232
class ClassMetadata extends GenericMetadata implements ClassMetadataInterface
3333
{
34-
/**
35-
* @internal This property is public in order to reduce the size of the
36-
* class' serialized representation. Do not access it. Use
37-
* {@link getClassName()} instead.
38-
*/
39-
public string $name;
40-
41-
/**
42-
* @internal This property is public in order to reduce the size of the
43-
* class' serialized representation. Do not access it. Use
44-
* {@link getDefaultGroup()} instead.
45-
*/
46-
public string $defaultGroup;
34+
private string $name;
35+
private string $defaultGroup;
4736

4837
/**
4938
* @var MemberMetadata[][]
50-
*
51-
* @internal This property is public in order to reduce the size of the
52-
* class' serialized representation. Do not access it. Use
53-
* {@link getPropertyMetadata()} instead.
5439
*/
55-
public array $members = [];
40+
private array $members = [];
5641

5742
/**
5843
* @var PropertyMetadata[]
59-
*
60-
* @internal This property is public in order to reduce the size of the
61-
* class' serialized representation. Do not access it. Use
62-
* {@link getPropertyMetadata()} instead.
6344
*/
64-
public array $properties = [];
45+
private array $properties = [];
6546

6647
/**
6748
* @var GetterMetadata[]
68-
*
69-
* @internal This property is public in order to reduce the size of the
70-
* class' serialized representation. Do not access it. Use
71-
* {@link getPropertyMetadata()} instead.
72-
*/
73-
public array $getters = [];
74-
75-
/**
76-
* @internal This property is public in order to reduce the size of the
77-
* class' serialized representation. Do not access it. Use
78-
* {@link getGroupSequence()} instead.
7949
*/
80-
public ?GroupSequence $groupSequence = null;
50+
private array $getters = [];
8151

82-
/**
83-
* @internal This property is public in order to reduce the size of the
84-
* class' serialized representation. Do not access it. Use
85-
* {@link isGroupSequenceProvider()} instead.
86-
*/
87-
public bool $groupSequenceProvider = false;
52+
private ?GroupSequence $groupSequence = null;
53+
private bool $groupSequenceProvider = false;
54+
private ?string $groupProvider = null;
8855

8956
/**
90-
* @internal This property is public in order to reduce the size of the
91-
* class' serialized representation. Do not access it. Use
92-
* {@link getGroupProvider()} instead.
57+
* The strategy for cascading objects.
58+
*
59+
* By default, objects are not cascaded.
60+
*
61+
* @var CascadingStrategy::*
9362
*/
94-
public ?string $groupProvider = null;
63+
private int $cascadingStrategy = CascadingStrategy::NONE;
9564

9665
/**
9766
* The strategy for traversing traversable objects.
9867
*
9968
* By default, only instances of {@link \Traversable} are traversed.
10069
*
10170
* @var TraversalStrategy::*
102-
*
103-
* @internal This property is public in order to reduce the size of the
104-
* class' serialized representation. Do not access it. Use
105-
* {@link getTraversalStrategy()} instead.
10671
*/
107-
public int $traversalStrategy = TraversalStrategy::IMPLICIT;
72+
private int $traversalStrategy = TraversalStrategy::IMPLICIT;
10873

10974
private \ReflectionClass $reflClass;
11075

@@ -123,6 +88,7 @@ public function __serialize(): array
12388
{
12489
if (self::class === (new \ReflectionMethod($this, '__sleep'))->class || self::class !== (new \ReflectionMethod($this, '__serialize'))->class) {
12590
return array_filter([
91+
'cascadingStrategy' => CascadingStrategy::NONE !== $this->cascadingStrategy ? $this->cascadingStrategy : null,
12692
'traversalStrategy' => TraversalStrategy::IMPLICIT !== $this->traversalStrategy ? $this->traversalStrategy : null,
12793
] + parent::__serialize() + [
12894
'getters' => $this->getters,
@@ -215,13 +181,7 @@ public function addConstraint(Constraint $constraint): static
215181
$this->checkConstraint($constraint);
216182

217183
if ($constraint instanceof Traverse) {
218-
if ($constraint->traverse) {
219-
// If traverse is true, traversal should be explicitly enabled
220-
$this->traversalStrategy = TraversalStrategy::TRAVERSE;
221-
} else {
222-
// If traverse is false, traversal should be explicitly disabled
223-
$this->traversalStrategy = TraversalStrategy::NONE;
224-
}
184+
$this->traversalStrategy = $constraint->traverse ? TraversalStrategy::TRAVERSE : TraversalStrategy::NONE;
225185

226186
// The constraint is not added
227187
return $this;
@@ -246,6 +206,14 @@ public function addConstraint(Constraint $constraint): static
246206

247207
$constraint->addImplicitGroupName($this->getDefaultGroup());
248208

209+
if ($constraint instanceof Valid && null === $constraint->groups) {
210+
$this->cascadingStrategy = CascadingStrategy::CASCADE;
211+
$this->traversalStrategy = $constraint->traverse ? TraversalStrategy::IMPLICIT : TraversalStrategy::NONE;
212+
213+
// The constraint is not added
214+
return $this;
215+
}
216+
249217
parent::addConstraint($constraint);
250218

251219
return $this;
@@ -498,6 +466,11 @@ public function getCascadingStrategy(): int
498466
return $this->cascadingStrategy;
499467
}
500468

469+
public function getTraversalStrategy(): int
470+
{
471+
return $this->traversalStrategy;
472+
}
473+
501474
private function addPropertyMetadata(PropertyMetadataInterface $metadata): void
502475
{
503476
$property = $metadata->getPropertyName();

src/Symfony/Component/Validator/Mapping/GenericMetadata.php

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -30,58 +30,38 @@ class GenericMetadata implements MetadataInterface
3030
{
3131
/**
3232
* @var Constraint[]
33-
*
34-
* @internal This property is public in order to reduce the size of the
35-
* class' serialized representation. Do not access it. Use
36-
* {@link getConstraints()} and {@link findConstraints()} instead.
3733
*/
38-
public array $constraints = [];
34+
private array $constraints = [];
3935

4036
/**
4137
* @var array<string, Constraint[]>
42-
*
43-
* @internal This property is public in order to reduce the size of the
44-
* class' serialized representation. Do not access it. Use
45-
* {@link findConstraints()} instead.
4638
*/
47-
public array $constraintsByGroup = [];
39+
private array $constraintsByGroup = [];
4840

4941
/**
5042
* The strategy for cascading objects.
5143
*
5244
* By default, objects are not cascaded.
5345
*
5446
* @var CascadingStrategy::*
55-
*
56-
* @internal This property is public in order to reduce the size of the
57-
* class' serialized representation. Do not access it. Use
58-
* {@link getCascadingStrategy()} instead.
5947
*/
60-
public int $cascadingStrategy = CascadingStrategy::NONE;
48+
private int $cascadingStrategy = CascadingStrategy::NONE;
6149

6250
/**
6351
* The strategy for traversing traversable objects.
6452
*
6553
* By default, traversable objects are not traversed.
6654
*
6755
* @var TraversalStrategy::*
68-
*
69-
* @internal This property is public in order to reduce the size of the
70-
* class' serialized representation. Do not access it. Use
71-
* {@link getTraversalStrategy()} instead.
7256
*/
73-
public int $traversalStrategy = TraversalStrategy::NONE;
57+
private int $traversalStrategy = TraversalStrategy::NONE;
7458

7559
/**
7660
* Is auto-mapping enabled?
7761
*
7862
* @var AutoMappingStrategy::*
79-
*
80-
* @internal This property is public in order to reduce the size of the
81-
* class' serialized representation. Do not access it. Use
82-
* {@link getAutoMappingStrategy()} instead.
8363
*/
84-
public int $autoMappingStrategy = AutoMappingStrategy::NONE;
64+
private int $autoMappingStrategy = AutoMappingStrategy::NONE;
8565

8666
public function __serialize(): array
8767
{
@@ -127,9 +107,6 @@ public function __sleep(): array
127107
];
128108
}
129109

130-
/**
131-
* Clones this object.
132-
*/
133110
public function __clone()
134111
{
135112
$constraints = $this->constraints;
@@ -166,13 +143,9 @@ public function addConstraint(Constraint $constraint): static
166143

167144
if ($constraint instanceof Valid && null === $constraint->groups) {
168145
$this->cascadingStrategy = CascadingStrategy::CASCADE;
146+
$this->traversalStrategy = $constraint->traverse ? TraversalStrategy::IMPLICIT : TraversalStrategy::NONE;
169147

170-
if ($constraint->traverse) {
171-
$this->traversalStrategy = TraversalStrategy::IMPLICIT;
172-
} else {
173-
$this->traversalStrategy = TraversalStrategy::NONE;
174-
}
175-
148+
// The constraint is not added
176149
return $this;
177150
}
178151

@@ -197,9 +170,9 @@ public function addConstraint(Constraint $constraint): static
197170
}
198171

199172
/**
200-
* Adds an list of constraints.
173+
* Adds a list of constraints.
201174
*
202-
* @param Constraint[] $constraints The constraints to add
175+
* @param Constraint[] $constraints
203176
*
204177
* @return $this
205178
*/

0 commit comments

Comments
 (0)