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

Skip to content

Commit 73b3cf7

Browse files
[VarDumper] Make ClassStub handle missing classes or methods
1 parent c3ec1c2 commit 73b3cf7

File tree

2 files changed

+66
-14
lines changed

2 files changed

+66
-14
lines changed

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

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,34 @@ public function __construct($identifier, $callable = null)
3232
$this->attr['ellipsis'] = strlen($identifier) - $i;
3333
}
3434

35-
if (null !== $callable) {
36-
if ($callable instanceof \Closure) {
37-
$r = new \ReflectionFunction($callable);
38-
} elseif (is_object($callable)) {
39-
$r = new \ReflectionMethod($callable, '__invoke');
40-
} elseif (is_array($callable)) {
41-
$r = new \ReflectionMethod($callable[0], $callable[1]);
42-
} elseif (false !== $i = strpos($callable, '::')) {
43-
$r = new \ReflectionMethod(substr($callable, 0, $i), substr($callable, 2 + $i));
35+
try {
36+
if (null !== $callable) {
37+
if ($callable instanceof \Closure) {
38+
$r = new \ReflectionFunction($callable);
39+
} elseif (is_object($callable)) {
40+
$r = array($callable, '__invoke');
41+
} elseif (is_array($callable)) {
42+
$r = $callable;
43+
} elseif (false !== $i = strpos($callable, '::')) {
44+
$r = array(substr($callable, 0, $i), substr($callable, 2 + $i));
45+
} else {
46+
$r = new \ReflectionFunction($callable);
47+
}
48+
} elseif (false !== $i = strpos($identifier, '::')) {
49+
$r = array(substr($identifier, 0, $i), substr($identifier, 2 + $i));
4450
} else {
45-
$r = new \ReflectionFunction($callable);
51+
$r = new \ReflectionClass($identifier);
4652
}
47-
} elseif (false !== $i = strpos($identifier, '::')) {
48-
$r = new \ReflectionMethod(substr($identifier, 0, $i), substr($identifier, 2 + $i));
49-
} else {
50-
$r = new \ReflectionClass($identifier);
53+
54+
if (is_array($r)) {
55+
try {
56+
$r = new \ReflectionMethod($r[0], $r[1]);
57+
} catch (\ReflectionException $e) {
58+
$r = new \ReflectionClass($r[0]);
59+
}
60+
}
61+
} catch (\ReflectionException $e) {
62+
return;
5163
}
5264

5365
if ($f = $r->getFileName()) {

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,46 @@ public function testClassStub()
121121
<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>"
122122
</samp>]
123123
</bar>
124+
EODUMP;
125+
126+
$this->assertStringMatchesFormat($expectedDump, $dump);
127+
}
128+
129+
public function testClassStubWithNotExistingClass()
130+
{
131+
$var = array(new ClassStub(NotExisting::class));
132+
133+
$cloner = new VarCloner();
134+
$dumper = new HtmlDumper();
135+
$dumper->setDumpHeader('<foo></foo>');
136+
$dumper->setDumpBoundaries('<bar>', '</bar>');
137+
$dump = $dumper->dump($cloner->cloneVar($var), true);
138+
139+
$expectedDump = <<<'EODUMP'
140+
<foo></foo><bar><span class=sf-dump-note>array:1</span> [<samp>
141+
<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>"
142+
</samp>]
143+
</bar>
144+
EODUMP;
145+
146+
$this->assertStringMatchesFormat($expectedDump, $dump);
147+
}
148+
149+
public function testClassStubWithNotExistingMethod()
150+
{
151+
$var = array(new ClassStub('hello', array(FooInterface::class, 'missing')));
152+
153+
$cloner = new VarCloner();
154+
$dumper = new HtmlDumper();
155+
$dumper->setDumpHeader('<foo></foo>');
156+
$dumper->setDumpBoundaries('<bar>', '</bar>');
157+
$dump = $dumper->dump($cloner->cloneVar($var), true);
158+
159+
$expectedDump = <<<'EODUMP'
160+
<foo></foo><bar><span class=sf-dump-note>array:1</span> [<samp>
161+
<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>"
162+
</samp>]
163+
</bar>
124164
EODUMP;
125165

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

0 commit comments

Comments
 (0)