8000 [VarDumper] Fine tune dumping log messages · symfony/symfony@7dd5c90 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7dd5c90

Browse files
[VarDumper] Fine tune dumping log messages
1 parent ce3ef6a commit 7dd5c90

File tree

12 files changed

+140
-45
lines changed

12 files changed

+140
-45
lines changed

src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/profiler.css.twig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,7 @@ table.logs .metadata {
913913
#collector-content .sf-dump-key { color: #789339; }
914914
#collector-content .sf-dump-ref { color: #6E6E6E; }
915915
#collector-content .sf-dump-ellipsis { color: #CC7832; max-width: 100em; }
916+
#collector-content .sf-dump-ellipsis-path { max-width: 5em; }
916917

917918
#collector-content .sf-dump {
918919
margin: 0;

src/Symfony/Component/Debug/DebugClassLoader.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public function loadClass($class)
162162
$name = $refl->getName();
163163

164164
if ($name !== $class && 0 === strcasecmp($name, $class)) {
165-
throw new \RuntimeException(sprintf('Case mismatch between loaded and declared class names: %s vs %s', $class, $name));
165+
throw new \RuntimeException(sprintf('Case mismatch between loaded and declared class names: "%s" vs "%s".', $class, $name));
166166
}
167167

168168
$parent = get_parent_class($class);
@@ -174,7 +174,7 @@ public function loadClass($class)
174174
}
175175

176176
if ($parent && isset(self::$final[$parent])) {
177-
@trigger_error(sprintf('The %s class is considered final%s. It may change without further notice as of its next major version. You should not extend it from %s.', $parent, self::$final[$parent], $name), E_USER_DEPRECATED);
177+
@trigger_error(sprintf('The "%s" class is considered final%s. It may change without further notice as of its next major version. You should not extend it from "%s".', $parent, self::$final[$parent], $name), E_USER_DEPRECATED);
178178
}
179179

180180
// Inherit @final annotations
@@ -186,7 +186,7 @@ public function loadClass($class)
186186
}
187187

188188
if ($parent && isset(self::$finalMethods[$parent][$method->name])) {
189-
@trigger_error(sprintf('%s It may change without further notice as of its next major version. You should not extend it from %s.', self::$finalMethods[$parent][$method->name], $name), E_USER_DEPRECATED);
189+
@trigger_error(sprintf('%s It may change without further notice as of its next major version. You should not extend it from "%s".', self::$finalMethods[$parent][$method->name], $name), E_USER_DEPRECATED);
190190
}
191191

192192
$doc = $method->getDocComment();
@@ -196,13 +196,13 @@ public function loadClass($class)
196196

197197
if (preg_match('#\n\s+\* @final(?:( .+?)\.?)?\r?\n\s+\*(?: @|/$)#s', $doc, $notice)) {
198198
$message = isset($notice[1]) ? preg_replace('#\s*\r?\n \* +#', ' ', $notice[1]) : '';
199-
self::$finalMethods[$name][$method->name] = sprintf('The %s::%s() method is considered final%s.', $name, $method->name, $message);
199+
self::$finalMethods[$name][$method->name] = sprintf('The "%s::%s()" method is considered final%s.', $name, $method->name, $message);
200200
}
201201
}
202202
}
203203

204204
if (in_array(strtolower($refl->getShortName()), self::$php7Reserved)) {
205-
@trigger_error(sprintf('%s uses a reserved class name (%s) that will break on PHP 7 and higher', $name, $refl->getShortName()), E_USER_DEPRECATED);
205+
@trigger_error(sprintf('The "%s" class uses the reserved name "%s", it will break on PHP 7 and higher', $name, $refl->getShortName()), E_USER_DEPRECATED);
206206
} elseif (preg_match('#\n \* @deprecated (.*?)\r?\n \*(?: @|/$)#s', $refl->getDocComment(), $notice)) {
207207
self::$deprecated[$name] = preg_replace('#\s*\r?\n \* +#', ' ', $notice[1]);
208208
} else {
@@ -223,7 +223,7 @@ public function loadClass($class)
223223

224224
if (!$parent || strncmp($ns, $parent, $len)) {
225225
if ($parent && isset(self::$deprecated[$parent]) && strncmp($ns, $parent, $len)) {
226-
@trigger_error(sprintf('The %s class extends %s that is deprecated %s', $name, $parent, self::$deprecated[$parent]), E_USER_DEPRECATED);
226+
@trigger_error(sprintf('The "%s" class extends "%s" that is deprecated %s', $name, $parent, self::$deprecated[$parent]), E_USER_DEPRECATED);
227227
}
228228

229229
$parentInterfaces = array();
@@ -245,7 +245,7 @@ public function loadClass($class)
245245

246246
foreach ($deprecatedInterfaces as $interface) {
247247
if (!isset($parentInterfaces[$interface])) {
248-
@trigger_error(sprintf('The %s %s %s that is deprecated %s', $name, $refl->isInterface() ? 'interface extends' : 'class implements', $interface, self::$deprecated[$interface]), E_USER_DEPRECATED);
248+
@trigger_error(sprintf('The "%s" %s "%s" that is deprecated %s', $name, $refl->isInterface() ? 'interface extends' : 'class implements', $interface, self::$deprecated[$interface]), E_USER_DEPRECATED);
249249
}
250250
}
251251
}
@@ -342,7 +342,7 @@ public function loadClass($class)
342342
if (0 === substr_compare($real, $tail, -$tailLen, $tailLen, true)
343343
&& 0 !== substr_compare($real, $tail, -$tailLen, $tailLen, false)
344344
) {
345-
throw new \RuntimeException(sprintf('Case mismatch between class and real file names: %s vs %s in %s', substr($tail, -$tailLen + 1), substr($real, -$tailLen + 1), substr($real, 0, -$tailLen + 1)));
345+
throw new \RuntimeException(sprintf('Case mismatch between class and real file names: "%s" vs "%s" in "%s".', substr($tail, -$tailLen + 1), substr($real, -$tailLen + 1), substr($real, 0, -$tailLen + 1)));
346346
}
347347
}
348348

src/Symfony/Component/Debug/Tests/DebugClassLoaderTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ class_exists('Test\\'.__NAMESPACE__.'\\'.$class, true);
185185

186186
$xError = array(
187187
'type' => E_USER_DEPRECATED,
188-
'message' => 'The Test\Symfony\Component\Debug\Tests\\'.$class.' class '.$type.' Symfony\Component\Debug\Tests\Fixtures\\'.$super.' that is deprecated but this is a test deprecation notice.',
188+
'message' => 'The "Test\Symfony\Component\Debug\Tests\\'.$class.'" class '.$type.' "Symfony\Component\Debug\Tests\Fixtures\\'.$super.'" that is deprecated but this is a test deprecation notice.',
189189
);
190190

191191
$this->assertSame($xError, $lastError);
@@ -263,7 +263,7 @@ class_exists('Test\\'.__NAMESPACE__.'\\Float', true);
263263

264264
$xError = array(
265265
'type' => E_USER_DEPRECATED,
266-
'message' => 'Test\Symfony\Component\Debug\Tests\Float uses a reserved class name (Float) that will break on PHP 7 and higher',
266+
'message' => 'The "Test\Symfony\Component\Debug\Tests\Float" class uses the reserved name "Float", it will break on PHP 7 and higher',
267267
);
268268

269269
$this->assertSame($xError, $lastError);
@@ -285,7 +285,7 @@ class_exists('Test\\'.__NAMESPACE__.'\\ExtendsFinalClass', true);
285285

286286
$xError = array(
287287
'type' => E_USER_DEPRECATED,
288-
'message' => 'The Symfony\Component\Debug\Tests\Fixtures\FinalClass class is considered final since version 3.3. It may change without further notice as of its next major version. You should not extend it from Test\Symfony\Component\Debug\Tests\ExtendsFinalClass.',
288+
'message' => 'The "Symfony\Component\Debug\Tests\Fixtures\FinalClass" class is considered final since version 3.3. It may change without further notice as of its next major version. You should not extend it from "Test\Symfony\Component\Debug\Tests\ExtendsFinalClass".',
289289
);
290290

291291
$this->assertSame($xError, $lastError);
@@ -307,7 +307,7 @@ class_exists(__NAMESPACE__.'\\Fixtures\\ExtendedFinalMethod', true);
307307

308308
$xError = array(
309309
'type' => E_USER_DEPRECATED,
310-
'message' => 'The Symfony\Component\Debug\Tests\Fixtures\FinalMethod::finalMethod() method is considered final since version 3.3. It may change without further notice as of its next major version. You should not extend it from Symfony\Component\Debug\Tests\Fixtures\ExtendedFinalMethod.',
310+
'message' => 'The "Symfony\Component\Debug\Tests\Fixtures\FinalMethod::finalMethod()" method is considered final since version 3.3. It may change without further notice as of its next major version. You should not extend it from "Symfony\Component\Debug\Tests\Fixtures\ExtendedFinalMethod".',
311311
);
312312

313313
$this->assertSame($xError, $lastError);

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

Lines changed: 2 additions & 0 deletions
F438
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ public function __construct($identifier, $callable = null)
3030

3131
if (0 < $i = strrpos($identifier, '\\')) {
3232
$this->attr['ellipsis'] = strlen($identifier) - $i;
33+
$this->attr['ellipsis-type'] = 'class';
34+
$this->attr['ellipsis-tail'] = 1;
3335
}
3436

3537
try {

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

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\VarDumper\Caster;
1313

14+
use Symfony\Component\Debug\Exception\SilencedErrorContext;
1415
use Symfony\Component\VarDumper\Exception\ThrowingCasterException;
1516
use Symfony\Component\VarDumper\Cloner\Stub;
1617

@@ -64,20 +65,44 @@ public static function castErrorException(\ErrorException $e, array $a, Stub $st
6465

6566
public static function castThrowingCasterException(ThrowingCasterException $e, array $a, Stub $stub, $isNested)
6667
{
68+
$trace = Caster::PREFIX_VIRTUAL.'trace';
6769
$prefix = Caster::PREFIX_PROTECTED;
6870
$xPrefix = "\0Exception\0";
6971

70-
if (isset($a[$xPrefix.'previous'], $a[$xPrefix.'trace']) && $a[$xPrefix.'previous'] instanceof \Exception) {
72+
if (isset($a[$xPrefix.'previous'], $a[$trace]) && $a[$xPrefix.'previous'] instanceof \Exception) {
7173
$b = (array) $a[$xPrefix.'previous'];
7274
self::traceUnshift($b[$xPrefix.'trace'], get_class($a[$xPrefix.'previous']), $b[$prefix.'file'], $b[$prefix.'line']);
73-
$a[$xPrefix.'trace'] = new TraceStub($b[$xPrefix.'trace'], false, 0, -count($a[$xPrefix.'trace']->value));
75+
$a[$trace] = new TraceStub($b[$xPrefix.'trace'], false, 0, -count($a[$trace]->value));
7476
}
7577

7678
unset($a[$xPrefix.'previous'], $a[$prefix.'code'], $a[$prefix.'file'], $a[$prefix.'line']);
7779

7880
return $a;
7981
}
8082

83+
public static function castSilencedErrorContext(SilencedErrorContext $e, array $a, Stub $stub, $isNested)
84+
{
85+
$sPrefix = "\0".SilencedErrorContext::class."\0";
86+
$xPrefix = "\0Exception\0";
87+
88+
if (!isset($a[$s = $sPrefix.'severity'])) {
89+
return $a;
90+
}
91+
92+
if (isset(self::$errorTypes[$a[$s]])) {
93+
$a[$s] = new ConstStub(self::$errorTypes[$a[$s]], $a[$s]);
94+
}
95+
96+
$trace = array(
97+
'file' => $a[$sPrefix.'file'],
98+
'line' => $a[$sPrefix.'line'],
99+
);
100+
unset($a[$sPrefix.'file'], $a[$sPrefix.'line']);
101+
$a[Caster::PREFIX_VIRTUAL.'trace'] = new TraceStub(array($trace));
102+
103+
return $a;
104+
}
105+
81106
public static function castTraceStub(TraceStub $trace, array $a, Stub $stub, $isNested)
82107
{
83108
if (!$isNested) {
@@ -159,8 +184,9 @@ public static function castFrameStub(FrameStub $frame, array $a, Stub $stub, $is
159184
$caller = isset($f['function']) ? sprintf('in %s() on line %d', (isset($f['class']) ? $f['class'].$f['type'] : '').$f['function'], $f['line']) : null;
160185
$src = $f['line'];
161186
$srcKey = $f['file'];
162-
$ellipsis = explode(DIRECTORY_SEPARATOR, $srcKey);
163-
$ellipsis = 3 < count($ellipsis) ? 2 + strlen(implode(array_slice($ellipsis, -2))) : 0;
187+
$ellipsis = (new LinkStub($srcKey, 0))->attr;
188+
$ellipsisTail = isset($ellipsis['ellipsis-tail']) ? $ellipsis['ellipsis-tail'] : 0;
189+
$ellipsis = isset($ellipsis['ellipsis']) ? $ellipsis['ellipsis'] : 0;
164190

165191
if (file_exists($f['file']) && 0 <= self::$srcContext) {
166192
if (!empty($f['class']) && is_subclass_of($f['class'], 'Twig_Template') && method_exists($f['class'], 'getDebugInfo')) {
@@ -187,7 +213,7 @@ public static function castFrameStub(FrameStub $frame, array $a, Stub $stub, $is
187213
}
188214
}
189215
}
190-
$srcAttr = $ellipsis ? 'ellipsis='.$ellipsis : '';
216+
$srcAttr = $ellipsis ? 'ellipsis-type=path&ellipsis='.$ellipsis.'&ellipsis-tail='.$ellipsisTail : '';
191217
self::$framesCache[$cacheKey] = $a[$prefix.'src'] = new EnumStub(array("\0~$srcAttr\0$srcKey" => $src));
192218
}
193219
}
@@ -221,7 +247,7 @@ private static function filterExceptionArray($xClass, array $a, $xPrefix, $filte
221247
if (isset($a[Caster::PREFIX_PROTECTED.'file'], $a[Caster::PREFIX_PROTECTED.'line'])) {
222248
self::traceUnshift($trace, $xClass, $a[Caster::PREFIX_PROTECTED.'file'], $a[Caster::PREFIX_PROTECTED.'line']);
223249
}
224-
$a[$xPrefix.'trace'] = new TraceStub($trace, self::$traceArgs);
250+
$a[Caster::PREFIX_VIRTUAL.'trace'] = new TraceStub($trace, self::$traceArgs);
225251
}
226252
if (empty($a[$xPrefix.'previous'])) {
227253
unset($a[$xPrefix.'previous']);

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

Lines changed: 67 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,34 +18,85 @@
1818
*/
1919
class LinkStub extends ConstStub
2020
{
21+
private static $vendorRoots;
22+
private static $composerRoots;
23+
2124
public function __construct($label, $line = 0, $href = null)
2225
{
2326
$this->value = $label;
2427

2528
if (null === $href) {
2629
$href = $label;
2730
}
28-
if (is_string($href)) {
29-
if (0 === strpos($href, 'file://')) {
30-
if ($href === $label) {
31-
$label = substr($label, 7);
31+
if (!is_string($href)) {
32+
return;
33+
}
34+
if (0 === strpos($href, 'file://')) {
35+
if ($href === $label) {
36+
$label = substr($label, 7);
37+
}
38+
$href = substr($href, 7);
39+
} elseif (false !== strpos($href, '://')) {
40+
$this->attr['href'] = $href;
41+
42+
return;
43+
}
44+
if (!file_exists($href)) {
45+
return;
46+
}
47+
if ($line) {
48+
$this->attr['line'] = $line;
49+
}
50+
if ($label !== $this->attr['file'] = realpath($href) ?: $href) {
51+
return;
52+
}
53+
if ($composerRoot = $this->getComposerRoot($href, $inVendor)) {
54+
$this->attr['ellipsis'] = strlen($href) - strlen($composerRoot) + 1;
55+
$this->attr['ellipsis-type'] = 'path';
56+
$this->attr['ellipsis-tail'] = 1 + ($inVendor ? 2 + strlen(implode(array_slice(explode(DIRECTORY_SEPARATOR, substr($href, 1 - $this->attr['ellipsis'])), 0, 2))) : 0);
57+
} elseif (3 < count($ellipsis = explode(DIRECTORY_SEPARATOR, $href))) {
58+
$this->attr['ellipsis'] = 2 + strlen(implode(array_slice($ellipsis, -2)));
59+
$this->attr['ellipsis-type'] = 'path';
60+
$this->attr['ellipsis-tail'] = 1;
61+
}
62+
}
63+
64+
private function getComposerRoot($file, &$inVendor)
65+
{
66+
if (null === self::$vendorRoots) {
67+
self::$vendorRoots = array();
68+
69+
foreach (get_declared_classes() as $class) {
70+
if ('C' === $class[0] && 0 === strpos($class, 'ComposerAutoloaderInit')) {
71+
$r = new \ReflectionClass($class);
72+
$v = dirname(dirname($r->getFileName()));
73+
if (file_exists($v.'/composer/installed.json')) {
74+
self::$vendorRoots[] = $v.DIRECTORY_SEPARATOR;
75+
}
3276
}
33-
$href = substr($href, 7);
34-
} elseif (false !== strpos($href, '://')) {
35-
$this->attr['href'] = $href;
77+
}
78+
}
79+
$inVendor = false;
80+
81+
if (isset(self::$composerRoots[$dir = dirname($file)])) {
82+
return self::$composerRoots[$dir];
83+
}
3684

37-
return;
85+
foreach (self::$vendorRoots as $root) {
86+
if ($inVendor = 0 === strpos($file, $root)) {
87+
return $root;
3888
}
39-
if (file_exists($href)) {
40-
if ($line) {
41-
$this->attr['line'] = $line;
42-
}
43-
$this->attr['file'] = realpath($href) ?: $href;
89+
}
4490

45-
if ($this->attr['file'] === $label && 3 < count($ellipsis = explode(DIRECTORY_SEPARATOR, $href))) {
46-
$this->attr['ellipsis'] = 2 + strlen(implode(array_slice($ellipsis, -2)));
47-
}
91+
$parent = $dir;
92+
while (!file_exists($parent.'/composer.json')) {
93+
if ($parent === dirname($parent)) {
94+
return self::$composerRoots[$dir] = false;
4895
}
96+
97+
$parent = dirname($parent);
4998
}
99+
100+
return self::$composerRoots[$dir] = $parent.DIRECTORY_SEPARATOR;
50101
}
51102
}

src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ abstract class AbstractCloner implements ClonerInterface
7979
'Symfony\Component\VarDumper\Exception\ThrowingCasterException' => array('Symfony\Component\VarDumper\Caster\ExceptionCaster', 'castThrowingCasterException'),
8080
'Symfony\Component\VarDumper\Caster\TraceStub' => array('Symfony\Component\VarDumper\Caster\ExceptionCaster', 'castTraceStub'),
8181
'Symfony\Component\VarDumper\Caster\FrameStub' => array('Symfony\Component\VarDumper\Caster\ExceptionCaster', 'castFrameStub'),
82+
'Symfony\Component\Debug\Exception\SilencedErrorContext' => array('Symfony\Component\VarDumper\Caster\ExceptionCaster', 'castSilencedErrorContext'),
8283

8384
'PHPUnit_Framework_MockObject_MockObject' => array('Symfony\Component\VarDumper\Caster\StubCaster', 'cutInternals'),
8485
'Prophecy\Prophecy\ProphecySubjectInterface' => array('Symfony\Component\VarDumper\Caster\StubCaster', 'cutInternals'),

src/Symfony/Component/VarDumper/Dumper/HtmlDumper.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,9 @@ function showCurrent(state)
623623
overflow: hidden;
624624
vertical-align: top;
625625
}
626+
pre.sf-dump .sf-dump-ellipsis+.sf-dump-ellipsis {
627+
max-width: none;
628+
}
626629
pre.sf-dump code {
627630
display:inline;
628631
padding:0;
@@ -788,9 +791,20 @@ protected function style($style, $value, $attr = array())
788791
$map = static::$controlCharsMap;
789792

790793
if (isset($attr['ellipsis'])) {
794+
$class = 'sf-dump-ellipsis';
795+
if (isset($attr['ellipsis-type'])) {
796+
$class = sprintf('"%s sf-dump-ellipsis-%s"', $class, $attr['ellipsis-type']);
797+
}
791798
$label = esc(substr($value, -$attr['ellipsis']));
792799
$style = str_replace(' title="', " title=\"$v\n", $style);
793-
$v = sprintf('<span class=sf-dump-ellipsis>%s</span>%s', substr($v, 0, -strlen($label)), $label);
800+
$v = sprintf('<span class=%s>%s</span>', $class, substr($v, 0, -strlen($label)));
801+
802+
if (!empty($attr['ellipsis-tail'])) {
803+
$tail = strlen(esc(substr($value, -$attr['ellipsis'], $attr['ellipsis-tail'])));
804+
$v .= sprintf('<span class=sf-dump-ellipsis>%s</span>%s', substr($label, 0, $tail), substr($label, $tail));
805+
} else {
806+
$v .= $label;
807+
}
794808
}
795809

796810
$v = "<span class=sf-dump-{$style}>".preg_replace_callback(static::$controlCharsRx, function ($c) use ($map) {

0 commit comments

Comments
 (0)
0