8000 Improve stack trace display · symfony/symfony@eea83c2 · GitHub
[go: up one dir, main page]

Skip to content

Commit eea83c2

Browse files
committed
Improve stack trace display
The package name is now included, allow people to understand why a deprecation is direct/indirect, etc…
1 parent 4e679bc commit eea83c2

File tree

3 files changed

+63
-6
lines changed

3 files changed

+63
-6
lines changed

src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler/Deprecation.php

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,10 +276,69 @@ public function __toString()
276276
$reflection->setAccessible(true);
277277
$reflection->setValue($exception, $this->trace);
278278

279-
return 'deprecation triggered by '.$this->originatingClass().'::'.$this->originatingMethod().':'.
279+
$string = 'deprecation triggered by '.$this->originatingClass().'::'.$this->originatingMethod().':'.
280280
"\n".$this->message.
281281
"\nStack trace:".
282-
"\n".str_replace(' '.getcwd().\DIRECTORY_SEPARATOR, ' ', $exception->getTraceAsString()).
283282
"\n";
283+
foreach ($exception->getTrace() as $index => $line) {
284+
$package = $this->getPackageFromLine($line);
285+
$string .= sprintf(
286+
'#%d [%s] %s%s::%s',
287+
$index,
288+
$package,
289+
isset($line['file']) ? sprintf('%s(%d): ', $line['file'], $line['line']): '',
290+
$line['class'],
291+
$line['function']
292+
);
293+
$textArgs = [];
294+
foreach ($line['args'] as $arg) {
295+
switch (gettype($arg)) {
296+
case 'integer':
297+
$textArgs[] = $arg;
298+
break;
299+
case 'string':
300+
$argString = "'";
301+
$substr = substr($arg, 0, 15);
302+
$argString .= str_replace("\n", '\n', $substr);
303+
if (strlen($substr) !== strlen($string)) {
304+
$argString .= '';
305+
}
306+
$textArgs[] = "$argString'";
307+
break;
308+
case 'array':
309+
$textArgs[] = 'Array';
310+
break;
311+
case 'object':
312+
$textArgs[] = sprintf('Object(%s)', get_class($arg));
313+
break;
314+
case 'boolean':
315+
$textArgs[] = $arg?'true':'false';
316+
break;
317+
318+
default:
319+
$textArgs[] = gettype($arg);
320+
break;
321+
}
322+
}
323+
$string .= sprintf('(%s)', implode(', ', $textArgs));
324+
$string .= PHP_EOL;
325+
}
326+
327+
return $string;
328+
}
329+
330+
private function getPackageFromLine(array $line)
331+
{
332+
if (!isset($line['file'])) {
333+
return 'internal function';
334+
}
335+
if (!$this->pathOriginatesFromVendor($line['file'])) {
336+
return 'source code';
337+
}
338+
try {
339+
return $this->getPackage($line['file']);
340+
} catch (\RuntimeException $e) {
341+
return 'unknown';
342+
}
284343
}
285344
}

src/Symfony/Bridge/PhpUnit/Tests/DeprecationErrorHandler/deprecated_regexp.phpt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,5 @@ Setting SYMFONY_DEPRECATIONS_HELPER to just "/foo/" is deprecated in favor of "r
3737
Legacy deprecation triggered by FooTestCase::testLegacyFoo:
3838
silenced foo deprecation
3939
Stack trace:
40-
#%A(%d): FooTestCase->testLegacyFoo()
41-
#%d {main}
40+
#%A(%d): FooTestCase::testLegacyFoo()
4241

src/Symfony/Bridge/PhpUnit/Tests/DeprecationErrorHandler/regexp.phpt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,5 @@ $foo->testLegacyFoo();
3535
Legacy deprecation triggered by FooTestCase::testLegacyFoo:
3636
silenced foo deprecation
3737
Stack trace:
38-
#%A(%d): FooTestCase->testLegacyFoo()
39-
#%d {main}
38+
#%A(%d): FooTestCase::testLegacyFoo()
4039

0 commit comments

Comments
 (0)
0