8000 [HttpKernel] Fix datacollector caster for reference object property · symfony/symfony@457a3de · GitHub
[go: up one dir, main page]

Skip to content

Commit 457a3de

Browse files
ebuildynicolas-grekas
authored andcommitted
[HttpKernel] Fix datacollector caster for reference object property
1 parent 85d0165 commit 457a3de

File tree

4 files changed

+104
-2
lines changed

4 files changed

+104
-2
lines changed

src/Symfony/Component/HttpKernel/DataCollector/DataCollector.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,21 @@ protected function getCasters()
7070
$casters = [
7171
'*' => function ($v, array $a, Stub $s, $isNested) {
7272
if (!$v instanceof Stub) {
73+
$b = $a;
7374
foreach ($a as $k => $v) {
74-
if (\is_object($v) && !$v instanceof \DateTimeInterface && !$v instanceof Stub) {
75-
$a[$k] = new CutStub($v);
75+
if (!\is_object($v) || $v instanceof \DateTimeInterface || $v instanceof Stub) {
76+
continue;
77+
}
78+
79+
try {
80+
$a[$k] = $s = new CutStub($v);
81+
82+
if ($b[$k] === $s) {
83+
// we've hit a non-typed reference
84+
$a[$k] = $v;
85+
}
86+
} catch (\TypeError $e) {
87+
// we've hit a typed reference
7688
}
7789
}
7890
}

src/Symfony/Component/HttpKernel/Tests/DataCollector/DataCollectorTest.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use Symfony\Component\HttpFoundation\Request;
1616
use Symfony\Component\HttpFoundation\Response;
1717
use Symfony\Component\HttpKernel\Tests\Fixtures\DataCollector\CloneVarDataCollector;
18+
use Symfony\Component\HttpKernel\Tests\Fixtures\UsePropertyInDestruct;
19+
use Symfony\Component\HttpKernel\Tests\Fixtures\WithPublicObjectProperty;
1820
use Symfony\Component\VarDumper\Cloner\VarCloner;
1921

2022
class DataCollectorTest extends TestCase
@@ -35,4 +37,68 @@ public function testCloneVarExistingFilePath()
3537

3638
$this->assertSame($filePath, $c->getData()[0]);
3739
}
40+
41+
/**
42+
* @requires PHP 8
43+
*/
44+
public function testClassPublicObjectProperty()
45+
{
46+
$parent = new WithPublicObjectProperty();
47+
$child = new WithPublicObjectProperty();
48+
49+
$child->parent = $parent;
50+
51+
$c = new CloneVarDataCollector($child);
52+
$c->collect(new Request(), new Response());
53+
54+
$this->assertNotNull($c->getData()->parent);
55+
}
56+
57+
/**
58+
* @requires PHP 8
59+
*/
60+
public function testClassPublicObjectPropertyAsReference()
61+
{
62+
$parent = new WithPublicObjectProperty();
63+
$child = new WithPublicObjectProperty();
64+
65+
$child->parent = &$parent;
66+
67+
$c = new CloneVarDataCollector($child);
68+
$c->collect(new Request(), new Response());
69+
70+
$this->assertNotNull($c->getData()->parent);
71+
}
72+
73+
/**
74+
* @requires PHP 8
75+
*/
76+
public function testClassUsePropertyInDestruct()
77+
{
78+
$parent = new UsePropertyInDestruct();
79+
$child = new UsePropertyInDestruct();
80+
81+
$child->parent = $parent;
82+
83+
$c = new CloneVarDataCollector($child);
84+
$c->collect(new Request(), new Response());
85+
86+
$this->assertNotNull($c->getData()->parent);
87+
}
88+
89+
/**
90+
* @requires PHP 8
91+
*/
92+
public function testClassUsePropertyAsReferenceInDestruct()
93+
{
94+
$parent = new UsePropertyInDestruct();
95+
$child = new UsePropertyInDestruct();
96+
97+
$child->parent = &$parent;
98+
99+
$c = new CloneVarDataCollector($child);
100+
$c->collect(new Request(), new Response());
101+
102+
$this->assertNotNull($c->getData()->parent);
103+
}
38104
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Symfony\Component\HttpKernel\Tests\Fixtures;
4+
5+
class UsePropertyInDestruct
6+
{
7+
public string $name;
8+
public $parent = null;
9+
10+
public function __destruct()
11+
{
12+
if ($this->parent !== null) {
13+
$this->parent->name = '';
14+
}
15+
}
16+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Symfony\Component\HttpKernel\Tests\Fixtures;
4+
5+
class WithPublicObjectProperty
6+
{
7+
public ?WithPublicObjectProperty $parent = null;
8+
}

0 commit comments

Comments
 (0)
0