Skip to content

Commit a3b40db

Browse files
committed
Enhanced Utils::printSafe() to output more information about arrays
1 parent 3f04d29 commit a3b40db

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/Utils/Utils.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,23 @@ public static function printSafe($var)
253253
if (is_object($var)) {
254254
return 'instance of ' . get_class($var);
255255
}
256+
if (is_array($var)) {
257+
$count = count($var);
258+
if (!isset($var[0]) && $count > 0) {
259+
$keys = [];
260+
$keyCount = 0;
261+
foreach ($var as $key => $value) {
262+
$keys[] = $key;
263+
if ($keyCount++ > 4) {
264+
break;
265+
}
266+
}
267+
$msg = "associative array($count) with first keys: " . implode(', ', $keys);
268+
} else {
269+
$msg = "array($count)";
270+
}
271+
return $msg;
272+
}
256273
if ('' === $var) {
257274
return '(empty string)';
258275
}

0 commit comments

Comments
 (0)