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
-------

16f2bd58d9 [VarDumper] fix dumping signature of callables
This commit is contained in:
Nicolas Grekas 2018-09-16 21:17:45 +02:00
commit 57c76a405b
3 changed files with 14 additions and 18 deletions

View File

@ -70,9 +70,6 @@ class ClassStub extends ConstStub
} else { } else {
$this->value .= $s; $this->value .= $s;
} }
if (isset($this->attr['ellipsis'])) {
$this->attr['ellipsis'] += \strlen($this->value) - \strlen($identifier);
}
} }
} catch (\ReflectionException $e) { } catch (\ReflectionException $e) {
return; return;

View File

@ -33,13 +33,22 @@ class ReflectionCaster
public static function castClosure(\Closure $c, array $a, Stub $stub, $isNested, $filter = 0) public static function castClosure(\Closure $c, array $a, Stub $stub, $isNested, $filter = 0)
{ {
$prefix = Caster::PREFIX_VIRTUAL;
$c = new \ReflectionFunction($c); $c = new \ReflectionFunction($c);
$a = static::castFunctionAbstract($c, $a, $stub, $isNested, $filter); $a = static::castFunctionAbstract($c, $a, $stub, $isNested, $filter);
$stub->class .= self::getSignature($a); $stub->class .= self::getSignature($a);
$prefix = Caster::PREFIX_DYNAMIC;
unset($a['name'], $a[$prefix.'this'], $a[$prefix.'parameter'], $a[Caster::PREFIX_VIRTUAL.'extra']);
$prefix = Caster::PREFIX_VIRTUAL;
if ($filter & Caster::EXCLUDE_VERBOSE) {
$stub->cut += ($c->getFileName() ? 2 : 0) + \count($a);
return array();
}
if (isset($a[$prefix.'parameters'])) { if (isset($a[$prefix.'parameters'])) {
foreach ($a[$prefix.'parameters']->value as &$v) { foreach ($a[$prefix.'parameters']->value as &$v) {
$param = $v; $param = $v;
@ -53,14 +62,11 @@ class ReflectionCaster
} }
} }
if (!($filter & Caster::EXCLUDE_VERBOSE) && $f = $c->getFileName()) { if ($f = $c->getFileName()) {
$a[$prefix.'file'] = new LinkStub($f, $c->getStartLine()); $a[$prefix.'file'] = new LinkStub($f, $c->getStartLine());
$a[$prefix.'line'] = $c->getStartLine().' to '.$c->getEndLine(); $a[$prefix.'line'] = $c->getStartLine().' to '.$c->getEndLine();
} }
$prefix = Caster::PREFIX_DYNAMIC;
unset($a['name'], $a[$prefix.'this'], $a[$prefix.'parameter'], $a[Caster::PREFIX_VIRTUAL.'extra']);
return $a; return $a;
} }
@ -333,7 +339,7 @@ class ReflectionCaster
} }
} }
} }
$signature = '('.substr($signature, 2).')'; $signature = (empty($a[$prefix.'returnsReference']) ? '' : '&').'('.substr($signature, 2).')';
if (isset($a[$prefix.'returnType'])) { if (isset($a[$prefix.'returnType'])) {
$signature .= ': '.substr(strrchr('\\'.$a[$prefix.'returnType'], '\\'), 1); $signature .= ': '.substr(strrchr('\\'.$a[$prefix.'returnType'], '\\'), 1);

View File

@ -87,16 +87,9 @@ EOTXT
public function testClosureCasterExcludingVerbosity() public function testClosureCasterExcludingVerbosity()
{ {
$var = function () {}; $var = function &($a = 5) {};
$expectedDump = <<<EOTXT $this->assertDumpEquals('Closure&($a = 5) { …6}', $var, Caster::EXCLUDE_VERBOSE);
Closure() {
class: "Symfony\Component\VarDumper\Tests\Caster\ReflectionCasterTest"
this: Symfony\Component\VarDumper\Tests\Caster\ReflectionCasterTest { }
}
EOTXT;
$this->assertDumpEquals($expectedDump, $var, Caster::EXCLUDE_VERBOSE);
} }
public function testReflectionParameter() public function testReflectionParameter()