Skip to content

Commit

Permalink
bug #19836 [VarDumper] Make ClassStub handle missing classes or metho…
Browse files Browse the repository at this point in the history
…ds (nicolas-grekas)

This PR was merged into the 3.2-dev branch.

Discussion
----------

[VarDumper] Make ClassStub handle missing classes or methods

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | yes (well, missing feat in previous PR)
| Tests pass?   | yes
| License       | MIT

So that one can still add ellipses when dumping about non existing classes or methods.

Commits
-------

73b3cf7 [VarDumper] Make ClassStub handle missing classes or methods
  • Loading branch information
nicolas-grekas committed Sep 6, 2016
2 parents a565fae + 73b3cf7 commit db3ed6c
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 14 deletions.
40 changes: 26 additions & 14 deletions src/Symfony/Component/VarDumper/Caster/ClassStub.php
Expand Up @@ -32,22 +32,34 @@ public function __construct($identifier, $callable = null)
$this->attr['ellipsis'] = strlen($identifier) - $i;
}

if (null !== $callable) {
if ($callable instanceof \Closure) {
$r = new \ReflectionFunction($callable);
} elseif (is_object($callable)) {
$r = new \ReflectionMethod($callable, '__invoke');
} elseif (is_array($callable)) {
$r = new \ReflectionMethod($callable[0], $callable[1]);
} elseif (false !== $i = strpos($callable, '::')) {
$r = new \ReflectionMethod(substr($callable, 0, $i), substr($callable, 2 + $i));
try {
if (null !== $callable) {
if ($callable instanceof \Closure) {
$r = new \ReflectionFunction($callable);
} elseif (is_object($callable)) {
$r = array($callable, '__invoke');
} elseif (is_array($callable)) {
$r = $callable;
} elseif (false !== $i = strpos($callable, '::')) {
$r = array(substr($callable, 0, $i), substr($callable, 2 + $i));
} else {
$r = new \ReflectionFunction($callable);
}
} elseif (false !== $i = strpos($identifier, '::')) {
$r = array(substr($identifier, 0, $i), substr($identifier, 2 + $i));
} else {
$r = new \ReflectionFunction($callable);
$r = new \ReflectionClass($identifier);
}
} elseif (false !== $i = strpos($identifier, '::')) {
$r = new \ReflectionMethod(substr($identifier, 0, $i), substr($identifier, 2 + $i));
} else {
$r = new \ReflectionClass($identifier);

if (is_array($r)) {
try {
$r = new \ReflectionMethod($r[0], $r[1]);
} catch (\ReflectionException $e) {
$r = new \ReflectionClass($r[0]);
}
}
} catch (\ReflectionException $e) {
return;
}

if ($f = $r->getFileName()) {
Expand Down
40 changes: 40 additions & 0 deletions src/Symfony/Component/VarDumper/Tests/Caster/StubCasterTest.php
Expand Up @@ -121,6 +121,46 @@ public function testClassStub()
<span class=sf-dump-index>0</span> => "<a data-file="%sFooInterface.php" data-line="8"><span class=sf-dump-str title="5 characters">hello</span></a>"
</samp>]
</bar>
EODUMP;

$this->assertStringMatchesFormat($expectedDump, $dump);
}

public function testClassStubWithNotExistingClass()
{
$var = array(new ClassStub(NotExisting::class));

$cloner = new VarCloner();
$dumper = new HtmlDumper();
$dumper->setDumpHeader('<foo></foo>');
$dumper->setDumpBoundaries('<bar>', '</bar>');
$dump = $dumper->dump($cloner->cloneVar($var), true);

$expectedDump = <<<'EODUMP'
<foo></foo><bar><span class=sf-dump-note>array:1</span> [<samp>
<span class=sf-dump-index>0</span> => "<span class=sf-dump-str title="52 characters"><abbr title="Symfony\Component\VarDumper\Tests\Caster" class=sf-dump-ellipsis>Symfony\Component\VarDumper\Tests\Caster</abbr>\NotExisting</span>"
</samp>]
</bar>
EODUMP;

$this->assertStringMatchesFormat($expectedDump, $dump);
}

public function testClassStubWithNotExistingMethod()
{
$var = array(new ClassStub('hello', array(FooInterface::class, 'missing')));

$cloner = new VarCloner();
$dumper = new HtmlDumper();
$dumper->setDumpHeader('<foo></foo>');
$dumper->setDumpBoundaries('<bar>', '</bar>');
$dump = $dumper->dump($cloner->cloneVar($var), true);

$expectedDump = <<<'EODUMP'
<foo></foo><bar><span class=sf-dump-note>array:1</span> [<samp>
<span class=sf-dump-index>0</span> => "<a data-file="%sFooInterface.php" data-line="5"><span class=sf-dump-str title="5 characters">hello</span></a>"
</samp>]
</bar>
EODUMP;

$this->assertStringMatchesFormat($expectedDump, $dump);
Expand Down

0 comments on commit db3ed6c

Please sign in to comment.