8000 [VarExporter] Deprecate per-property lazy-initializers · symfony/symfony@28a78ea · GitHub
[go: up one dir, main page]

Skip to content

Commit 28a78ea

Browse files
[VarExporter] Deprecate per-property lazy-initializers
1 parent 6c91073 commit 28a78ea

File tree

6 files changed

+39
-30
lines changed

6 files changed

+39
-30
lines changed

UPGRADE-6.4.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Components
3838
* [Serializer](#Serializer)
3939
* [Templating](#Templating)
4040
* [Validator](#Validator)
41+
* [VarExporter](#VarExporter)
4142
* [Workflow](#Workflow)
4243

4344
BrowserKit
@@ -230,6 +231,11 @@ Validator
230231
* Deprecate `ValidatorBuilder::disableAnnotationMapping()`, use `ValidatorBuilder::disableAttributeMapping()` instead
231232
* Deprecate `AnnotationLoader`, use `AttributeLoader` instead
232233

234+
VarExporter
235+
-----------
236+
237+
* Deprecate per-property lazy-initializers
238+
233239
Workflow
234240
--------
235241

src/Symfony/Component/VarExporter/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
6.4
5+
---
6+
7+
* Deprecate per-property lazy-initializers
8+
49
6.2
510
---
611

src/Symfony/Component/VarExporter/LazyGhostTrait.php

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,20 @@ trait LazyGhostTrait
2323
/**
2424
* Creates a lazy-loading ghost instance.
2525
*
26-
* When the initializer is a closure, it should initialize all properties at
27-
* once and is given the instance to initialize as argument.
28-
*
29-
* When the initializer is an array of closures, it should be indexed by
30-
* properties and closures should accept 4 arguments: the instance to
31-
* initialize, the property to initialize, its write-scope, and its default
32-
* value. Each closure should return the value of the corresponding property.
33-
* The special "\0" key can be used to define a closure that returns all
34-
* properties at once when full-initialization is needed; it takes the
35-
* instance and its default properties as arguments.
36-
*
37-
* Properties should be indexed by their array-cast name, see
26+
* Skipped properties should be indexed by their array-cast identifier, see
3827
* https://php.net/manual/language.types.array#language.types.array.casting
3928
*
40-
* @param (\Closure(static):void
41-
* |array<string, \Closure(static, string, ?string, mixed):mixed>
42-
* |array{"\0": \Closure(static, array<string, mixed>):array<string, mixed>}) $initializer
43-
* @param array<string, true>|null $skippedProperties An array indexed by the properties to skip, aka the ones
44-
* that the initializer doesn't set when its a closure
29+
* @param (\Closure(static):void $initializer The closure should initialize the object it receives as argument
30+
* @param array<string, true>|null $skippedProperties An array indexed by the properties to skip, a.k.a. the ones
31+
* that the initializer doesn't initialize, if any
4532
* @param static|null $instance
4633
*/
4734
public static function createLazyGhost(\Closure|array $initializer, array $skippedProperties = null, object $instance = null): static
4835
{
36+
if (\is_array($initializer)) {
37+
trigger_deprecation('symfony/var-exporter', '6.4', 'Per-property lazy-initializers are deprecated and won\'t be supported anymore in 7.0, use an object initializer instead.');
38+
}
39+
4940
$onlyProperties = null === $skippedProperties && \is_array($initializer) ? $initializer : null;
5041

5142
if (self::class !== $class = $instance ? $instance::class : static::class) {

src/Symfony/Component/VarExporter/README.md

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -105,18 +105,6 @@ $foo = FooLazyGhost::createLazyGhost(initializer: function (Foo $instance): void
105105
// be called only when and if a *property* is accessed.
106106
```
107107

108-
You can also partially initialize the objects on a property-by-property basis by
109-
adding two arguments to the initializer:
110-
111-
```php
112-
$initializer = function (Foo $instance, string $propertyName, ?string $propertyScope): mixed {
113-
if (Foo::class === $propertyScope && 'bar' === $propertyName) {
114-
return 123;
115-
}
116-
// [...] Add more logic for the other properties
117-
};
118-
```
119-
120108
### `LazyProxyTrait`
121109

122110
Alternatively, `LazyProxyTrait` can be used to create virtual proxies:

src/Symfony/Component/VarExporter/Tests/LazyGhostTraitTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,9 @@ public function testFullInitialization()
186186
$this->assertSame(1, $counter);
187187
}
188188

189+
/**
190+
* @group legacy
191+
*/
189192
public function testPartialInitialization()
190193
{
191194
$counter = 0;
@@ -243,6 +246,9 @@ public function testPartialInitialization()
243246
$this->assertSame([123, 345, 456, 567, 234, 678], array_values($properties));
244247
}
245248

249+
/**
250+
* @group legacy
251+
*/
246252
public function testPartialInitializationWithReset()
247253
{
248254
$initializer = static fn (ChildTestClass $instance, string $property, ?string $scope, mixed $default) => 234;
@@ -275,6 +281,9 @@ public function testPartialInitializationWithReset()
275281
$this->assertSame(234, $instance->public);
276282
}
277283

284+
/**
285+
* @group legacy
286+
*/
278287
public function testPartialInitializationWithNastyPassByRef()
279288
{
280289
$instance = ChildTestClass::createLazyGhost(['public' => fn (ChildTestClass $instance, string &$property, ?string &$scope, mixed $default) => $property = $scope = 123]);
@@ -307,6 +316,9 @@ public function testReflectionPropertyGetValue()
307316
$this->assertSame(-3, $r->getValue($obj));
308317
}
309318

319+
/**
320+
* @group legacy
321+
*/
310322
public function testFullPartialInitialization()
311323
{
312324
$counter = 0;
@@ -335,6 +347,9 @@ public function testFullPartialInitialization()
335347
$this->assertSame(1000, $counter);
336348
}
337349

350+
/**
351+
* @group legacy
352+
*/
338353
public function testPartialInitializationFallback()
339354
{
340355
$counter = 0;
@@ -357,6 +372,9 @@ public function testPartialInitializationFallback()
357372
$this->assertSame(1000, $counter);
358373
}
359374

375+
/**
376+
* @group legacy
377+
*/
360378
public function testFullInitializationAfterPartialInitialization()
361379
{
362380
$counter = 0;

src/Symfony/Component/VarExporter/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
}
1717
],
1818
"require": {
19-
"php": ">=8.1"
19+
"php": ">=8.1",
20+
"symfony/deprecation-contracts": "^2.5|^3"
2021
},
2122
"require-dev": {
2223
"symfony/var-dumper": "^5.4|^6.0|^7.0"

0 commit comments

Comments
 (0)
0