Skip to content

Commit

Permalink
Kint_Parser_Trace: Allow filtering of trace frames
Browse files Browse the repository at this point in the history
  • Loading branch information
jnvsor committed Apr 22, 2017
1 parent afdd224 commit d6b2540
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 38 deletions.
2 changes: 1 addition & 1 deletion build/kint-aante-light.php

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/kint-solarized-dark.php

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/kint-solarized.php

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/kint.php

Large diffs are not rendered by default.

26 changes: 19 additions & 7 deletions src/Kint.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,12 +302,22 @@ public static function dump($data = null)
// Kint::dump(1) shorthand
if ((!isset($params[0]['name']) || $params[0]['name'] == '1') && $num_args === 1 && $data === 1) {
if (KINT_PHP525) {
$trace = debug_backtrace(true);
$data = debug_backtrace(true);
} else {
$trace = debug_backtrace();
$data = debug_backtrace();
}

$trace = array();

// No need to normalize as we've already called it through getCalleeInfo at this point
foreach ($data as $index => $frame) {
if (Kint_Parser_Trace::frameIsListed($frame, self::$aliases)) {
$trace = array();
}

$trace[] = $frame;
}

$trace = Kint_Parser_Trace::trimTrace($trace);
$lastframe = array_shift($trace);
$tracename = $lastframe['function'].'(1)';
if (isset($lastframe['class'], $lastframe['type'])) {
Expand Down Expand Up @@ -427,17 +437,19 @@ public static function getIdeLink($file, $line)
*/
private static function getCalleeInfo($trace, $num_params)
{
Kint_Parser_Trace::normalizeAliases(self::$aliases);
$miniTrace = array();

foreach ($trace as $index => $frame) {
if ($frame['function'] === 'spl_autoload_call' && !isset($frame['object']) && !isset($frame['class'])) {
continue;
if (Kint_Parser_Trace::frameIsListed($frame, self::$aliases)) {
$miniTrace = array();
}

$miniTrace[] = $frame;
if (!Kint_Parser_Trace::frameIsListed($frame, array('spl_autoload_call'))) {
$miniTrace[] = $frame;
}
}

$miniTrace = Kint_Parser_Trace::trimTrace($miniTrace);
$callee = reset($miniTrace);
$caller = next($miniTrace);
if (!$callee) {
Expand Down
39 changes: 12 additions & 27 deletions src/Parser/Trace.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

class Kint_Parser_Trace extends Kint_Parser_Plugin
{
public static $blacklist = array('spl_autoload_call');

public function getTypes()
{
return array('array');
Expand Down Expand Up @@ -29,6 +31,8 @@ public function parse(&$var, Kint_Object &$o, $trigger)

$old_trace = $rep->contents;

self::normalizeAliases(self::$blacklist);

$rep->contents = array();

foreach ($old_trace as $frame) {
Expand All @@ -39,7 +43,7 @@ public function parse(&$var, Kint_Object &$o, $trigger)
continue;
}

if ($trace[$index]['function'] === 'spl_autoload_call' && !isset($trace[$index]['object']) && !isset($trace[$index]['class'])) {
if (self::frameIsListed($trace[$index], self::$blacklist)) {
continue;
}

Expand Down Expand Up @@ -82,49 +86,30 @@ public static function isTrace(array $trace)
return $file_found;
}

public static function trimTrace(array $trace)
public static function frameIsListed(array $frame, array $matches)
{
$trimmed_trace = array();

foreach ($trace as $index => $frame) {
if (self::frameIsInternal($frame)) {
$trimmed_trace = array();
}

$trimmed_trace[] = $frame;
}

return $trimmed_trace;
}

private static function frameIsInternal(array $frame)
{
self::normalizeAliases(Kint::$aliases);

if (isset($frame['object'])) {
$called = array($frame['object'], strtolower($frame['function']));
} elseif (isset($frame['class'])) {
if (isset($frame['class'])) {
$called = array(strtolower($frame['class']), strtolower($frame['function']));
} else {
$called = strtolower($frame['function']);
}

return in_array($called, Kint::$aliases, true);
return in_array($called, $matches, true);
}

private static function normalizeAliases(array &$aliases)
public static function normalizeAliases(array &$aliases)
{
foreach ($aliases as $index => &$alias) {
if (!is_callable($alias)) {
unset($aliases[$index]);
} elseif (is_array($alias) && count($alias) === 2) {
if (is_array($alias) && count($alias) === 2) {
$alias = array_values($alias);
$alias[1] = strtolower($alias[1]);
if (is_string($alias[0])) {
$alias[0] = strtolower($alias[0]);
}
} elseif (is_string($alias)) {
$alias = strtolower($alias);
} else {
unset($aliases[$index]);
}
}

Expand Down

0 comments on commit d6b2540

Please sign in to comment.