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

Skip to content

Commit 57c76a4

Browse files
bug #28444 [VarDumper] fix dumping signature of callables (nicolas-grekas)
This PR was merged into the 4.2-dev branch. Discussion ---------- [VarDumper] fix dumping signature of callables | Q | A | ------------- | --- | Branch? | master | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Adds missing visual hint when returning by reference + makes exclude-verbose mode display only the signature. E.g. using psysh, before: ![image](https://user-images.githubusercontent.com/243674/45360788-ebe0bf80-b5d0-11e8-8e43-62f954e926af.png) after: ![image](https://user-images.githubusercontent.com/243674/45360760-d9668600-b5d0-11e8-820a-98cc174ba2ca.png) (`dump -a` shows the details as usual.) Commits ------- 16f2bd5 [VarDumper] fix dumping signature of callables
2 parents 68c869b + 16f2bd5 commit 57c76a4

File tree

3 files changed

+14
-18
lines changed

3 files changed

+14
-18
lines changed

src/Symfony/Component/VarDumper/Caster/ClassStub.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,6 @@ public function __construct(string $identifier, $callable = null)
7070
} else {
7171
$this->value .= $s;
7272
}
73-
if (isset($this->attr['ellipsis'])) {
74-
$this->attr['ellipsis'] += \strlen($this->value) - \strlen($identifier);
75-
}
7673
}
7774
} catch (\ReflectionException $e) {
7875
return;

src/Symfony/Component/VarDumper/Caster/ReflectionCaster.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,22 @@ class ReflectionCaster
3333

3434
public static function castClosure(\Closure $c, array $a, Stub $stub, $isNested, $filter = 0)
3535
{
36-
$prefix = Caster::PREFIX_VIRTUAL;
3736
$c = new \ReflectionFunction($c);
3837

3938
$a = static::castFunctionAbstract($c, $a, $stub, $isNested, $filter);
4039

4140
$stub->class .= self::getSignature($a);
4241

42+
$prefix = Caster::PREFIX_DYNAMIC;
43+
unset($a['name'], $a[$prefix.'this'], $a[$prefix.'parameter'], $a[Caster::PREFIX_VIRTUAL.'extra']);
44+
$prefix = Caster::PREFIX_VIRTUAL;
45+
46+
if ($filter & Caster::EXCLUDE_VERBOSE) {
47+
$stub->cut += ($c->getFileName() ? 2 : 0) + \count($a);
48+
49+
return array();
50+
}
51+
4352
if (isset($a[$prefix.'parameters'])) {
4453
foreach ($a[$prefix.'parameters']->value as &$v) {
4554
$param = $v;
@@ -53,14 +62,11 @@ public static function castClosure(\Closure $c, array $a, Stub $stub, $isNested,
5362
}
5463
}
5564

56-
if (!($filter & Caster::EXCLUDE_VERBOSE) && $f = $c->getFileName()) {
65+
if ($f = $c->getFileName()) {
5766
$a[$prefix.'file'] = new LinkStub($f, $c->getStartLine());
5867
$a[$prefix.'line'] = $c->getStartLine().' to '.$c->getEndLine();
5968
}
6069

61-
$prefix = Caster::PREFIX_DYNAMIC;
62-
unset($a['name'], $a[$prefix.'this'], $a[$prefix.'parameter'], $a[Caster::PREFIX_VIRTUAL.'extra']);
63-
6470
return $a;
6571
}
6672

@@ -333,7 +339,7 @@ public static function getSignature(array $a)
333339
}
334340
}
335341
}
336-
$signature = '('.substr($signature, 2).')';
342+
$signature = (empty($a[$prefix.'returnsReference']) ? '' : '&').'('.substr($signature, 2).')';
337343

338344
if (isset($a[$prefix.'returnType'])) {
339345
$signature .= ': '.substr(strrchr('\\'.$a[$prefix.'returnType'], '\\'), 1);

src/Symfony/Component/VarDumper/Tests/Caster/ReflectionCasterTest.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,9 @@ public function testClosureCaster()
8787

8888
public function testClosureCasterExcludingVerbosity()
8989
{
90-
$var = function () {};
90+
$var = function &($a = 5) {};
9191

92-
$expectedDump = <<<EOTXT
93-
Closure() {
94-
class: "Symfony\Component\VarDumper\Tests\Caster\ReflectionCasterTest"
95-
this: Symfony\Component\VarDumper\Tests\Caster\ReflectionCasterTest { …}
96-
}
97-
EOTXT;
98-
99-
$this->assertDumpEquals($expectedDump, $var, Caster::EXCLUDE_VERBOSE);
92+
$this->assertDumpEquals('Closure&($a = 5) { …6}', $var, Caster::EXCLUDE_VERBOSE);
10093
}
10194

10295
public function testReflectionParameter()

0 commit comments

Comments
 (0)