Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb1e4214

Browse files
[VarDumper] Add support of named arguments todd() anddump() to display the argument name
1 parentc7f82de commitb1e4214

File tree

11 files changed

+145
-18
lines changed

11 files changed

+145
-18
lines changed

‎src/Symfony/Component/VarDumper/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Add caster for`WeakMap`
8+
* Add support of named arguments to`dd()` and`dump()` to display the argument name
89

910
6.2
1011
---
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespaceSymfony\Component\VarDumper\Caster;
13+
14+
useSymfony\Component\VarDumper\Cloner\Stub;
15+
16+
/**
17+
* Represents any arbitrary value.
18+
*
19+
* @author Alexandre Daubois <alex.daubois@gmail.com>
20+
*/
21+
class ScalarStubextends Stub
22+
{
23+
publicfunction__construct(mixed$value)
24+
{
25+
$this->value =$value;
26+
}
27+
}

‎src/Symfony/Component/VarDumper/Caster/StubCaster.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,12 @@ public static function castEnum(EnumStub $c, array $a, Stub $stub, bool $isNeste
8181

8282
return$a;
8383
}
84+
85+
publicstaticfunctioncastScalar(ScalarStub$scalarStub,array$a,Stub$stub)
86+
{
87+
$stub->type = Stub::TYPE_SCALAR;
88+
$stub->attr['value'] =$scalarStub->value;
89+
90+
return$a;
91+
}
8492
}

‎src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ abstract class AbstractCloner implements ClonerInterface
2828
'Symfony\Component\VarDumper\Caster\CutArrayStub' => ['Symfony\Component\VarDumper\Caster\StubCaster','castCutArray'],
2929
'Symfony\Component\VarDumper\Caster\ConstStub' => ['Symfony\Component\VarDumper\Caster\StubCaster','castStub'],
3030
'Symfony\Component\VarDumper\Caster\EnumStub' => ['Symfony\Component\VarDumper\Caster\StubCaster','castEnum'],
31+
'Symfony\Component\VarDumper\Caster\ScalarStub' => ['Symfony\Component\VarDumper\Caster\StubCaster','castScalar'],
3132

3233
'Fiber' => ['Symfony\Component\VarDumper\Caster\FiberCaster','castFiber'],
3334

‎src/Symfony/Component/VarDumper/Cloner/Data.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,11 @@ public function withContext(array $context): static
211211
return$data;
212212
}
213213

214+
publicfunctiongetContext():array
215+
{
216+
return$this->context;
217+
}
218+
214219
/**
215220
* Seeks to a specific key in nested data structures.
216221
*/
@@ -262,11 +267,12 @@ public function dump(DumperInterface $dumper)
262267
{
263268
$refs = [0];
264269
$cursor =newCursor();
270+
$label =$this->context['label'] ??'';
265271

266272
if ($cursor->attr =$this->context[SourceContextProvider::class] ?? []) {
267273
$cursor->attr['if_links'] =true;
268274
$cursor->hashType = -1;
269-
$dumper->dumpScalar($cursor,'default','^');
275+
$dumper->dumpScalar($cursor,'default',$label.'^');
270276
$cursor->attr = ['if_links' =>true];
271277
$dumper->dumpScalar($cursor,'default','');
272278
$cursor->hashType =0;
@@ -362,6 +368,10 @@ private function dumpItem(DumperInterface $dumper, Cursor $cursor, array &$refs,
362368
$dumper->leaveHash($cursor,$item->type,$item->class,$withChildren,$cut);
363369
break;
364370

371+
case Stub::TYPE_SCALAR:
372+
$dumper->dumpScalar($cursor,'default',$item->attr['value']);
373+
break;
374+
365375
default:
366376
thrownew \RuntimeException(sprintf('Unexpected Stub type: "%s".',$item->type));
367377
}

‎src/Symfony/Component/VarDumper/Cloner/Stub.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class Stub
2323
publicconstTYPE_ARRAY =3;
2424
publicconstTYPE_OBJECT =4;
2525
publicconstTYPE_RESOURCE =5;
26+
publicconstTYPE_SCALAR =6;
2627

2728
publicconstSTRING_BINARY =1;
2829
publicconstSTRING_UTF8 =2;

‎src/Symfony/Component/VarDumper/Dumper/ContextualizedDumper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function __construct(DataDumperInterface $wrappedDumper, array $contextPr
3333

3434
publicfunctiondump(Data$data)
3535
{
36-
$context =[];
36+
$context =$data->getContext();
3737
foreach ($this->contextProvidersas$contextProvider) {
3838
$context[$contextProvider::class] =$contextProvider->getContext();
3939
}

‎src/Symfony/Component/VarDumper/Resources/functions/dump.php

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,35 @@
99
* file that was distributed with this source code.
1010
*/
1111

12+
useSymfony\Component\VarDumper\Caster\ScalarStub;
1213
useSymfony\Component\VarDumper\VarDumper;
1314

1415
if (!function_exists('dump')) {
1516
/**
1617
* @author Nicolas Grekas <p@tchwork.com>
18+
* @author Alexandre Daubois <alex.daubois@gmail.com>
1719
*/
18-
functiondump(mixed$var,mixed...$moreVars):mixed
20+
functiondump(mixed ...$vars):mixed
1921
{
20-
VarDumper::dump($var);
22+
if (!$vars) {
23+
VarDumper::dump(newScalarStub('🐛'));
2124

22-
foreach ($moreVarsas$v) {
23-
VarDumper::dump($v);
25+
returnnull;
2426
}
2527

26-
if (1 <func_num_args()) {
27-
returnfunc_get_args();
28+
if (isset($vars[0]) &&1 ===count($vars)) {
29+
VarDumper::dump($vars[0]);
30+
}else {
31+
foreach ($varsas$key =>$v) {
32+
VarDumper::dump($v,is_numeric($key) ?$key+1 :$key);
33+
}
2834
}
2935

30-
return$var;
36+
if (1 <count($vars)) {
37+
return$vars;
38+
}
39+
40+
returnreset($vars);
3141
}
3242
}
3343

@@ -41,8 +51,12 @@ function dd(...$vars): void
4151
header('HTTP/1.1 500 Internal Server Error');
4252
}
4353

44-
foreach ($varsas$v) {
45-
VarDumper::dump($v);
54+
if (isset($vars[0]) &&1 ===count($vars)) {
55+
VarDumper::dump($vars[0]);
56+
}else {
57+
foreach ($varsas$key =>$v) {
58+
VarDumper::dump($v,is_numeric($key) ?$key+1 :$key);
59+
}
4660
}
4761

4862
exit(1);

‎src/Symfony/Component/VarDumper/Tests/Caster/StubCasterTest.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
useSymfony\Component\VarDumper\Caster\ArgsStub;
1616
useSymfony\Component\VarDumper\Caster\ClassStub;
1717
useSymfony\Component\VarDumper\Caster\LinkStub;
18+
useSymfony\Component\VarDumper\Caster\ScalarStub;
1819
useSymfony\Component\VarDumper\Cloner\VarCloner;
1920
useSymfony\Component\VarDumper\Dumper\HtmlDumper;
2021
useSymfony\Component\VarDumper\Test\VarDumperTestTrait;
@@ -87,6 +88,19 @@ public function testArgsStubWithClosure()
8788
$this->assertDumpMatchesFormat($expectedDump,$args);
8889
}
8990

91+
publicfunctiontestEmptyStub()
92+
{
93+
$args = [newScalarStub('🐛')];
94+
95+
$expectedDump = <<<'EODUMP'
96+
array:1 [
97+
0 => 🐛
98+
]
99+
EODUMP;
100+
101+
$this->assertDumpMatchesFormat($expectedDump,$args);
102+
}
103+
90104
publicfunctiontestLinkStub()
91105
{
92106
$var = [newLinkStub(__CLASS__,0,__FILE__)];
@@ -203,7 +217,7 @@ public function testClassStubWithAnonymousClass()
203217

204218
$expectedDump = <<<'EODUMP'
205219
<foo></foo><bar><span class=sf-dump-note>array:1</span> [<samp data-depth=1 class=sf-dump-expanded>
206-
<span class=sf-dump-index>0</span> => "<a href="%sStubCasterTest.php:195" rel="noopener noreferrer"><span class=sf-dump-str title="19 characters">Exception@anonymous</span></a>"
220+
<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>"
207221
</samp>]
208222
</bar>
209223
EODUMP;

‎src/Symfony/Component/VarDumper/Tests/Dumper/FunctionsTest.php

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@
1818

1919
class FunctionsTestextends TestCase
2020
{
21+
publicfunctiontestDumpWithoutArg()
22+
{
23+
$this->setupVarDumper();
24+
25+
ob_start();
26+
$return =dump();
27+
ob_end_clean();
28+
29+
$this->assertNull($return);
30+
}
31+
2132
publicfunctiontestDumpReturnsFirstArg()
2233
{
2334
$this->setupVarDumper();
@@ -28,7 +39,20 @@ public function testDumpReturnsFirstArg()
2839
$return =dump($var1);
2940
ob_end_clean();
3041

31-
$this->assertEquals($var1,$return);
42+
$this->assertSame($var1,$return);
43+
}
44+
45+
publicfunctiontestDumpReturnsFirstNamedArgWithoutSectionName()
46+
{
47+
$this->setupVarDumper();
48+
49+
$var1 ='a';
50+
51+
ob_start();
52+
$return =dump(first:$var1);
53+
ob_end_clean();
54+
55+
$this->assertSame($var1,$return);
3256
}
3357

3458
publicfunctiontestDumpReturnsAllArgsInArray()
@@ -43,7 +67,22 @@ public function testDumpReturnsAllArgsInArray()
4367
$return =dump($var1,$var2,$var3);
4468
ob_end_clean();
4569

46-
$this->assertEquals([$var1,$var2,$var3],$return);
70+
$this->assertSame([$var1,$var2,$var3],$return);
71+
}
72+
73+
publicfunctiontestDumpReturnsAllNamedArgsInArray()
74+
{
75+
$this->setupVarDumper();
76+
77+
$var1 ='a';
78+
$var2 ='b';
79+
$var3 ='c';
80+
81+
ob_start();
82+
$return =dump($var1, second:$var2, third:$var3);
83+
ob_end_clean();
84+
85+
$this->assertSame([$var1,'second' =>$var2,'third' =>$var3],$return);
4786
}
4887

4988
protectedfunctionsetupVarDumper()

‎src/Symfony/Component/VarDumper/VarDumper.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,17 @@ class VarDumper
3737
*/
3838
privatestatic$handler;
3939

40-
publicstaticfunctiondump(mixed$var)
40+
/**
41+
* @param string|null $label
42+
*/
43+
publicstaticfunctiondump(mixed$var/*, string $label = null */)
4144
{
45+
$label =2 <=\func_num_args() ?func_get_arg(1) :null;
4246
if (null ===self::$handler) {
4347
self::register();
4448
}
4549

46-
return (self::$handler)($var);
50+
return (self::$handler)($var,$label);
4751
}
4852

4953
publicstaticfunctionsetHandler(callable$callable =null): ?callable
@@ -90,8 +94,16 @@ private static function register(): void
9094
$dumper =newContextualizedDumper($dumper, [newSourceContextProvider()]);
9195
}
9296

93-
self::$handler =function ($var)use ($cloner,$dumper) {
94-
$dumper->dump($cloner->cloneVar($var));
97+
self::$handler =function ($var,string$label =null)use ($cloner,$dumper) {
98+
$var =$cloner->cloneVar($var);
99+
100+
if (null !==$label) {
101+
$var =$var->withContext([
102+
'label' =>$label,
103+
]);
104+
}
105+
106+
$dumper->dump($var);
95107
};
96108
}
97109

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp