Skip to content

Commit a1f4267

Browse files
bug #60421 [VarExporter] Fixed lazy-loading ghost objects generation with property hooks (cheack)
This PR was submitted for the 7.3 branch but it was merged into the 6.4 branch instead. Discussion ---------- [VarExporter] Fixed lazy-loading ghost objects generation with property hooks | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | License | MIT Fixed the bug introduced in #60288. If the type is boolean: ```php public bool $property = false { set { ... } } ``` an empty string is exported: ```php public bool $property = { ... } ``` which leads to `ParseError: syntax error, unexpected token "{"` error. Commits ------- 0c71a93 Fixed lazy-loading ghost objects generation with property hooks with default values.
2 parents e408d54 + 0c71a93 commit a1f4267

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

src/Symfony/Component/VarExporter/ProxyHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public static function generateLazyGhost(\ReflectionClass $class): string
8080
.($p->isProtected() ? 'protected' : 'public')
8181
.($p->isProtectedSet() ? ' protected(set)' : '')
8282
." {$type} \${$name}"
83-
.($p->hasDefaultValue() ? ' = '.$p->getDefaultValue() : '')
83+
.($p->hasDefaultValue() ? ' = '.VarExporter::export($p->getDefaultValue()) : '')
8484
." {\n";
8585

8686
foreach ($p->getHooks() as $hook => $method) {

src/Symfony/Component/VarExporter/Tests/Fixtures/LazyProxy/HookedWithDefaultValue.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,18 @@
44

55
class HookedWithDefaultValue
66
{
7-
public int $backedWithDefault = 321 {
8-
get => $this->backedWithDefault;
9-
set => $this->backedWithDefault = $value;
7+
public int $backedIntWithDefault = 321 {
8+
get => $this->backedIntWithDefault;
9+
set => $this->backedIntWithDefault = $value;
10+
}
11+
12+
public string $backedStringWithDefault = '321' {
13+
get => $this->backedStringWithDefault;
14+
set => $this->backedStringWithDefault = $value;
15+
}
16+
17+
public bool $backedBoolWithDefault = false {
18+
get => $this->backedBoolWithDefault;
19+
set => $this->backedBoolWithDefault = $value;
1020
}
1121
}

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -516,16 +516,22 @@ public function testPropertyHooksWithDefaultValue()
516516
$initialized = true;
517517
});
518518

519-
$this->assertSame(321, $object->backedWithDefault);
519+
$this->assertSame(321, $object->backedIntWithDefault);
520+
$this->assertSame('321', $object->backedStringWithDefault);
521+
$this->assertSame(false, $object->backedBoolWithDefault);
520522
$this->assertTrue($initialized);
521523

522524
$initialized = false;
523525
$object = $this->createLazyGhost(HookedWithDefaultValue::class, function ($instance) use (&$initialized) {
524526
$initialized = true;
525527
});
526-
$object->backedWithDefault = 654;
528+
$object->backedIntWithDefault = 654;
529+
$object->backedStringWithDefault = '654';
530+
$object->backedBoolWithDefault = true;
527531
$this->assertTrue($initialized);
528-
$this->assertSame(654, $object->backedWithDefault);
532+
$this->assertSame(654, $object->backedIntWithDefault);
533+
$this->assertSame('654', $object->backedStringWithDefault);
534+
$this->assertSame(true, $object->backedBoolWithDefault);
529535
}
530536

531537
/**

0 commit comments

Comments
 (0)