Skip to content

Commit 71bd02f

Browse files
committed
Parameter to strip useless entries from the fullstack output
1 parent 4a4a3aa commit 71bd02f

File tree

3 files changed

+39
-34
lines changed

3 files changed

+39
-34
lines changed

lib/r.php

+24-31
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
{
77
use Slam\Debug\R as DebugR;
88

9-
function r($var, bool $exit = true, int $level = 0, bool $fullstack = false): void
9+
function r($var, bool $exit = true, int $level = 0, bool $fullstack = false, string $stripFromFullstack = null): void
1010
{
1111
DebugR::$db = \debug_backtrace();
12-
DebugR::debug($var, $exit, $level, $fullstack);
12+
DebugR::debug($var, $exit, $level, $fullstack, $stripFromFullstack);
1313
}
1414

15-
function rq(string $query, array $params, bool $exit = true, int $level = 0, bool $fullstack = false): void
15+
function rq(string $query, array $params, bool $exit = true, int $level = 0, bool $fullstack = false, string $stripFromFullstack = null): void
1616
{
1717
\uksort($params, function (string $key1, string $key2) {
1818
return \strlen($key2) <=> \strlen($key1);
@@ -27,50 +27,56 @@ function rq(string $query, array $params, bool $exit = true, int $level = 0, boo
2727
}
2828

2929
DebugR::$db = \debug_backtrace();
30-
DebugR::debug($query, $exit, $level, $fullstack);
30+
DebugR::debug($query, $exit, $level, $fullstack, $stripFromFullstack);
3131
}
3232
}
3333

3434
namespace Slam\Debug
3535
{
3636
final class R
3737
{
38+
/**
39+
* @var array
40+
*/
3841
public static $db = [];
3942

4043
private function __construct()
4144
{
4245
}
4346

44-
public static function debug($var, bool $exit = true, int $level = 0, bool $fullstack = false): void
47+
public static function debug($var, bool $exit = true, int $level = 0, bool $fullstack = false, string $stripFromFullstack = null): void
4548
{
4649
if (null === $var || \is_scalar($var)) {
4750
\ob_start();
4851
\var_dump($var);
49-
$output = \trim(\ob_get_clean());
52+
$output = \trim((string) \ob_get_clean());
5053
} elseif ($level > 0) {
5154
$output = \print_r(Doctrine\Debug::export($var, $level), true);
5255
} else {
5356
$output = \print_r($var, true);
5457
}
5558

5659
if (\PHP_SAPI === 'cli') {
57-
\fwrite(\STDERR, \PHP_EOL . self::formatDb($fullstack) . $output . \PHP_EOL);
60+
\fwrite(\STDERR, \PHP_EOL . self::formatDb($fullstack, $stripFromFullstack) . $output . \PHP_EOL);
5861
} else {
59-
echo '<pre><strong>' . self::formatDb($fullstack) . '</strong><br />' . \htmlspecialchars($output) . '</pre>';
62+
echo '<pre><strong>' . self::formatDb($fullstack, $stripFromFullstack) . '</strong><br />' . \htmlspecialchars($output) . '</pre>';
6063
}
6164

6265
if ($exit) {
6366
exit(253);
6467
}
6568
}
6669

67-
private static function formatDb(bool $fullstack): string
70+
private static function formatDb(bool $fullstack, string $stripFromFullstack = null): string
6871
{
6972
$output = '';
7073

7174
foreach (self::$db as $point) {
7275
if (isset($point['file'])) {
73-
$output .= $point['file'] . '(' . $point['line'] . '): ';
76+
if (null !== $stripFromFullstack && false !== \strpos($point['file'], $stripFromFullstack)) {
77+
continue;
78+
}
79+
$output .= $point['file'] . ':' . $point['line'] . ' > ';
7480
}
7581

7682
$output .= (isset($point['class']) ? $point['class'] . '->' : '') . $point['function'];
@@ -145,7 +151,7 @@ private function __construct()
145151
*
146152
* @return string
147153
*/
148-
public static function dump($var, $maxDepth = 2, $stripTags = true, $echo = true)
154+
public static function dump($var, int $maxDepth = 2, bool $stripTags = true, bool $echo = true)
149155
{
150156
if (\extension_loaded('xdebug')) {
151157
\ini_set('xdebug.var_display_max_depth', (string) $maxDepth);
@@ -156,7 +162,7 @@ public static function dump($var, $maxDepth = 2, $stripTags = true, $echo = true
156162
\ob_start();
157163
\var_dump($var);
158164

159-
$dump = \ob_get_contents();
165+
$dump = (string) \ob_get_contents();
160166

161167
\ob_end_clean();
162168

@@ -171,11 +177,10 @@ public static function dump($var, $maxDepth = 2, $stripTags = true, $echo = true
171177

172178
/**
173179
* @param mixed $var
174-
* @param int $maxDepth
175180
*
176181
* @return mixed
177182
*/
178-
public static function export($var, $maxDepth)
183+
public static function export($var, int $maxDepth)
179184
{
180185
$return = null;
181186
$isObj = \is_object($var);
@@ -203,7 +208,7 @@ public static function export($var, $maxDepth)
203208
return $var;
204209
}
205210

206-
$return = new \stdclass();
211+
$return = new \stdClass();
207212
if ($var instanceof \DateTimeInterface) {
208213
$return->__CLASS__ = \get_class($var);
209214
$return->date = $var->format('c');
@@ -230,13 +235,9 @@ public static function export($var, $maxDepth)
230235
* Fill the $return variable with class attributes
231236
* Based on obj2array function from {@see https://secure.php.net/manual/en/function.get-object-vars.php#47075}.
232237
*
233-
* @param object $var
234-
* @param \stdClass $return
235-
* @param int $maxDepth
236-
*
237238
* @return mixed
238239
*/
239-
private static function fillReturnWithClassAttributes($var, \stdClass $return, $maxDepth)
240+
private static function fillReturnWithClassAttributes(object $var, \stdClass $return, int $maxDepth)
240241
{
241242
$clone = (array) $var;
242243

@@ -254,28 +255,20 @@ private static function fillReturnWithClassAttributes($var, \stdClass $return, $
254255

255256
/**
256257
* Gets the real class name of a class name that could be a proxy.
257-
*
258-
* @param string $class
259-
*
260-
* @return string
261258
*/
262-
private static function getRealClass($class)
259+
private static function getRealClass(string $class): string
263260
{
264261
if (! \class_exists(Proxy::class) || false === ($pos = \strrpos($class, '\\' . Proxy::MARKER . '\\'))) {
265262
return $class;
266263
}
267264

268-
return \substr($class, $pos + Proxy::MARKER_LENGTH + 2);
265+
return \substr($class, (int) ($pos + Proxy::MARKER_LENGTH + 2));
269266
}
270267

271268
/**
272269
* Gets the real class name of an object (even if its a proxy).
273-
*
274-
* @param object $object
275-
*
276-
* @return string
277270
*/
278-
private static function getClass($object)
271+
private static function getClass(object $object): string
279272
{
280273
return self::getRealClass(\get_class($object));
281274
}

tests/Doctrine/DebugTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ public function testExportParentAttributes(TestAsset\ParentClass $class, array $
114114
$print_r_class = \print_r($class, true);
115115
$print_r_expected = \print_r($expected, true);
116116

117-
$print_r_class = \substr($print_r_class, \strpos($print_r_class, '('));
118-
$print_r_expected = \substr($print_r_expected, \strpos($print_r_expected, '('));
117+
$print_r_class = \substr($print_r_class, (int) \strpos($print_r_class, '('));
118+
$print_r_expected = \substr($print_r_expected, (int) \strpos($print_r_expected, '('));
119119

120120
static::assertSame($print_r_expected, $print_r_class);
121121

tests/RTest.php

+13-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
final class RTest extends TestCase
1111
{
12-
public const STREAM_FILTER_NAME = 'STDERR_MOCK';
12+
private const STREAM_FILTER_NAME = 'STDERR_MOCK';
1313

1414
/**
1515
* @var bool
@@ -59,6 +59,18 @@ public function testFullstackOutput(): void
5959
static::assertStringContainsString(__FILE__, MockStderr::$output);
6060
static::assertStringContainsString(__FUNCTION__, MockStderr::$output);
6161
static::assertStringContainsString('TestCase', MockStderr::$output);
62+
static::assertRegExp(\sprintf('/%s:\d+\b/', \preg_quote(__FILE__, '/')), MockStderr::$output);
63+
static::assertStringContainsString('TextUI/Command', MockStderr::$output);
64+
}
65+
66+
public function testStripEntriesFromFullstack(): void
67+
{
68+
r(1, false, 0, true, 'TextUI');
69+
70+
static::assertStringContainsString(__FILE__, MockStderr::$output);
71+
static::assertStringContainsString(__FUNCTION__, MockStderr::$output);
72+
static::assertStringContainsString('TestCase', MockStderr::$output);
73+
static::assertStringNotContainsString('TextUI/Command', MockStderr::$output);
6274
}
6375

6476
public function testQueryDebug(): void

0 commit comments

Comments
 (0)