Skip to content

[HttpKernel] Fix datacollector caster for reference object property #54072

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 12, 2024
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
16 changes: 14 additions & 2 deletions src/Symfony/Component/HttpKernel/DataCollector/DataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,21 @@ protected function getCasters()
$casters = [
'*' => function ($v, array $a, Stub $s, $isNested) {
if (!$v instanceof Stub) {
$b = $a;
foreach ($a as $k => $v) {
if (\is_object($v) && !$v instanceof \DateTimeInterface && !$v instanceof Stub) {
$a[$k] = new CutStub($v);
if (!\is_object($v) || $v instanceof \DateTimeInterface || $v instanceof Stub) {
continue;
}

try {
$a[$k] = $s = new CutStub($v);

if ($b[$k] === $s) {
// we've hit a non-typed reference
$a[$k] = $v;
}
} catch (\TypeError $e) {
// we've hit a typed reference
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Tests\Fixtures\DataCollector\CloneVarDataCollector;
use Symfony\Component\HttpKernel\Tests\Fixtures\UsePropertyInDestruct;
use Symfony\Component\HttpKernel\Tests\Fixtures\WithPublicObjectProperty;
use Symfony\Component\VarDumper\Cloner\VarCloner;

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

$this->assertSame($filePath, $c->getData()[0]);
}

/**
* @requires PHP 8
*/
public function testClassPublicObjectProperty()
{
$parent = new WithPublicObjectProperty();
$child = new WithPublicObjectProperty();

$child->parent = $parent;

$c = new CloneVarDataCollector($child);
$c->collect(new Request(), new Response());

$this->assertNotNull($c->getData()->parent);
}

/**
* @requires PHP 8
*/
public function testClassPublicObjectPropertyAsReference()
{
$parent = new WithPublicObjectProperty();
$child = new WithPublicObjectProperty();

$child->parent = &$parent;

$c = new CloneVarDataCollector($child);
$c->collect(new Request(), new Response());

$this->assertNotNull($c->getData()->parent);
}

/**
* @requires PHP 8
*/
public function testClassUsePropertyInDestruct()
{
$parent = new UsePropertyInDestruct();
$child = new UsePropertyInDestruct();

$child->parent = $parent;

$c = new CloneVarDataCollector($child);
$c->collect(new Request(), new Response());

$this->assertNotNull($c->getData()->parent);
}

/**
* @requires PHP 8
*/
public function testClassUsePropertyAsReferenceInDestruct()
{
$parent = new UsePropertyInDestruct();
$child = new UsePropertyInDestruct();

$child->parent = &$parent;

$c = new CloneVarDataCollector($child);
$c->collect(new Request(), new Response());

$this->assertNotNull($c->getData()->parent);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Symfony\Component\HttpKernel\Tests\Fixtures;

class UsePropertyInDestruct
{
public string $name;
public $parent = null;

public function __destruct()
{
if ($this->parent !== null) {
$this->parent->name = '';
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Symfony\Component\HttpKernel\Tests\Fixtures;

class WithPublicObjectProperty
{
public ?WithPublicObjectProperty $parent = null;
}
Loading