Skip to content

[VarDumper] Make ClassStub handle missing classes or methods #19836

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
Sep 6, 2016
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
40 changes: 26 additions & 14 deletions src/Symfony/Component/VarDumper/Caster/ClassStub.php
Original file line number Diff line number Diff line change
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);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't this be better as a switch?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope :)

} 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
Original file line number Diff line number Diff line change
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