Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[VarDumper] Add support for virtual properties #57833

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
Aug 22, 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
1 change: 1 addition & 0 deletions src/Symfony/Component/VarDumper/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

* Add support for `FORCE_COLOR` environment variable
* Add support for virtual properties

7.1
---
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/VarDumper/Caster/Caster.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@
$p->isPublic() => $p->name,
$p->isProtected() => self::PREFIX_PROTECTED.$p->name,
default => "\0".$className."\0".$p->name,
}] = new UninitializedStub($p);
}] = \PHP_VERSION_ID >= 80400 && $p->isVirtual() ? new VirtualStub($p) : new UninitializedStub($p);

Check failure on line 193 in src/Symfony/Component/VarDumper/Caster/Caster.php

View workflow job for this annotation

GitHub Actions / Psalm

UndefinedMethod

src/Symfony/Component/VarDumper/Caster/Caster.php:193:50: UndefinedMethod: Method ReflectionProperty::isVirtual does not exist (see https://psalm.dev/022)

Check failure on line 193 in src/Symfony/Component/VarDumper/Caster/Caster.php

View workflow job for this annotation

GitHub Actions / Psalm

UndefinedMethod

src/Symfony/Component/VarDumper/Caster/Caster.php:193:50: UndefinedMethod: Method ReflectionProperty::isVirtual does not exist (see https://psalm.dev/022)
}

return $classProperties;
Expand Down
21 changes: 21 additions & 0 deletions src/Symfony/Component/VarDumper/Caster/VirtualStub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\VarDumper\Caster;

class VirtualStub extends ConstStub
{
public function __construct(\ReflectionProperty $property)
{
parent::__construct('~'.($property->hasType() ? ' '.$property->getType() : ''), 'Virtual property');
$this->attr['virtual'] = true;
}
}
17 changes: 12 additions & 5 deletions src/Symfony/Component/VarDumper/Dumper/CliDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ class CliDumper extends AbstractDumper
'default' => '0;38;5;208',
'num' => '1;38;5;38',
'const' => '1;38;5;208',
'virtual' => '3',
'str' => '1;38;5;113',
'note' => '38;5;38',
'ref' => '38;5;247',
'public' => '',
'protected' => '',
'private' => '',
'public' => '39',
'protected' => '39',
'private' => '39',
'meta' => '38;5;170',
'key' => '38;5;113',
'index' => '38;5;38',
Expand Down Expand Up @@ -347,7 +348,10 @@ protected function dumpKey(Cursor $cursor): void
if ($cursor->hashKeyIsBinary) {
$key = $this->utf8Encode($key);
}
$attr = ['binary' => $cursor->hashKeyIsBinary];
$attr = [
'binary' => $cursor->hashKeyIsBinary,
'virtual' => $cursor->attr['virtual'] ?? false,
];
$bin = $cursor->hashKeyIsBinary ? 'b' : '';
$style = 'key';
switch ($cursor->hashType) {
Expand All @@ -371,7 +375,7 @@ protected function dumpKey(Cursor $cursor): void
// no break
case Cursor::HASH_OBJECT:
if (!isset($key[0]) || "\0" !== $key[0]) {
$this->line .= '+'.$bin.$this->style('public', $key).': ';
$this->line .= '+'.$bin.$this->style('public', $key, $attr).': ';
} elseif (0 < strpos($key, "\0", 1)) {
$key = explode("\0", substr($key, 1), 2);

Expand Down Expand Up @@ -506,6 +510,9 @@ protected function style(string $style, string $value, array $attr = []): string
if ('label' === $style && '' !== $value) {
$value .= ' ';
}
if ($this->colors && ($attr['virtual'] ?? false)) {
$value = "\033[{$this->styles['virtual']}m".$value;
}

return $value;
}
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/VarDumper/Dumper/HtmlDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class HtmlDumper extends CliDumper
'default' => 'background-color:#18171B; color:#FF8400; line-height:1.2em; font:12px Menlo, Monaco, Consolas, monospace; word-wrap: break-word; white-space: pre-wrap; position:relative; z-index:99999; word-break: break-all',
'num' => 'font-weight:bold; color:#1299DA',
'const' => 'font-weight:bold',
'virtual' => 'font-style:italic',
'str' => 'font-weight:bold; color:#56DB3A',
'note' => 'color:#1299DA',
'ref' => 'color:#A0A0A0',
Expand All @@ -45,6 +46,7 @@ class HtmlDumper extends CliDumper
'default' => 'background:none; color:#CC7832; line-height:1.2em; font:12px Menlo, Monaco, Consolas, monospace; word-wrap: break-word; white-space: pre-wrap; position:relative; z-index:99999; word-break: break-all',
'num' => 'font-weight:bold; color:#1299DA',
'const' => 'font-weight:bold',
'virtual' => 'font-style:italic',
'str' => 'font-weight:bold; color:#629755;',
'note' => 'color:#6897BB',
'ref' => 'color:#6E6E6E',
Expand Down Expand Up @@ -921,6 +923,9 @@ protected function style(string $style, string $value, array $attr = []): string
if ('label' === $style) {
$v .= ' ';
}
if ($attr['virtual'] ?? false) {
$v = '<span class=sf-dump-virtual>'.$v.'</span>';
}

return $v;
}
Expand Down
38 changes: 37 additions & 1 deletion src/Symfony/Component/VarDumper/Tests/Caster/StubCasterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
use Symfony\Component\VarDumper\Caster\ClassStub;
use Symfony\Component\VarDumper\Caster\LinkStub;
use Symfony\Component\VarDumper\Caster\ScalarStub;
use Symfony\Component\VarDumper\Caster\VirtualStub;
use Symfony\Component\VarDumper\Cloner\VarCloner;
use Symfony\Component\VarDumper\Dumper\HtmlDumper;
use Symfony\Component\VarDumper\Test\VarDumperTestTrait;
use Symfony\Component\VarDumper\Tests\Fixtures\FooInterface;
use Symfony\Component\VarDumper\Tests\Fixtures\VirtualProperty;

class StubCasterTest extends TestCase
{
Expand Down Expand Up @@ -101,6 +103,40 @@ public function testEmptyStub()
$this->assertDumpMatchesFormat($expectedDump, $args);
}

/**
* @requires PHP 8.4
*/
public function testVirtualPropertyStub()
{
$class = new \ReflectionClass(VirtualProperty::class);
$args = [new VirtualStub($class->getProperty('fullName'))];

$expectedDump = <<<'EODUMP'
array:1 [
0 => ~ string
]
EODUMP;

$this->assertDumpMatchesFormat($expectedDump, $args);
}

/**
* @requires PHP 8.4
*/
public function testVirtualPropertyWithoutTypeStub()
{
$class = new \ReflectionClass(VirtualProperty::class);
$args = [new VirtualStub($class->getProperty('noType'))];

$expectedDump = <<<'EODUMP'
array:1 [
0 => ~
]
EODUMP;

$this->assertDumpMatchesFormat($expectedDump, $args);
}

public function testLinkStub()
{
$var = [new LinkStub(__CLASS__, 0, __FILE__)];
Expand Down Expand Up @@ -217,7 +253,7 @@ public function testClassStubWithAnonymousClass()

$expectedDump = <<<'EODUMP'
<foo></foo><bar><span class=sf-dump-note>array:1</span> [<samp data-depth=1 class=sf-dump-expanded>
<span class=sf-dump-index>0</span> => "<a href="%sStubCasterTest.php:209" rel="noopener noreferrer"><span class=sf-dump-str title="19 characters">Exception@anonymous</span></a>"
<span class=sf-dump-index>0</span> => "<a href="%sStubCasterTest.php:245" rel="noopener noreferrer"><span class=sf-dump-str title="19 characters">Exception@anonymous</span></a>"
</samp>]
</bar>
EODUMP;
Expand Down
16 changes: 16 additions & 0 deletions src/Symfony/Component/VarDumper/Tests/Dumper/CliDumperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Symfony\Component\VarDumper\Dumper\AbstractDumper;
use Symfony\Component\VarDumper\Dumper\CliDumper;
use Symfony\Component\VarDumper\Test\VarDumperTestTrait;
use Symfony\Component\VarDumper\Tests\Fixtures\VirtualProperty;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;

Expand Down Expand Up @@ -304,6 +305,21 @@ public function testFlags()
putenv('DUMP_STRING_LENGTH=');
}

/**
* @requires PHP 8.4
*/
public function testVirtualProperties()
{
$this->assertDumpEquals(<<<EODUMP
Symfony\Component\VarDumper\Tests\Fixtures\VirtualProperty {
+firstName: "John"
+lastName: "Doe"
+fullName: ~ string
-noType: ~
}
EODUMP, new VirtualProperty());
}

public function testThrowingCaster()
{
$out = fopen('php://memory', 'r+');
Expand Down
25 changes: 25 additions & 0 deletions src/Symfony/Component/VarDumper/Tests/Dumper/HtmlDumperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\VarDumper\Caster\ImgStub;
use Symfony\Component\VarDumper\Cloner\VarCloner;
use Symfony\Component\VarDumper\Dumper\HtmlDumper;
use Symfony\Component\VarDumper\Tests\Fixtures\VirtualProperty;

/**
* @author Nicolas Grekas <[email protected]>
Expand Down Expand Up @@ -117,6 +118,30 @@ public function testGet()
);
}

/**
* @requires PHP 8.4
*/
public function testVirtualProperties()
{
$dumper = new HtmlDumper('php://output');
$dumper->setDumpHeader('<foo></foo>');
$dumper->setDumpBoundaries('<bar>', '</bar>');
$cloner = new VarCloner();

$data = $cloner->cloneVar(new VirtualProperty());
$out = $dumper->dump($data, true);

$this->assertStringMatchesFormat(<<<EODUMP
<foo></foo><bar><span class=sf-dump-note>Symfony\Component\VarDumper\Tests\Fixtures\VirtualProperty</span> {<a class=sf-dump-ref>#%i</a><samp data-depth=1 class=sf-dump-expanded>
+<span class=sf-dump-public title="Public property">firstName</span>: "<span class=sf-dump-str title="4 characters">John</span>"
+<span class=sf-dump-public title="Public property">lastName</span>: "<span class=sf-dump-str title="3 characters">Doe</span>"
+<span class=sf-dump-virtual><span class=sf-dump-public title="Public property">fullName</span></span>: <span class=sf-dump-virtual><span class=sf-dump-const title="Virtual property">~ string</span></span>
-<span class=sf-dump-virtual><span class=sf-dump-private title="Private property defined in class:&#10;`Symfony\Component\VarDumper\Tests\Fixtures\VirtualProperty`">noType</span></span>: <span class=sf-dump-virtual><span class=sf-dump-const title="Virtual property">~</span></span>
</samp>}
</bar>
EODUMP, $out);
}

public function testCharset()
{
$var = mb_convert_encoding('Словарь', 'CP1251', 'UTF-8');
Expand Down
21 changes: 21 additions & 0 deletions src/Symfony/Component/VarDumper/Tests/Fixtures/VirtualProperty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Symfony\Component\VarDumper\Tests\Fixtures;

class VirtualProperty
{
public string $firstName = 'John';
public string $lastName = 'Doe';

public string $fullName {
get {
return $this->firstName.' '.$this->lastName;
}
}

private $noType {
get {
return null;
}
}
}
Loading