Skip to content

Commit

Permalink
Merge pull request #5485 from kenjis/update-kint
Browse files Browse the repository at this point in the history
chore: update Kint to 4.0
  • Loading branch information
kenjis authored Dec 20, 2021
2 parents cbe1028 + d542514 commit 9bc6753
Show file tree
Hide file tree
Showing 93 changed files with 1,513 additions and 1,019 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"ext-intl": "*",
"ext-json": "*",
"ext-mbstring": "*",
"kint-php/kint": "^3.3",
"kint-php/kint": "^4.0",
"laminas/laminas-escaper": "^2.9",
"psr/log": "^1.1"
},
Expand Down
4 changes: 2 additions & 2 deletions system/CodeIgniter.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ protected function initializeKint()
*/
$config = config('Config\Kint');

Kint::$max_depth = $config->maxDepth;
Kint::$depth_limit = $config->maxDepth;
Kint::$display_called_from = $config->displayCalledFrom;
Kint::$expanded = $config->expanded;

Expand All @@ -261,7 +261,7 @@ protected function initializeKint()
RichRenderer::$folder = $config->richFolder;
RichRenderer::$sort = $config->richSort;
if (! empty($config->richObjectPlugins) && is_array($config->richObjectPlugins)) {
RichRenderer::$object_plugins = $config->richObjectPlugins;
RichRenderer::$value_plugins = $config->richObjectPlugins;
}
if (! empty($config->richTabPlugins) && is_array($config->richTabPlugins)) {
RichRenderer::$tab_plugins = $config->richTabPlugins;
Expand Down
124 changes: 84 additions & 40 deletions system/ThirdParty/Kint/CallFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,33 +27,35 @@

class CallFinder
{
private static $ignore = array(
private static $ignore = [
T_CLOSE_TAG => true,
T_COMMENT => true,
T_DOC_COMMENT => true,
T_INLINE_HTML => true,
T_OPEN_TAG => true,
T_OPEN_TAG_WITH_ECHO => true,
T_WHITESPACE => true,
);
];

/**
* Things we need to do specially for operator tokens:
* - Refuse to strip spaces around them
* - Wrap the access path in parentheses if there
* are any of these in the final short parameter.
*/
private static $operator = array(
private static $operator = [
T_AND_EQUAL => true,
T_BOOLEAN_AND => true,
T_BOOLEAN_OR => true,
T_ARRAY_CAST => true,
T_BOOL_CAST => true,
T_CLASS => true,
T_CLONE => true,
T_CONCAT_EQUAL => true,
T_DEC => true,
T_DIV_EQUAL => true,
T_DOUBLE_CAST => true,
T_FUNCTION => true,
T_INC => true,
T_INCLUDE => true,
T_INCLUDE_ONCE => true,
Expand Down Expand Up @@ -84,6 +86,9 @@ class CallFinder
T_STRING_CAST => true,
T_UNSET_CAST => true,
T_XOR_EQUAL => true,
T_POW => true,
T_POW_EQUAL => true,
T_DOUBLE_ARROW => true,
'!' => true,
'%' => true,
'&' => true,
Expand All @@ -100,9 +105,9 @@ class CallFinder
'^' => true,
'|' => true,
'~' => true,
);
];

private static $strip = array(
private static $strip = [
'(' => true,
')' => true,
'[' => true,
Expand All @@ -112,39 +117,43 @@ class CallFinder
T_OBJECT_OPERATOR => true,
T_DOUBLE_COLON => true,
T_NS_SEPARATOR => true,
);
];

private static $classcalls = [
T_DOUBLE_COLON => true,
T_OBJECT_OPERATOR => true,
];

private static $namespace = [
T_STRING => true,
];

public static function getFunctionCalls($source, $line, $function)
{
static $up = array(
static $up = [
'(' => true,
'[' => true,
'{' => true,
T_CURLY_OPEN => true,
T_DOLLAR_OPEN_CURLY_BRACES => true,
);
static $down = array(
];
static $down = [
')' => true,
']' => true,
'}' => true,
);
static $modifiers = array(
];
static $modifiers = [
'!' => true,
'@' => true,
'~' => true,
'+' => true,
'-' => true,
);
static $identifier = array(
];
static $identifier = [
T_DOUBLE_COLON => true,
T_STRING => true,
T_NS_SEPARATOR => true,
);

if (KINT_PHP56) {
self::$operator[T_POW] = true;
self::$operator[T_POW_EQUAL] = true;
}
];

if (KINT_PHP70) {
self::$operator[T_SPACESHIP] = true;
Expand All @@ -154,11 +163,24 @@ public static function getFunctionCalls($source, $line, $function)
self::$operator[T_COALESCE_EQUAL] = true;
}

if (KINT_PHP80) {
$up[T_ATTRIBUTE] = true;
self::$operator[T_MATCH] = true;
self::$strip[T_NULLSAFE_OBJECT_OPERATOR] = true;
self::$classcalls[T_NULLSAFE_OBJECT_OPERATOR] = true;
self::$namespace[T_NAME_FULLY_QUALIFIED] = true;
self::$namespace[T_NAME_QUALIFIED] = true;
self::$namespace[T_NAME_RELATIVE] = true;
$identifier[T_NAME_FULLY_QUALIFIED] = true;
$identifier[T_NAME_QUALIFIED] = true;
$identifier[T_NAME_RELATIVE] = true;
}

$tokens = \token_get_all($source);
$cursor = 1;
$function_calls = array();
/** @var array<int, null|array|string> Performance optimization preventing backwards loops */
$prev_tokens = array(null, null, null);
$function_calls = [];
// Performance optimization preventing backwards loops
$prev_tokens = [null, null, null];

if (\is_array($function)) {
$class = \explode('\\', $function[0]);
Expand Down Expand Up @@ -188,10 +210,16 @@ public static function getFunctionCalls($source, $line, $function)
continue;
}

$prev_tokens = array($prev_tokens[1], $prev_tokens[2], $token);
$prev_tokens = [$prev_tokens[1], $prev_tokens[2], $token];

// Check if it's the right type to be the function we're looking for
if (T_STRING !== $token[0] || \strtolower($token[1]) !== $function) {
if (!isset(self::$namespace[$token[0]])) {
continue;
}

$ns = \explode('\\', \strtolower($token[1]));

if (\end($ns) !== $function) {
continue;
}

Expand All @@ -203,15 +231,23 @@ public static function getFunctionCalls($source, $line, $function)

// Check if it matches the signature
if (null === $class) {
if ($prev_tokens[1] && \in_array($prev_tokens[1][0], array(T_DOUBLE_COLON, T_OBJECT_OPERATOR), true)) {
if ($prev_tokens[1] && isset(self::$classcalls[$prev_tokens[1][0]])) {
continue;
}
} else {
if (!$prev_tokens[1] || T_DOUBLE_COLON !== $prev_tokens[1][0]) {
continue;
}

if (!$prev_tokens[0] || T_STRING !== $prev_tokens[0][0] || \strtolower($prev_tokens[0][1]) !== $class) {
if (!$prev_tokens[0] || !isset(self::$namespace[$prev_tokens[0][0]])) {
continue;
}

/** @var array{int, string, int} $prev_tokens[0] */
// All self::$namespace tokens are T_ constants
$ns = \explode('\\', \strtolower($prev_tokens[0][1]));

if (\end($ns) !== $class) {
continue;
}
}
Expand All @@ -222,8 +258,8 @@ public static function getFunctionCalls($source, $line, $function)
$instring = false; // Whether we're in a string or not
$realtokens = false; // Whether the current scope contains anything meaningful or not
$paramrealtokens = false; // Whether the current parameter contains anything meaningful
$params = array(); // All our collected parameters
$shortparam = array(); // The short version of the parameter
$params = []; // All our collected parameters
$shortparam = []; // The short version of the parameter
$param_start = $offset; // The distance to the start of the parameter

// Loop through the following tokens until the function call ends
Expand Down Expand Up @@ -276,11 +312,11 @@ public static function getFunctionCalls($source, $line, $function)
$shortparam[] = '"';
} elseif (1 === $depth) {
if (',' === $token[0]) {
$params[] = array(
$params[] = [
'full' => \array_slice($tokens, $param_start, $offset - $param_start),
'short' => $shortparam,
);
$shortparam = array();
];
$shortparam = [];
$paramrealtokens = false;
$param_start = $offset + 1;
} elseif (T_CONSTANT_ENCAPSED_STRING === $token[0] && \strlen($token[1]) > 2) {
Expand All @@ -293,10 +329,10 @@ public static function getFunctionCalls($source, $line, $function)
// Depth has dropped to 0 (So we've hit the closing paren)
if ($depth <= 0) {
if ($paramrealtokens) {
$params[] = array(
$params[] = [
'full' => \array_slice($tokens, $param_start, $offset - $param_start),
'short' => $shortparam,
);
];
}

break;
Expand All @@ -322,11 +358,11 @@ public static function getFunctionCalls($source, $line, $function)
}
}

$param = array(
$param = [
'name' => self::tokensToString($name),
'path' => self::tokensToString(self::tokensTrim($param['full'])),
'expression' => $expression,
);
];
}

// Get the modifiers
Expand All @@ -340,7 +376,7 @@ public static function getFunctionCalls($source, $line, $function)
--$index;
}

$mods = array();
$mods = [];

while (isset($tokens[$index])) {
if (isset(self::$ignore[$tokens[$index][0]])) {
Expand All @@ -357,10 +393,10 @@ public static function getFunctionCalls($source, $line, $function)
break;
}

$function_calls[] = array(
$function_calls[] = [
'parameters' => $params,
'modifiers' => $mods,
);
];
}

return $function_calls;
Expand Down Expand Up @@ -436,10 +472,11 @@ private static function tokensTrim(array $tokens)
private static function tokensFormatted(array $tokens)
{
$space = false;
$attribute = false;

$tokens = self::tokensTrim($tokens);

$output = array();
$output = [];
$last = null;

foreach ($tokens as $index => $token) {
Expand All @@ -450,7 +487,10 @@ private static function tokensFormatted(array $tokens)

$next = $tokens[self::realTokenIndex($tokens, $index)];

if (isset(self::$strip[$last[0]]) && !self::tokenIsOperator($next)) {
/** @var array|string $last */
if ($attribute && ']' === $last[0]) {
$attribute = false;
} elseif (isset(self::$strip[$last[0]]) && !self::tokenIsOperator($next)) {
continue;
}

Expand All @@ -461,6 +501,10 @@ private static function tokensFormatted(array $tokens)
$token = ' ';
$space = true;
} else {
if (KINT_PHP80 && $last && T_ATTRIBUTE == $last[0]) {
$attribute = true;
}

$space = false;
$last = $token;
}
Expand Down
Loading

0 comments on commit 9bc6753

Please sign in to comment.