chore: update Kint to v6.0 (#9289)

* feat: update kint to v6.0

* fix: run cs
This commit is contained in:
Denny Septian Panggabean 2024-12-02 13:17:23 +07:00 committed by GitHub
parent bcedf1ccfc
commit c46cea4ee0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
145 changed files with 7521 additions and 4551 deletions

View File

@ -20,7 +20,7 @@
"codeigniter/coding-standard": "^1.7",
"fakerphp/faker": "^1.9",
"friendsofphp/php-cs-fixer": "^3.47.1",
"kint-php/kint": "^5.0.4",
"kint-php/kint": "^6.0",
"mikey179/vfsstream": "^1.6",
"nexusphp/cs-config": "^3.6",
"phpunit/phpunit": "^10.5.16 || ^11.2",

View File

@ -3,7 +3,6 @@
namespace Config;
use Kint\Parser\ConstructablePluginInterface;
use Kint\Renderer\AbstractRenderer;
use Kint\Renderer\Rich\TabPluginInterface;
use Kint\Renderer\Rich\ValuePluginInterface;
@ -41,7 +40,6 @@ class Kint
*/
public string $richTheme = 'aante-light.css';
public bool $richFolder = false;
public int $richSort = AbstractRenderer::SORT_FULL;
/**
* @var array<string, class-string<ValuePluginInterface>>|null

View File

@ -19,7 +19,7 @@
"require-dev": {
"codeigniter/phpstan-codeigniter": "^1.4",
"fakerphp/faker": "^1.9",
"kint-php/kint": "^5.0.4",
"kint-php/kint": "^6.0",
"mikey179/vfsstream": "^1.6",
"nexusphp/tachycardia": "^2.0",
"phpstan/extension-installer": "^1.4",

View File

@ -547,7 +547,7 @@ class Autoloader
RichRenderer::$theme = $config->richTheme;
RichRenderer::$folder = $config->richFolder;
RichRenderer::$sort = $config->richSort;
if (isset($config->richObjectPlugins) && is_array($config->richObjectPlugins)) {
RichRenderer::$value_plugins = $config->richObjectPlugins;
}

View File

@ -294,7 +294,7 @@ class CodeIgniter
RichRenderer::$theme = $config->richTheme;
RichRenderer::$folder = $config->richFolder;
RichRenderer::$sort = $config->richSort;
if (isset($config->richObjectPlugins) && is_array($config->richObjectPlugins)) {
RichRenderer::$value_plugins = $config->richObjectPlugins;
}

View File

@ -30,10 +30,17 @@ namespace Kint;
/**
* @psalm-type PhpTokenArray = array{int, string, int}
* @psalm-type PhpToken = string|PhpTokenArray
* @psalm-type CallParameter = array{
* name: string,
* path: string,
* expression: bool,
* literal: bool,
* new_without_parens: bool,
* }
*/
class CallFinder
{
private static $ignore = [
private static array $ignore = [
T_CLOSE_TAG => true,
T_COMMENT => true,
T_DOC_COMMENT => true,
@ -49,13 +56,12 @@ class CallFinder
* - Wrap the access path in parentheses if there
* are any of these in the final short parameter.
*/
private static $operator = [
private static array $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,
@ -79,7 +85,6 @@ class CallFinder
T_MINUS_EQUAL => true,
T_MOD_EQUAL => true,
T_MUL_EQUAL => true,
T_NEW => true,
T_OBJECT_CAST => true,
T_OR_EQUAL => true,
T_PLUS_EQUAL => true,
@ -96,6 +101,8 @@ class CallFinder
T_POW_EQUAL => true,
T_SPACESHIP => true,
T_DOUBLE_ARROW => true,
T_FN => true,
T_COALESCE_EQUAL => true,
'!' => true,
'%' => true,
'&' => true,
@ -114,7 +121,12 @@ class CallFinder
'~' => true,
];
private static $strip = [
private static array $preserve_spaces = [
T_CLASS => true,
T_NEW => true,
];
private static array $strip = [
'(' => true,
')' => true,
'[' => true,
@ -126,19 +138,19 @@ class CallFinder
T_NS_SEPARATOR => true,
];
private static $classcalls = [
private static array $classcalls = [
T_DOUBLE_COLON => true,
T_OBJECT_OPERATOR => true,
];
private static $namespace = [
private static array $namespace = [
T_STRING => true,
];
/**
* @psalm-param callable-array|callable-string $function
*
* @psalm-return list<array{parameters: list, modifiers: list<PhpToken>}>
* @psalm-return list<array{parameters: list<CallParameter>, modifiers: list<PhpToken>}>
*
* @return array List of matching calls on the relevant line
*/
@ -169,11 +181,6 @@ class CallFinder
T_NS_SEPARATOR => true,
];
if (KINT_PHP74) {
self::$operator[T_FN] = true;
self::$operator[T_COALESCE_EQUAL] = true;
}
if (KINT_PHP80) {
$up[T_ATTRIBUTE] = true;
self::$operator[T_MATCH] = true;
@ -187,9 +194,12 @@ class CallFinder
$identifier[T_NAME_RELATIVE] = true;
}
if (!KINT_PHP84) {
self::$operator[T_NEW] = true; // @codeCoverageIgnore
}
/** @psalm-var list<PhpToken> */
$tokens = \token_get_all($source);
$cursor = 1;
$function_calls = [];
// Performance optimization preventing backwards loops
@ -204,6 +214,7 @@ class CallFinder
$class = null;
/**
* @psalm-suppress RedundantFunctionCallGivenDocblockType
* Psalm bug #11075
*/
$function = \strtolower($function);
}
@ -214,11 +225,7 @@ class CallFinder
continue;
}
// Count newlines for line number instead of using $token[2]
// since certain situations (String tokens after whitespace) may
// not have the correct line number unless you do this manually
$cursor += \substr_count($token[1], "\n");
if ($cursor > $line) {
if ($token[2] > $line) {
break;
}
@ -229,6 +236,12 @@ class CallFinder
$prev_tokens = [$prev_tokens[1], $prev_tokens[2], $token];
// The logic for 7.3 through 8.1 is far more complicated.
// This should speed things up without making a lot more work for us
if (KINT_PHP82 && $line !== $token[2]) {
continue;
}
// Check if it's the right type to be the function we're looking for
if (!isset(self::$namespace[$token[0]])) {
continue;
@ -242,26 +255,29 @@ class CallFinder
// Check if it's a function call
$nextReal = self::realTokenIndex($tokens, $index);
if (!isset($nextReal, $tokens[$nextReal]) || '(' !== $tokens[$nextReal]) {
if ('(' !== ($tokens[$nextReal] ?? null)) {
continue;
}
// Check if it matches the signature
if (null === $class) {
if ($prev_tokens[1] && isset(self::$classcalls[$prev_tokens[1][0]])) {
if (null !== $prev_tokens[1] && isset(self::$classcalls[$prev_tokens[1][0]])) {
continue;
}
} else {
if (!$prev_tokens[1] || T_DOUBLE_COLON !== $prev_tokens[1][0]) {
if (null === $prev_tokens[1] || T_DOUBLE_COLON !== $prev_tokens[1][0]) {
continue;
}
if (!$prev_tokens[0] || !isset(self::$namespace[$prev_tokens[0][0]])) {
if (null === $prev_tokens[0] || !isset(self::$namespace[$prev_tokens[0][0]])) {
continue;
}
// All self::$namespace tokens are T_ constants
/** @psalm-var PhpTokenArray $prev_tokens[0] */
/**
* @psalm-var PhpTokenArray $prev_tokens[0]
* Psalm bug #746 (wontfix)
*/
$ns = \explode('\\', \strtolower($prev_tokens[0][1]));
if (\end($ns) !== $class) {
@ -269,7 +285,7 @@ class CallFinder
}
}
$inner_cursor = $cursor;
$last_line = $token[2];
$depth = 1; // The depth respective to the function call
$offset = $nextReal + 1; // The start of the function call
$instring = false; // Whether we're in a string or not
@ -283,10 +299,8 @@ class CallFinder
while (isset($tokens[$offset])) {
$token = $tokens[$offset];
// Ensure that the $inner_cursor is correct and
// that $token is either a T_ constant or a string
if (\is_array($token)) {
$inner_cursor += \substr_count($token[1], "\n");
$last_line = $token[2];
}
if (!isset(self::$ignore[$token[0]]) && !isset($down[$token[0]])) {
@ -312,7 +326,7 @@ class CallFinder
}
$shortparam[] = $token;
}
} elseif ('"' === $token[0]) {
} elseif ('"' === $token || 'b"' === $token) {
// Strings use the same symbol for up and down, but we can
// only ever be inside one string, so just use a bool for that
if ($instring) {
@ -326,7 +340,7 @@ class CallFinder
$instring = !$instring;
$shortparam[] = '"';
$shortparam[] = $token;
} elseif (1 === $depth) {
if (',' === $token[0]) {
$params[] = [
@ -336,8 +350,19 @@ class CallFinder
$shortparam = [];
$paramrealtokens = false;
$param_start = $offset + 1;
} elseif (T_CONSTANT_ENCAPSED_STRING === $token[0] && \strlen($token[1]) > 2) {
$shortparam[] = $token[1][0].'...'.$token[1][0];
} elseif (T_CONSTANT_ENCAPSED_STRING === $token[0]) {
$quote = $token[1][0];
if ('b' === $quote) {
$quote = $token[1][1];
if (\strlen($token[1]) > 3) {
$token[1] = 'b'.$quote.'...'.$quote;
}
} else {
if (\strlen($token[1]) > 2) {
$token[1] = $quote.'...'.$quote;
}
}
$shortparam[] = $token;
} else {
$shortparam[] = $token;
}
@ -360,15 +385,21 @@ class CallFinder
// If we're not passed (or at) the line at the end
// of the function call, we're too early so skip it
if ($inner_cursor < $line) {
continue;
// Only applies to < 8.2 since we check line explicitly above that
if (!KINT_PHP82 && $last_line < $line) {
continue; // @codeCoverageIgnore
}
// Format the final output parameters
foreach ($params as &$param) {
$name = self::tokensFormatted($param['short']);
$formatted_parameters = [];
// Format the final output parameters
foreach ($params as $param) {
$name = self::tokensFormatted($param['short']);
$path = self::tokensToString(self::tokensTrim($param['full']));
$expression = false;
$literal = false;
$new_without_parens = false;
foreach ($name as $token) {
if (self::tokenIsOperator($token)) {
$expression = true;
@ -376,16 +407,79 @@ class CallFinder
}
}
$param = [
'name' => self::tokensToString($name),
'path' => self::tokensToString(self::tokensTrim($param['full'])),
// As of 8.4 new is only an expression when parentheses are
// omitted. In that case we can cheat and add them ourselves.
//
// > PHP interprets the first expression after new as a class name
// per https://wiki.php.net/rfc/new_without_parentheses
if (KINT_PHP84 && !$expression && T_NEW === $name[0][0]) {
$had_name_token = false;
$new_without_parens = true;
foreach ($name as $token) {
if (T_NEW === $token[0]) {
continue;
}
if (isset(self::$ignore[$token[0]])) {
continue;
}
if (T_CLASS === $token[0]) {
$new_without_parens = false;
break;
}
if ('(' === $token && $had_name_token) {
$new_without_parens = false;
break;
}
$had_name_token = true;
}
}
if (!$expression && 1 === \count($name)) {
switch ($name[0][0]) {
case T_CONSTANT_ENCAPSED_STRING:
case T_LNUMBER:
case T_DNUMBER:
$literal = true;
break;
case T_STRING:
switch (\strtolower($name[0][1])) {
case 'null':
case 'true':
case 'false':
$literal = true;
}
}
$name = self::tokensToString($name);
} else {
$name = self::tokensToString($name);
if (!$expression) {
switch (\strtolower($name)) {
case 'array()':
case '[]':
$literal = true;
break;
}
}
}
$formatted_parameters[] = [
'name' => $name,
'path' => $path,
'expression' => $expression,
'literal' => $literal,
'new_without_parens' => $new_without_parens,
];
}
// Skip first-class callables
/** @psalm-var list<array{name: string, path: string, expression: bool}> $params */
if (KINT_PHP81 && 1 === \count($params) && '...' === \reset($params)['path']) {
if (KINT_PHP81 && 1 === \count($formatted_parameters) && '...' === \reset($formatted_parameters)['path']) {
continue;
}
@ -418,7 +512,7 @@ class CallFinder
}
$function_calls[] = [
'parameters' => $params,
'parameters' => $formatted_parameters,
'modifiers' => $mods,
];
}
@ -426,9 +520,6 @@ class CallFinder
return $function_calls;
}
/**
* @psalm-param PhpToken[] $tokens
*/
private static function realTokenIndex(array $tokens, int $index): ?int
{
++$index;
@ -457,8 +548,13 @@ class CallFinder
}
/**
* @psalm-param PhpToken[] $tokens
* @psalm-param PhpToken $token The token to check
*/
private static function tokenPreserveWhitespace($token): bool
{
return self::tokenIsOperator($token) || isset(self::$preserve_spaces[$token[0]]);
}
private static function tokensToString(array $tokens): string
{
$out = '';
@ -474,9 +570,6 @@ class CallFinder
return $out;
}
/**
* @psalm-param PhpToken[] $tokens
*/
private static function tokensTrim(array $tokens): array
{
foreach ($tokens as $index => $token) {
@ -500,11 +593,6 @@ class CallFinder
return \array_reverse($tokens);
}
/**
* @psalm-param PhpToken[] $tokens
*
* @psalm-return PhpToken[]
*/
private static function tokensFormatted(array $tokens): array
{
$tokens = self::tokensTrim($tokens);
@ -519,7 +607,7 @@ class CallFinder
$last = null;
if (T_FUNCTION === $tokens[0][0] ||
(KINT_PHP74 && T_FN === $tokens[0][0]) ||
T_FN === $tokens[0][0] ||
(KINT_PHP80 && T_MATCH === $tokens[0][0])
) {
$ignorestrip = true;
@ -538,21 +626,24 @@ class CallFinder
}
$next = $tokens[$next];
/** @psalm-var PhpToken $last */
/**
* @psalm-var PhpToken $last
* Since we call tokensTrim we know we can't be here without a $last
*/
if ($attribute && ']' === $last[0]) {
$attribute = false;
} elseif (!$ignorestrip && isset(self::$strip[$last[0]]) && !self::tokenIsOperator($next)) {
} elseif (!$ignorestrip && isset(self::$strip[$last[0]]) && !self::tokenPreserveWhitespace($next)) {
continue;
}
if (!$ignorestrip && isset(self::$strip[$next[0]]) && $last && !self::tokenIsOperator($last)) {
if (!$ignorestrip && isset(self::$strip[$next[0]]) && !self::tokenPreserveWhitespace($last)) {
continue;
}
$token = ' ';
$token[1] = ' ';
$space = true;
} else {
if (KINT_PHP80 && $last && T_ATTRIBUTE == $last[0]) {
if (KINT_PHP80 && null !== $last && T_ATTRIBUTE === $last[0]) {
$attribute = true;
}

View File

@ -29,7 +29,7 @@ namespace Kint;
use Kint\Parser\Parser;
use Kint\Renderer\RendererInterface;
use Kint\Zval\Value;
use Kint\Value\Context\ContextInterface;
interface FacadeInterface
{
@ -42,8 +42,8 @@ interface FacadeInterface
/**
* Renders a list of vars including the pre and post renders.
*
* @param array $vars Data to dump
* @param Value[] $base The base value objects
* @param array $vars Data to dump
* @param ContextInterface[] $base The base contexts
*/
public function dumpAll(array $vars, array $base): string;
}

View File

@ -31,12 +31,22 @@ use InvalidArgumentException;
use Kint\Parser\ConstructablePluginInterface;
use Kint\Parser\Parser;
use Kint\Parser\PluginInterface;
use Kint\Renderer\ConstructableRendererInterface;
use Kint\Renderer\RendererInterface;
use Kint\Renderer\TextRenderer;
use Kint\Zval\Value;
use Kint\Value\Context\BaseContext;
use Kint\Value\Context\ContextInterface;
use Kint\Value\UninitializedValue;
/**
* @psalm-consistent-constructor
* Psalm bug #8523
*
* @psalm-import-type CallParameter from CallFinder
*
* @psalm-type KintMode = Kint::MODE_*|bool
*
* @psalm-api
*/
class Kint implements FacadeInterface
{
@ -51,134 +61,111 @@ class Kint implements FacadeInterface
* false: Disabled
* true: Enabled, default mode selection
* other: Manual mode selection
*
* @psalm-var KintMode
*/
public static $enabled_mode = true;
/**
* Default mode.
*
* @var string
* @psalm-var KintMode
*/
public static $mode_default = self::MODE_RICH;
/**
* Default mode in CLI with cli_detection on.
*
* @var string
* @psalm-var KintMode
*/
public static $mode_default_cli = self::MODE_CLI;
/**
* @var bool Return output instead of echoing
*/
public static $return;
/**
* @var string format of the link to the source file in trace entries.
*
* Use %f for file path, %l for line number.
*
* [!] EXAMPLE (works with for phpStorm and RemoteCall Plugin):
*
* Kint::$file_link_format = 'http://localhost:8091/?message=%f:%l';
*/
public static $file_link_format = '';
/**
* @var bool whether to display where kint was called from
*/
public static $display_called_from = true;
/**
* @var array base directories of your application that will be displayed instead of the full path.
*
* Keys are paths, values are replacement strings
*
* [!] EXAMPLE (for Laravel 5):
*
* Kint::$app_root_dirs = [
* base_path() => '<BASE>',
* app_path() => '<APP>',
* config_path() => '<CONFIG>',
* database_path() => '<DATABASE>',
* public_path() => '<PUBLIC>',
* resource_path() => '<RESOURCE>',
* storage_path() => '<STORAGE>',
* ];
*
* Defaults to [$_SERVER['DOCUMENT_ROOT'] => '<ROOT>']
*/
public static $app_root_dirs = [];
/**
* @var int depth limit for array/object traversal. 0 for no limit
*/
public static $depth_limit = 7;
/**
* @var bool expand all trees by default for rich view
*/
public static $expanded = false;
/**
* @var bool enable detection when Kint is command line.
*
* Formats output with whitespace only; does not HTML-escape it
*/
public static $cli_detection = true;
public static bool $cli_detection = true;
/**
* @var bool Return output instead of echoing
*/
public static bool $return = false;
/**
* @var int depth limit for array/object traversal. 0 for no limit
*/
public static int $depth_limit = 7;
/**
* @var bool expand all trees by default for rich view
*/
public static bool $expanded = false;
/**
* @var bool whether to display where kint was called from
*/
public static bool $display_called_from = true;
/**
* @var array Kint aliases. Add debug functions in Kint wrappers here to fix modifiers and backtraces
*/
public static $aliases = [
['Kint\\Kint', 'dump'],
['Kint\\Kint', 'trace'],
['Kint\\Kint', 'dumpAll'],
public static array $aliases = [
[self::class, 'dump'],
[self::class, 'trace'],
[self::class, 'dumpAll'],
];
/**
* @psalm-var class-string[] Array of modes to renderer class names
* @psalm-var array<RendererInterface|class-string<ConstructableRendererInterface>>
*
* Array of modes to renderer class names
*/
public static $renderers = [
self::MODE_RICH => \Kint\Renderer\RichRenderer::class,
self::MODE_PLAIN => \Kint\Renderer\PlainRenderer::class,
self::MODE_TEXT => \Kint\Renderer\TextRenderer::class,
self::MODE_CLI => \Kint\Renderer\CliRenderer::class,
public static array $renderers = [
self::MODE_RICH => Renderer\RichRenderer::class,
self::MODE_PLAIN => Renderer\PlainRenderer::class,
self::MODE_TEXT => TextRenderer::class,
self::MODE_CLI => Renderer\CliRenderer::class,
];
/**
* @psalm-var class-string[]
* @psalm-var array<PluginInterface|class-string<ConstructablePluginInterface>>
*/
public static $plugins = [
public static array $plugins = [
\Kint\Parser\ArrayLimitPlugin::class,
\Kint\Parser\ArrayObjectPlugin::class,
\Kint\Parser\Base64Plugin::class,
\Kint\Parser\BinaryPlugin::class,
\Kint\Parser\BlacklistPlugin::class,
\Kint\Parser\ClassHooksPlugin::class,
\Kint\Parser\ClassMethodsPlugin::class,
\Kint\Parser\ClassStaticsPlugin::class,
\Kint\Parser\ClassStringsPlugin::class,
\Kint\Parser\ClosurePlugin::class,
\Kint\Parser\ColorPlugin::class,
\Kint\Parser\DateTimePlugin::class,
\Kint\Parser\DomPlugin::class,
\Kint\Parser\EnumPlugin::class,
\Kint\Parser\FsPathPlugin::class,
\Kint\Parser\HtmlPlugin::class,
\Kint\Parser\IteratorPlugin::class,
\Kint\Parser\JsonPlugin::class,
\Kint\Parser\MicrotimePlugin::class,
\Kint\Parser\MysqliPlugin::class,
// \Kint\Parser\SerializePlugin::class,
\Kint\Parser\SimpleXMLElementPlugin::class,
\Kint\Parser\SplFileInfoPlugin::class,
\Kint\Parser\SplObjectStoragePlugin::class,
\Kint\Parser\StreamPlugin::class,
\Kint\Parser\TablePlugin::class,
\Kint\Parser\ThrowablePlugin::class,
\Kint\Parser\TimestampPlugin::class,
\Kint\Parser\ToStringPlugin::class,
\Kint\Parser\TracePlugin::class,
\Kint\Parser\XmlPlugin::class,
];
protected static $plugin_pool = [];
protected $parser;
protected $renderer;
protected Parser $parser;
protected RendererInterface $renderer;
public function __construct(Parser $p, RendererInterface $r)
{
@ -210,7 +197,7 @@ class Kint implements FacadeInterface
{
$this->renderer->setStatics($statics);
$this->parser->setDepthLimit(isset($statics['depth_limit']) ? $statics['depth_limit'] : 0);
$this->parser->setDepthLimit($statics['depth_limit'] ?? 0);
$this->parser->clearPlugins();
if (!isset($statics['plugins'])) {
@ -222,19 +209,22 @@ class Kint implements FacadeInterface
foreach ($statics['plugins'] as $plugin) {
if ($plugin instanceof PluginInterface) {
$plugins[] = $plugin;
} elseif (\is_string($plugin) && \is_subclass_of($plugin, ConstructablePluginInterface::class)) {
if (!isset(static::$plugin_pool[$plugin])) {
$p = new $plugin();
static::$plugin_pool[$plugin] = $p;
}
$plugins[] = static::$plugin_pool[$plugin];
} elseif (\is_string($plugin) && \is_a($plugin, ConstructablePluginInterface::class, true)) {
$plugins[] = new $plugin($this->parser);
}
}
$plugins = $this->renderer->filterParserPlugins($plugins);
foreach ($plugins as $plugin) {
$this->parser->addPlugin($plugin);
try {
$this->parser->addPlugin($plugin);
} catch (InvalidArgumentException $e) {
\trigger_error(
'Plugin '.Utils::errorSanitizeString(\get_class($plugin)).' could not be added to a Kint parser: '.Utils::errorSanitizeString($e->getMessage()),
E_USER_WARNING
);
}
}
}
@ -246,7 +236,7 @@ class Kint implements FacadeInterface
$this->parser->setDepthLimit(0);
}
$this->parser->setCallerClass(isset($info['caller']['class']) ? $info['caller']['class'] : null);
$this->parser->setCallerClass($info['caller']['class'] ?? null);
}
public function dumpAll(array $vars, array $base): string
@ -255,17 +245,17 @@ class Kint implements FacadeInterface
throw new InvalidArgumentException('Kint::dumpAll requires arrays of identical size and keys as arguments');
}
if ([] === $vars) {
return $this->dumpNothing();
}
$output = $this->renderer->preRender();
if ([] === $vars) {
$output .= $this->renderer->renderNothing();
}
foreach ($vars as $key => $arg) {
if (!$base[$key] instanceof Value) {
throw new InvalidArgumentException('Kint::dumpAll requires all elements of the second argument to be Value instances');
foreach ($vars as $key => $_) {
if (!$base[$key] instanceof ContextInterface) {
throw new InvalidArgumentException('Kint::dumpAll requires all elements of the second argument to be ContextInterface instances');
}
$output .= $this->dumpVar($arg, $base[$key]);
$output .= $this->dumpVar($vars[$key], $base[$key]);
}
$output .= $this->renderer->postRender();
@ -273,16 +263,24 @@ class Kint implements FacadeInterface
return $output;
}
protected function dumpNothing(): string
{
$output = $this->renderer->preRender();
$output .= $this->renderer->render(new UninitializedValue(new BaseContext('No argument')));
$output .= $this->renderer->postRender();
return $output;
}
/**
* Dumps and renders a var.
*
* @param mixed &$var Data to dump
* @param Value $base Base object
*/
protected function dumpVar(&$var, Value $base): string
protected function dumpVar(&$var, ContextInterface $c): string
{
return $this->renderer->render(
$this->parser->parse($var, $base)
$this->parser->parse($var, $c)
);
}
@ -295,13 +293,11 @@ class Kint implements FacadeInterface
{
return [
'aliases' => static::$aliases,
'app_root_dirs' => static::$app_root_dirs,
'cli_detection' => static::$cli_detection,
'depth_limit' => static::$depth_limit,
'display_called_from' => static::$display_called_from,
'enabled_mode' => static::$enabled_mode,
'expanded' => static::$expanded,
'file_link_format' => static::$file_link_format,
'mode_default' => static::$mode_default,
'mode_default_cli' => static::$mode_default_cli,
'plugins' => static::$plugins,
@ -335,67 +331,57 @@ class Kint implements FacadeInterface
return null;
}
/** @psalm-var class-string[] $statics['renderers'] */
if (isset($statics['renderers'][$mode]) && \is_subclass_of($statics['renderers'][$mode], RendererInterface::class)) {
$renderer = new $statics['renderers'][$mode]();
} else {
$renderer = new TextRenderer();
$renderer = null;
if (isset($statics['renderers'][$mode])) {
if ($statics['renderers'][$mode] instanceof RendererInterface) {
$renderer = $statics['renderers'][$mode];
}
if (\is_a($statics['renderers'][$mode], ConstructableRendererInterface::class, true)) {
$renderer = new $statics['renderers'][$mode]();
}
}
$renderer ??= new TextRenderer();
return new static(new Parser(), $renderer);
}
/**
* Creates base objects given parameter info.
* Creates base contexts given parameter info.
*
* @param array $params Parameters as returned from getCallInfo
* @param int $argc Number of arguments the helper was called with
* @psalm-param list<CallParameter> $params
*
* @return Value[] Base objects for the arguments
* @return BaseContext[] Base contexts for the arguments
*/
public static function getBasesFromParamInfo(array $params, int $argc): array
{
static $blacklist = [
'null',
'true',
'false',
'array(...)',
'array()',
'[...]',
'[]',
'(...)',
'()',
'"..."',
'b"..."',
"'...'",
"b'...'",
];
$params = \array_values($params);
$bases = [];
for ($i = 0; $i < $argc; ++$i) {
$param = $params[$i] ?? null;
if (!isset($param['name']) || \is_numeric($param['name'])) {
$name = null;
} elseif (\in_array(\strtolower($param['name']), $blacklist, true)) {
$name = null;
if (!empty($param['literal'])) {
$name = 'literal';
} else {
$name = $param['name'];
$name = $param['name'] ?? '$'.$i;
}
if (isset($param['path'])) {
$access_path = $param['path'];
if (!empty($param['expression'])) {
if ($param['expression']) {
$access_path = '('.$access_path.')';
} elseif ($param['new_without_parens']) {
$access_path .= '()';
}
} else {
$access_path = '$'.$i;
}
$bases[] = Value::blank($name, $access_path);
$base = new BaseContext($name);
$base->access_path = $access_path;
$bases[] = $base;
}
return $bases;
@ -411,6 +397,8 @@ class Kint implements FacadeInterface
* @param array $args Arguments
*
* @return array Call info
*
* @psalm-param list<non-empty-array> $trace
*/
public static function getCallInfo(array $aliases, array $trace, array $args): array
{
@ -419,7 +407,7 @@ class Kint implements FacadeInterface
$caller = null;
$miniTrace = [];
foreach ($trace as $index => $frame) {
foreach ($trace as $frame) {
if (Utils::traceFrameIsListed($frame, $aliases)) {
$found = true;
$miniTrace = [];
@ -456,7 +444,7 @@ class Kint implements FacadeInterface
'trace' => $miniTrace,
];
if ($call) {
if (null !== $call) {
$ret['params'] = $call['parameters'];
$ret['modifiers'] = $call['modifiers'];
}
@ -477,7 +465,7 @@ class Kint implements FacadeInterface
return 0;
}
Utils::normalizeAliases(static::$aliases);
static::$aliases = Utils::normalizeAliases(static::$aliases);
$call_info = static::getCallInfo(static::$aliases, \debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), []);
@ -514,10 +502,9 @@ class Kint implements FacadeInterface
\array_shift($trimmed_trace);
$output = $kintstance->dumpAll(
[$trimmed_trace],
[Value::blank('Kint\\Kint::trace()', 'debug_backtrace()')]
);
$base = new BaseContext('Kint\\Kint::trace()');
$base->access_path = 'debug_backtrace()';
$output = $kintstance->dumpAll([$trimmed_trace], [$base]);
if (static::$return || \in_array('@', $call_info['modifiers'], true)) {
return $output;
@ -537,7 +524,7 @@ class Kint implements FacadeInterface
*
* Functionally equivalent to Kint::dump(1) or Kint::dump(debug_backtrace())
*
* @psalm-param array ...$args
* @psalm-param mixed ...$args
*
* @return int|string
*/
@ -547,7 +534,7 @@ class Kint implements FacadeInterface
return 0;
}
Utils::normalizeAliases(static::$aliases);
static::$aliases = Utils::normalizeAliases(static::$aliases);
$call_info = static::getCallInfo(static::$aliases, \debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), $args);
@ -587,54 +574,6 @@ class Kint implements FacadeInterface
return 0;
}
/**
* generic path display callback, can be configured in app_root_dirs; purpose is
* to show relevant path info and hide as much of the path as possible.
*/
public static function shortenPath(string $file): string
{
$file = \array_values(\array_filter(\explode('/', \str_replace('\\', '/', $file)), 'strlen'));
$longest_match = 0;
$match = '/';
foreach (static::$app_root_dirs as $path => $alias) {
/** @psalm-var string $path */
if (empty($path)) {
continue;
}
$path = \array_values(\array_filter(\explode('/', \str_replace('\\', '/', $path)), 'strlen'));
if (\array_slice($file, 0, \count($path)) === $path && \count($path) > $longest_match) {
$longest_match = \count($path);
$match = $alias;
}
}
if ($longest_match) {
$file = \array_merge([$match], \array_slice($file, $longest_match));
return \implode('/', $file);
}
// fallback to find common path with Kint dir
$kint = \array_values(\array_filter(\explode('/', \str_replace('\\', '/', KINT_DIR)), 'strlen'));
foreach ($file as $i => $part) {
if (!isset($kint[$i]) || $kint[$i] !== $part) {
return ($i ? '.../' : '/').\implode('/', \array_slice($file, $i));
}
}
return '/'.\implode('/', $file);
}
public static function getIdeLink(string $file, int $line): string
{
return \str_replace(['%f', '%l'], [$file, $line], static::$file_link_format);
}
/**
* Returns specific function call info from a stack trace frame, or null if no match could be found.
*
@ -648,7 +587,7 @@ class Kint implements FacadeInterface
if (
!isset($frame['file'], $frame['line'], $frame['function']) ||
!\is_readable($frame['file']) ||
!$source = \file_get_contents($frame['file'])
false === ($source = \file_get_contents($frame['file']))
) {
return null;
}
@ -686,6 +625,8 @@ class Kint implements FacadeInterface
'name' => \substr($param['name'], 3).'['.\var_export($key, true).']',
'path' => \substr($param['path'], 3).'['.\var_export($key, true).']',
'expression' => false,
'literal' => false,
'new_without_parens' => false,
];
}
} else {
@ -696,6 +637,8 @@ class Kint implements FacadeInterface
'name' => 'array_values('.\substr($param['name'], 3).')['.$j.']',
'path' => 'array_values('.\substr($param['path'], 3).')['.$j.']',
'expression' => false,
'literal' => false,
'new_without_parens' => false,
];
}
}

View File

@ -27,19 +27,22 @@ declare(strict_types=1);
namespace Kint\Parser;
/**
* @psalm-consistent-constructor
*/
abstract class AbstractPlugin implements ConstructablePluginInterface
{
protected $parser;
private Parser $parser;
public function __construct()
public function __construct(Parser $parser)
{
$this->parser = $parser;
}
public function setParser(Parser $p): void
{
$this->parser = $p;
}
protected function getParser(): Parser
{
return $this->parser;
}
}

View File

@ -29,30 +29,43 @@ namespace Kint\Parser;
use InvalidArgumentException;
use Kint\Utils;
use Kint\Zval\Value;
use Kint\Value\AbstractValue;
use Kint\Value\ArrayValue;
use Kint\Value\Context\BaseContext;
use Kint\Value\Context\ContextInterface;
use Kint\Value\Representation\ContainerRepresentation;
use Kint\Value\Representation\ProfileRepresentation;
use Kint\Value\Representation\ValueRepresentation;
class ArrayLimitPlugin extends AbstractPlugin
class ArrayLimitPlugin extends AbstractPlugin implements PluginBeginInterface
{
/**
* Maximum size of arrays before limiting.
*
* @var int
*/
public static $trigger = 1000;
public static int $trigger = 1000;
/**
* Maximum amount of items to show in a limited array.
*
* @var int
*/
public static $limit = 50;
public static int $limit = 50;
/**
* Don't limit arrays with string keys.
*
* @var bool
*/
public static $numeric_only = true;
public static bool $numeric_only = true;
public function __construct(Parser $p)
{
if (self::$limit < 0) {
throw new InvalidArgumentException('ArrayLimitPlugin::$limit can not be lower than 0');
}
if (self::$limit >= self::$trigger) {
throw new InvalidArgumentException('ArrayLimitPlugin::$limit can not be lower than ArrayLimitPlugin::$trigger');
}
parent::__construct($p);
}
public function getTypes(): array
{
@ -64,80 +77,92 @@ class ArrayLimitPlugin extends AbstractPlugin
return Parser::TRIGGER_BEGIN;
}
public function parse(&$var, Value &$o, int $trigger): void
public function parseBegin(&$var, ContextInterface $c): ?AbstractValue
{
if (self::$limit >= self::$trigger) {
throw new InvalidArgumentException('ArrayLimitPlugin::$limit can not be lower than ArrayLimitPlugin::$trigger');
$parser = $this->getParser();
$pdepth = $parser->getDepthLimit();
if (!$pdepth) {
return null;
}
$depth = $this->parser->getDepthLimit();
$cdepth = $c->getDepth();
if (!$depth) {
return;
}
if ($o->depth >= $depth - 1) {
return;
if ($cdepth >= $pdepth - 1) {
return null;
}
if (\count($var) < self::$trigger) {
return;
return null;
}
if (self::$numeric_only && Utils::isAssoc($var)) {
return;
return null;
}
$base = clone $o;
$base->depth = $depth - 1;
$obj = $this->parser->parse($var, $base);
$slice = \array_slice($var, 0, self::$limit, true);
$array = $parser->parse($slice, $c);
if ('array' != $obj->type) {
return; // @codeCoverageIgnore
if (!$array instanceof ArrayValue) {
return null;
}
$obj->depth = $o->depth;
$i = 0;
$base = new BaseContext($c->getName());
$base->depth = $pdepth - 1;
$base->access_path = $c->getAccessPath();
foreach ($obj->value->contents as $child) {
// We only bother setting the correct depth for the first child,
// any deeper children should be cancelled by the depth limit
$child->depth = $o->depth + 1;
$this->recalcDepthLimit($child);
$slice = \array_slice($var, self::$limit, null, true);
$slice = $parser->parse($slice, $base);
if (!$slice instanceof ArrayValue) {
return null;
}
$var2 = \array_slice($var, 0, self::$limit, true);
$base = clone $o;
$slice = $this->parser->parse($var2, $base);
foreach ($slice->getContents() as $child) {
$this->replaceDepthLimit($child, $cdepth + 1);
}
\array_splice($obj->value->contents, 0, self::$limit, $slice->value->contents);
$out = new ArrayValue($c, \count($var), \array_merge($array->getContents(), $slice->getContents()));
$out->flags = $array->flags;
$o = $obj;
// Explicitly copy over profile plugin
$arrayp = $array->getRepresentation('profiling');
$slicep = $slice->getRepresentation('profiling');
if ($arrayp instanceof ProfileRepresentation && $slicep instanceof ProfileRepresentation) {
$out->addRepresentation(new ProfileRepresentation($arrayp->complexity + $slicep->complexity));
}
$this->parser->haltParse();
// Add contents. Check is in case some bad plugin empties both $slice and $array
if ($contents = $out->getContents()) {
$out->addRepresentation(new ContainerRepresentation('Contents', $contents, null, true));
}
return $out;
}
protected function recalcDepthLimit(Value $o): void
protected function replaceDepthLimit(AbstractValue $v, int $depth): void
{
$hintkey = \array_search('depth_limit', $o->hints, true);
if (false !== $hintkey) {
$o->hints[$hintkey] = 'array_limit';
$c = $v->getContext();
if ($c instanceof BaseContext) {
$c->depth = $depth;
}
$reps = $o->getRepresentations();
if ($o->value) {
$reps[] = $o->value;
$pdepth = $this->getParser()->getDepthLimit();
if (($v->flags & AbstractValue::FLAG_DEPTH_LIMIT) && $pdepth && $depth < $pdepth) {
$v->flags = $v->flags & ~AbstractValue::FLAG_DEPTH_LIMIT | AbstractValue::FLAG_ARRAY_LIMIT;
}
$reps = $v->getRepresentations();
foreach ($reps as $rep) {
if ($rep->contents instanceof Value) {
$this->recalcDepthLimit($rep->contents);
} elseif (\is_array($rep->contents)) {
foreach ($rep->contents as $child) {
if ($child instanceof Value) {
$this->recalcDepthLimit($child);
}
if ($rep instanceof ContainerRepresentation) {
foreach ($rep->getContents() as $child) {
$this->replaceDepthLimit($child, $depth + 1);
}
} elseif ($rep instanceof ValueRepresentation) {
$this->replaceDepthLimit($rep->getValue(), $depth + 1);
}
}
}

View File

@ -28,9 +28,10 @@ declare(strict_types=1);
namespace Kint\Parser;
use ArrayObject;
use Kint\Zval\Value;
use Kint\Value\AbstractValue;
use Kint\Value\Context\ContextInterface;
class ArrayObjectPlugin extends AbstractPlugin
class ArrayObjectPlugin extends AbstractPlugin implements PluginBeginInterface
{
public function getTypes(): array
{
@ -42,24 +43,26 @@ class ArrayObjectPlugin extends AbstractPlugin
return Parser::TRIGGER_BEGIN;
}
public function parse(&$var, Value &$o, int $trigger): void
public function parseBegin(&$var, ContextInterface $c): ?AbstractValue
{
if (!$var instanceof ArrayObject) {
return;
return null;
}
$flags = $var->getFlags();
if (ArrayObject::STD_PROP_LIST === $flags) {
return;
return null;
}
$parser = $this->getParser();
$var->setFlags(ArrayObject::STD_PROP_LIST);
$o = $this->parser->parse($var, $o);
$v = $parser->parse($var, $c);
$var->setFlags($flags);
$this->parser->haltParse();
return $v;
}
}

View File

@ -27,24 +27,22 @@ declare(strict_types=1);
namespace Kint\Parser;
use Kint\Zval\Representation\Representation;
use Kint\Zval\Value;
use Kint\Value\AbstractValue;
use Kint\Value\Context\BaseContext;
use Kint\Value\Representation\ValueRepresentation;
use Kint\Value\StringValue;
class Base64Plugin extends AbstractPlugin
class Base64Plugin extends AbstractPlugin implements PluginCompleteInterface
{
/**
* The minimum length before a string will be considered for base64 decoding.
*
* @var int
*/
public static $min_length_hard = 16;
public static int $min_length_hard = 16;
/**
* The minimum length before the base64 decoding will take precedence.
*
* @var int
*/
public static $min_length_soft = 50;
public static int $min_length_soft = 50;
public function getTypes(): array
{
@ -56,41 +54,50 @@ class Base64Plugin extends AbstractPlugin
return Parser::TRIGGER_SUCCESS;
}
public function parse(&$var, Value &$o, int $trigger): void
public function parseComplete(&$var, AbstractValue $v, int $trigger): AbstractValue
{
if (\strlen($var) < self::$min_length_hard || \strlen($var) % 4) {
return;
return $v;
}
if (\preg_match('/^[A-Fa-f0-9]+$/', $var)) {
return;
return $v;
}
if (!\preg_match('/^[A-Za-z0-9+\\/=]+$/', $var)) {
return;
return $v;
}
$data = \base64_decode($var, true);
if (false === $data) {
return;
return $v;
}
$base_obj = new Value();
$base_obj->depth = $o->depth + 1;
$base_obj->name = 'base64_decode('.$o->name.')';
$c = $v->getContext();
if ($o->access_path) {
$base_obj->access_path = 'base64_decode('.$o->access_path.')';
$base = new BaseContext('base64_decode('.$c->getName().')');
$base->depth = $c->getDepth() + 1;
if (null !== ($ap = $c->getAccessPath())) {
$base->access_path = 'base64_decode('.$ap.')';
}
$r = new Representation('Base64');
$r->contents = $this->parser->parse($data, $base_obj);
$data = $this->getParser()->parse($data, $base);
$data->flags |= AbstractValue::FLAG_GENERATED;
if (!$data instanceof StringValue || false === $data->getEncoding()) {
return $v;
}
$r = new ValueRepresentation('Base64', $data);
if (\strlen($var) > self::$min_length_soft) {
$o->addRepresentation($r, 0);
$v->addRepresentation($r, 0);
} else {
$o->addRepresentation($r);
$v->addRepresentation($r);
}
return $v;
}
}

View File

@ -27,10 +27,11 @@ declare(strict_types=1);
namespace Kint\Parser;
use Kint\Zval\BlobValue;
use Kint\Zval\Value;
use Kint\Value\AbstractValue;
use Kint\Value\Representation\BinaryRepresentation;
use Kint\Value\StringValue;
class BinaryPlugin extends AbstractPlugin
class BinaryPlugin extends AbstractPlugin implements PluginCompleteInterface
{
public function getTypes(): array
{
@ -42,10 +43,12 @@ class BinaryPlugin extends AbstractPlugin
return Parser::TRIGGER_SUCCESS;
}
public function parse(&$var, Value &$o, int $trigger): void
public function parseComplete(&$var, AbstractValue $v, int $trigger): AbstractValue
{
if (!$o instanceof BlobValue || !\in_array($o->encoding, ['ASCII', 'UTF-8'], true)) {
$o->value->hints[] = 'binary';
if ($v instanceof StringValue && false === $v->getEncoding()) {
$v->addRepresentation(new BinaryRepresentation($v->getValue(), true), 0);
}
return $v;
}
}

View File

@ -27,25 +27,30 @@ declare(strict_types=1);
namespace Kint\Parser;
use Kint\Zval\InstanceValue;
use Kint\Zval\Value;
use Kint\Value\AbstractValue;
use Kint\Value\Context\ContextInterface;
use Kint\Value\InstanceValue;
use Psr\Container\ContainerInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
class BlacklistPlugin extends AbstractPlugin
class BlacklistPlugin extends AbstractPlugin implements PluginBeginInterface
{
/**
* List of classes and interfaces to blacklist.
*
* @var array
* @var class-string[]
*/
public static $blacklist = [];
public static array $blacklist = [];
/**
* List of classes and interfaces to blacklist except when dumped directly.
*
* @var array
* @var class-string[]
*/
public static $shallow_blacklist = [ContainerInterface::class];
public static array $shallow_blacklist = [
ContainerInterface::class,
EventDispatcherInterface::class,
];
public function getTypes(): array
{
@ -57,45 +62,35 @@ class BlacklistPlugin extends AbstractPlugin
return Parser::TRIGGER_BEGIN;
}
public function parse(&$var, Value &$o, int $trigger): void
public function parseBegin(&$var, ContextInterface $c): ?AbstractValue
{
foreach (self::$blacklist as $class) {
if ($var instanceof $class) {
$this->blacklistValue($var, $o);
return;
return $this->blacklistValue($var, $c);
}
}
if ($o->depth <= 0) {
return;
if ($c->getDepth() <= 0) {
return null;
}
foreach (self::$shallow_blacklist as $class) {
if ($var instanceof $class) {
$this->blacklistValue($var, $o);
return;
return $this->blacklistValue($var, $c);
}
}
return null;
}
/**
* @param object &$var
*/
protected function blacklistValue(&$var, Value &$o): void
protected function blacklistValue(&$var, ContextInterface $c): InstanceValue
{
$object = new InstanceValue();
$object->transplant($o);
$object->classname = \get_class($var);
$object->spl_object_hash = \spl_object_hash($var);
$object->clearRepresentations();
$object->value = null;
$object->size = null;
$object->hints[] = 'blacklist';
$object = new InstanceValue($c, \get_class($var), \spl_object_hash($var), \spl_object_id($var));
$object->flags |= AbstractValue::FLAG_BLACKLIST;
$o = $object;
$this->parser->haltParse();
return $object;
}
}

View File

@ -0,0 +1,122 @@
<?php
declare(strict_types=1);
/*
* The MIT License (MIT)
*
* Copyright (c) 2013 Jonathan Vollebregt (jnvsor@gmail.com), Rokas Šleinius (raveren@gmail.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
namespace Kint\Parser;
use Kint\Value\AbstractValue;
use Kint\Value\Context\MethodContext;
use Kint\Value\Context\PropertyContext;
use Kint\Value\DeclaredCallableBag;
use Kint\Value\InstanceValue;
use Kint\Value\MethodValue;
use Kint\Value\Representation\ContainerRepresentation;
use ReflectionProperty;
class ClassHooksPlugin extends AbstractPlugin implements PluginCompleteInterface
{
public static bool $verbose = false;
/** @psalm-var array<class-string, array<string, MethodValue[]>> */
private array $cache = [];
/** @psalm-var array<class-string, array<string, MethodValue[]>> */
private array $cache_verbose = [];
public function getTypes(): array
{
return ['object'];
}
public function getTriggers(): int
{
if (!KINT_PHP84) {
return Parser::TRIGGER_NONE; // @codeCoverageIgnore
}
return Parser::TRIGGER_SUCCESS;
}
public function parseComplete(&$var, AbstractValue $v, int $trigger): AbstractValue
{
if (!$v instanceof InstanceValue) {
return $v;
}
$props = $v->getRepresentation('properties');
if (!$props instanceof ContainerRepresentation) {
return $v;
}
foreach ($props->getContents() as $prop) {
$c = $prop->getContext();
if (!$c instanceof PropertyContext || PropertyContext::HOOK_NONE === $c->hooks) {
continue;
}
$cname = $c->getName();
$cowner = $c->owner_class;
if (!isset($this->cache_verbose[$cowner][$cname])) {
$ref = new ReflectionProperty($cowner, $cname);
$hooks = $ref->getHooks();
foreach ($hooks as $hook) {
if (!self::$verbose && false === $hook->getDocComment()) {
continue;
}
$m = new MethodValue(
new MethodContext($hook),
new DeclaredCallableBag($hook)
);
$this->cache_verbose[$cowner][$cname][] = $m;
if (false !== $hook->getDocComment()) {
$this->cache[$cowner][$cname][] = $m;
}
}
$this->cache[$cowner][$cname] ??= [];
if (self::$verbose) {
$this->cache_verbose[$cowner][$cname] ??= [];
}
}
$cache = self::$verbose ? $this->cache_verbose : $this->cache;
$cache = $cache[$cowner][$cname] ?? [];
if (\count($cache)) {
$prop->addRepresentation(new ContainerRepresentation('Hooks', $cache, 'propertyhooks'));
}
}
return $v;
}
}

View File

@ -27,15 +27,32 @@ declare(strict_types=1);
namespace Kint\Parser;
use Kint\Zval\InstanceValue;
use Kint\Zval\MethodValue;
use Kint\Zval\Representation\Representation;
use Kint\Zval\Value;
use Kint\Value\AbstractValue;
use Kint\Value\Context\MethodContext;
use Kint\Value\DeclaredCallableBag;
use Kint\Value\InstanceValue;
use Kint\Value\MethodValue;
use Kint\Value\Representation\ContainerRepresentation;
use ReflectionClass;
use ReflectionMethod;
class ClassMethodsPlugin extends AbstractPlugin
class ClassMethodsPlugin extends AbstractPlugin implements PluginCompleteInterface
{
private static $cache = [];
public static bool $show_access_path = true;
/**
* Whether to go out of the way to show constructor paths
* when the instance isn't accessible.
*
* Disabling this improves performance.
*/
public static bool $show_constructor_path = false;
/** @psalm-var array<class-string, MethodValue[]> */
private array $instance_cache = [];
/** @psalm-var array<class-string, MethodValue[]> */
private array $static_cache = [];
public function getTypes(): array
{
@ -47,69 +64,162 @@ class ClassMethodsPlugin extends AbstractPlugin
return Parser::TRIGGER_SUCCESS;
}
public function parse(&$var, Value &$o, int $trigger): void
/**
* @psalm-template T of AbstractValue
*
* @psalm-param mixed $var
* @psalm-param T $v
*
* @psalm-return T
*/
public function parseComplete(&$var, AbstractValue $v, int $trigger): AbstractValue
{
$class = \get_class($var);
if (!$v instanceof InstanceValue) {
return $v;
}
// assuming class definition will not change inside one request
if (!isset(self::$cache[$class])) {
$class = $v->getClassName();
$scope = $this->getParser()->getCallerClass();
if ($contents = $this->getCachedMethods($class)) {
if (self::$show_access_path) {
if (null !== $v->getContext()->getAccessPath()) {
// If we have an access path we can generate them for the children
foreach ($contents as $key => $val) {
if ($val->getContext()->isAccessible($scope)) {
$val = clone $val;
$val->getContext()->setAccessPathFromParent($v);
$contents[$key] = $val;
}
}
} elseif (self::$show_constructor_path && isset($contents['__construct'])) {
// __construct is the only exception: The only non-static method
// that can be called without access to the parent instance.
// Technically I guess it really is a static method but so long
// as PHP continues to refer to it as a normal one so will we.
$val = $contents['__construct'];
if ($val->getContext()->isAccessible($scope)) {
$val = clone $val;
$val->getContext()->setAccessPathFromParent($v);
$contents['__construct'] = $val;
}
}
}
$v->addRepresentation(new ContainerRepresentation('Methods', $contents));
}
if ($contents = $this->getCachedStaticMethods($class)) {
$v->addRepresentation(new ContainerRepresentation('Static methods', $contents));
}
return $v;
}
/**
* @psalm-param class-string $class
*
* @psalm-return MethodValue[]
*/
private function getCachedMethods(string $class): array
{
if (!isset($this->instance_cache[$class])) {
$methods = [];
$reflection = new ReflectionClass($class);
$r = new ReflectionClass($class);
foreach ($reflection->getMethods() as $method) {
$methods[] = new MethodValue($method);
$parent_methods = [];
if ($parent = \get_parent_class($class)) {
$parent_methods = $this->getCachedMethods($parent);
}
\usort($methods, ['Kint\\Parser\\ClassMethodsPlugin', 'sort']);
foreach ($r->getMethods() as $mr) {
if ($mr->isStatic()) {
continue;
}
self::$cache[$class] = $methods;
}
$canon_name = \strtolower($mr->name);
if ($mr->isPrivate() && '__construct' !== $canon_name) {
$canon_name = \strtolower($mr->getDeclaringClass()->name).'::'.$canon_name;
}
if (!empty(self::$cache[$class])) {
$rep = new Representation('Available methods', 'methods');
if ($mr->getDeclaringClass()->name === $class) {
$method = new MethodValue(new MethodContext($mr), new DeclaredCallableBag($mr));
$methods[$canon_name] = $method;
unset($parent_methods[$canon_name]);
} elseif (isset($parent_methods[$canon_name])) {
$method = $parent_methods[$canon_name];
unset($parent_methods[$canon_name]);
// Can't cache access paths
foreach (self::$cache[$class] as $m) {
$method = clone $m;
$method->depth = $o->depth + 1;
if (!$method->getContext()->inherited) {
$method = clone $method;
$method->getContext()->inherited = true;
}
if (!$this->parser->childHasPath($o, $method)) {
$method->access_path = null;
$methods[$canon_name] = $method;
} elseif ($mr->getDeclaringClass()->isInterface()) {
$c = new MethodContext($mr);
$c->inherited = true;
$methods[$canon_name] = new MethodValue($c, new DeclaredCallableBag($mr));
}
}
foreach ($parent_methods as $name => $method) {
if (!$method->getContext()->inherited) {
$method = clone $method;
$method->getContext()->inherited = true;
}
if ('__construct' === $name) {
$methods['__construct'] = $method;
} else {
$method->setAccessPathFrom($o);
$methods[] = $method;
}
if ($method->owner_class !== $class && $d = $method->getRepresentation('method_definition')) {
$d = clone $d;
$d->inherited = true;
$method->replaceRepresentation($d);
}
$rep->contents[] = $method;
}
$o->addRepresentation($rep);
$this->instance_cache[$class] = $methods;
}
return $this->instance_cache[$class];
}
private static function sort(MethodValue $a, MethodValue $b): int
/**
* @psalm-param class-string $class
*
* @psalm-return MethodValue[]
*/
private function getCachedStaticMethods(string $class): array
{
$sort = ((int) $a->static) - ((int) $b->static);
if ($sort) {
return $sort;
if (!isset($this->static_cache[$class])) {
$methods = [];
$r = new ReflectionClass($class);
$parent_methods = [];
if ($parent = \get_parent_class($class)) {
$parent_methods = $this->getCachedStaticMethods($parent);
}
foreach ($r->getMethods(ReflectionMethod::IS_STATIC) as $mr) {
$canon_name = \strtolower($mr->getDeclaringClass()->name.'::'.$mr->name);
if ($mr->getDeclaringClass()->name === $class) {
$method = new MethodValue(new MethodContext($mr), new DeclaredCallableBag($mr));
$methods[$canon_name] = $method;
} elseif (isset($parent_methods[$canon_name])) {
$methods[$canon_name] = $parent_methods[$canon_name];
} elseif ($mr->getDeclaringClass()->isInterface()) {
$c = new MethodContext($mr);
$c->inherited = true;
$methods[$canon_name] = new MethodValue($c, new DeclaredCallableBag($mr));
}
unset($parent_methods[$canon_name]);
}
$this->static_cache[$class] = $methods + $parent_methods;
}
$sort = Value::sortByAccess($a, $b);
if ($sort) {
return $sort;
}
$sort = InstanceValue::sortByHierarchy($a->owner_class, $b->owner_class);
if ($sort) {
return $sort;
}
return $a->startline - $b->startline;
return $this->static_cache[$class];
}
}

View File

@ -27,17 +27,23 @@ declare(strict_types=1);
namespace Kint\Parser;
use Kint\Zval\InstanceValue;
use Kint\Zval\Representation\Representation;
use Kint\Zval\Value;
use Kint\Value\AbstractValue;
use Kint\Value\Context\ClassConstContext;
use Kint\Value\Context\ClassDeclaredContext;
use Kint\Value\Context\ClassOwnedContext;
use Kint\Value\Context\StaticPropertyContext;
use Kint\Value\InstanceValue;
use Kint\Value\Representation\ContainerRepresentation;
use Kint\Value\UninitializedValue;
use ReflectionClass;
use ReflectionClassConstant;
use ReflectionProperty;
use UnitEnum;
class ClassStaticsPlugin extends AbstractPlugin
class ClassStaticsPlugin extends AbstractPlugin implements PluginCompleteInterface
{
private static $cache = [];
/** @psalm-var array<class-string, array<1|0, list<AbstractValue>>> */
private array $cache = [];
public function getTypes(): array
{
@ -49,106 +55,176 @@ class ClassStaticsPlugin extends AbstractPlugin
return Parser::TRIGGER_SUCCESS;
}
public function parse(&$var, Value &$o, int $trigger): void
/**
* @psalm-template T of AbstractValue
*
* @psalm-param mixed $var
* @psalm-param T $v
*
* @psalm-return T
*/
public function parseComplete(&$var, AbstractValue $v, int $trigger): AbstractValue
{
if (!$o instanceof InstanceValue) {
return;
if (!$v instanceof InstanceValue) {
return $v;
}
$class = \get_class($var);
$reflection = new ReflectionClass($class);
$class = $v->getClassName();
$parser = $this->getParser();
$r = new ReflectionClass($class);
// Constants
if (!isset(self::$cache[$class])) {
$consts = [];
$statics_full_name = false;
$statics = [];
$props = $r->getProperties(ReflectionProperty::IS_STATIC);
foreach ($props as $prop) {
$statics[$prop->name] = $prop;
}
foreach ($reflection->getConstants() as $name => $val) {
// Skip enum constants
if ($var instanceof UnitEnum && $val instanceof UnitEnum && $o->classname == \get_class($val)) {
$parent = $r;
while ($parent = $parent->getParentClass()) {
foreach ($parent->getProperties(ReflectionProperty::IS_STATIC) as $static) {
if (isset($statics[$static->name]) && $statics[$static->name]->getDeclaringClass()->name === $static->getDeclaringClass()->name) {
continue;
}
$const = Value::blank($name);
$const->const = true;
$const->depth = $o->depth + 1;
$const->owner_class = $class;
$const->operator = Value::OPERATOR_STATIC;
$creflection = new ReflectionClassConstant($class, $name);
$const->access = Value::ACCESS_PUBLIC;
if ($creflection->isProtected()) {
$const->access = Value::ACCESS_PROTECTED;
} elseif ($creflection->isPrivate()) {
$const->access = Value::ACCESS_PRIVATE;
}
if ($this->parser->childHasPath($o, $const)) {
$const->access_path = '\\'.$class.'::'.$name;
}
$const = $this->parser->parse($val, $const);
$consts[] = $const;
$statics[] = $static;
}
self::$cache[$class] = $consts;
}
$statics = new Representation('Static class properties', 'statics');
$statics->contents = self::$cache[$class];
$statics_parsed = [];
$found_statics = [];
foreach ($reflection->getProperties(ReflectionProperty::IS_STATIC) as $static) {
$prop = new Value();
$prop->name = '$'.$static->getName();
$prop->depth = $o->depth + 1;
$prop->static = true;
$prop->operator = Value::OPERATOR_STATIC;
$prop->owner_class = $static->getDeclaringClass()->name;
$cdepth = $v->getContext()->getDepth();
foreach ($statics as $static) {
$prop = new StaticPropertyContext(
'$'.$static->getName(),
$static->getDeclaringClass()->name,
ClassDeclaredContext::ACCESS_PUBLIC
);
$prop->depth = $cdepth + 1;
$prop->final = KINT_PHP84 && $static->isFinal();
$prop->access = Value::ACCESS_PUBLIC;
if ($static->isProtected()) {
$prop->access = Value::ACCESS_PROTECTED;
$prop->access = ClassDeclaredContext::ACCESS_PROTECTED;
} elseif ($static->isPrivate()) {
$prop->access = Value::ACCESS_PRIVATE;
$prop->access = ClassDeclaredContext::ACCESS_PRIVATE;
}
if ($this->parser->childHasPath($o, $prop)) {
if ($prop->isAccessible($parser->getCallerClass())) {
$prop->access_path = '\\'.$prop->owner_class.'::'.$prop->name;
}
if (isset($found_statics[$prop->name])) {
$statics_full_name = true;
} else {
$found_statics[$prop->name] = true;
if ($prop->owner_class !== $class && ClassDeclaredContext::ACCESS_PRIVATE === $prop->access) {
$statics_full_name = true;
}
}
if ($statics_full_name) {
$prop->name = $prop->owner_class.'::'.$prop->name;
}
$static->setAccessible(true);
if (KINT_PHP74 && !$static->isInitialized()) {
$prop->type = 'uninitialized';
$statics->contents[] = $prop;
/**
* @psalm-suppress TooFewArguments
* Appears to have been fixed in master
*/
if (!$static->isInitialized()) {
$statics_parsed[] = new UninitializedValue($prop);
} else {
$static = $static->getValue();
$statics->contents[] = $this->parser->parse($static, $prop);
$statics_parsed[] = $parser->parse($static, $prop);
}
}
if (empty($statics->contents)) {
return;
if ($statics_parsed) {
$v->addRepresentation(new ContainerRepresentation('Static properties', $statics_parsed, 'statics'));
}
\usort($statics->contents, ['Kint\\Parser\\ClassStaticsPlugin', 'sort']);
if ($consts = $this->getCachedConstants($r)) {
$v->addRepresentation(new ContainerRepresentation('Class constants', $consts, 'constants'));
}
$o->addRepresentation($statics);
return $v;
}
private static function sort(Value $a, Value $b): int
/** @psalm-return list<AbstractValue> */
private function getCachedConstants(ReflectionClass $r): array
{
$sort = ((int) $a->const) - ((int) $b->const);
if ($sort) {
return $sort;
$parser = $this->getParser();
$pdepth = $parser->getDepthLimit();
$pdepth_enabled = (int) ($pdepth > 0);
$class = $r->getName();
// Separate cache for dumping with/without depth limit
// This means we can do immediate depth limit on normal dumps
if (!isset($this->cache[$class][$pdepth_enabled])) {
$consts = [];
$reflectors = [];
foreach ($r->getConstants() as $name => $val) {
$cr = new ReflectionClassConstant($class, $name);
// Skip enum constants
if (\is_a($cr->class, UnitEnum::class, true) && $val instanceof UnitEnum && $cr->class === \get_class($val)) {
continue;
}
$reflectors[$cr->name] = [$cr, $val];
$consts[$cr->name] = null;
}
if ($r = $r->getParentClass()) {
$parents = $this->getCachedConstants($r);
foreach ($parents as $value) {
$c = $value->getContext();
$cname = $c->getName();
if (isset($reflectors[$cname]) && $c instanceof ClassOwnedContext && $reflectors[$cname][0]->getDeclaringClass()->name === $c->owner_class) {
$consts[$cname] = $value;
unset($reflectors[$cname]);
} else {
$value = clone $value;
$c = $value->getContext();
if ($c instanceof ClassOwnedContext) {
$c->name = $c->owner_class.'::'.$cname;
}
$consts[] = $value;
}
}
}
foreach ($reflectors as [$cr, $val]) {
$context = new ClassConstContext(
$cr->name,
$cr->getDeclaringClass()->name,
ClassDeclaredContext::ACCESS_PUBLIC
);
$context->depth = $pdepth ?: 1;
$context->final = KINT_PHP81 && $cr->isFinal();
if ($cr->isProtected()) {
$context->access = ClassDeclaredContext::ACCESS_PROTECTED;
} elseif ($cr->isPrivate()) {
$context->access = ClassDeclaredContext::ACCESS_PRIVATE;
} else {
// No access path for protected/private. Tough shit the cache is worth it
$context->access_path = '\\'.$context->owner_class.'::'.$context->name;
}
$consts[$cr->name] = $parser->parse($val, $context);
}
/** @psalm-var AbstractValue[] $consts */
$this->cache[$class][$pdepth_enabled] = \array_values($consts);
}
$sort = Value::sortByAccess($a, $b);
if ($sort) {
return $sort;
}
return InstanceValue::sortByHierarchy($a->owner_class, $b->owner_class);
return $this->cache[$class][$pdepth_enabled];
}
}

View File

@ -0,0 +1,102 @@
<?php
declare(strict_types=1);
/*
* The MIT License (MIT)
*
* Copyright (c) 2013 Jonathan Vollebregt (jnvsor@gmail.com), Rokas Šleinius (raveren@gmail.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
namespace Kint\Parser;
use Kint\Value\AbstractValue;
use Kint\Value\Context\BaseContext;
use Kint\Value\InstanceValue;
use ReflectionClass;
class ClassStringsPlugin extends AbstractPlugin implements PluginCompleteInterface
{
public static array $blacklist = [];
protected ClassMethodsPlugin $methods_plugin;
protected ClassStaticsPlugin $statics_plugin;
public function __construct(Parser $parser)
{
parent::__construct($parser);
$this->methods_plugin = new ClassMethodsPlugin($parser);
$this->statics_plugin = new ClassStaticsPlugin($parser);
}
public function setParser(Parser $p): void
{
parent::setParser($p);
$this->methods_plugin->setParser($p);
$this->statics_plugin->setParser($p);
}
public function getTypes(): array
{
return ['string'];
}
public function getTriggers(): int
{
return Parser::TRIGGER_SUCCESS;
}
public function parseComplete(&$var, AbstractValue $v, int $trigger): AbstractValue
{
$c = $v->getContext();
if ($c->getDepth() > 0) {
return $v;
}
if (!\class_exists($var, true)) {
return $v;
}
if (\in_array($var, self::$blacklist, true)) {
return $v;
}
$r = new ReflectionClass($var);
$fakeC = new BaseContext($c->getName());
$fakeC->access_path = null;
$fakeV = new InstanceValue($fakeC, $r->getName(), 'badhash', -1);
$fakeVar = null;
$fakeV = $this->methods_plugin->parseComplete($fakeVar, $fakeV, Parser::TRIGGER_SUCCESS);
$fakeV = $this->statics_plugin->parseComplete($fakeVar, $fakeV, Parser::TRIGGER_SUCCESS);
foreach (['methods', 'static_methods', 'statics', 'constants'] as $rep) {
if ($rep = $fakeV->getRepresentation($rep)) {
$v->addRepresentation($rep);
}
}
return $v;
}
}

View File

@ -28,13 +28,14 @@ declare(strict_types=1);
namespace Kint\Parser;
use Closure;
use Kint\Zval\ClosureValue;
use Kint\Zval\ParameterValue;
use Kint\Zval\Representation\Representation;
use Kint\Zval\Value;
use Kint\Value\AbstractValue;
use Kint\Value\ClosureValue;
use Kint\Value\Context\BaseContext;
use Kint\Value\Representation\ContainerRepresentation;
use ReflectionFunction;
use ReflectionReference;
class ClosurePlugin extends AbstractPlugin
class ClosurePlugin extends AbstractPlugin implements PluginCompleteInterface
{
public function getTypes(): array
{
@ -46,51 +47,47 @@ class ClosurePlugin extends AbstractPlugin
return Parser::TRIGGER_SUCCESS;
}
public function parse(&$var, Value &$o, int $trigger): void
public function parseComplete(&$var, AbstractValue $v, int $trigger): AbstractValue
{
if (!$var instanceof Closure) {
return;
return $v;
}
$object = new ClosureValue();
$object->transplant($o);
$o = $object;
$c = $v->getContext();
$object = new ClosureValue($c, $var);
$object->flags = $v->flags;
$object->appendRepresentations($v->getRepresentations());
$object->removeRepresentation('properties');
$closure = new ReflectionFunction($var);
$o->filename = $closure->getFileName();
$o->startline = $closure->getStartLine();
foreach ($closure->getParameters() as $param) {
$o->parameters[] = new ParameterValue($param);
}
$p = new Representation('Parameters');
$p->contents = $o->parameters;
$o->addRepresentation($p, 0);
$statics = [];
if ($v = $closure->getClosureThis()) {
$statics = ['this' => $v];
}
if (\count($statics = $statics + $closure->getStaticVariables())) {
$statics = $statics + $closure->getStaticVariables();
$cdepth = $c->getDepth();
if (\count($statics)) {
$statics_parsed = [];
foreach ($statics as $name => &$static) {
$obj = Value::blank('$'.$name);
$obj->depth = $o->depth + 1;
$statics_parsed[$name] = $this->parser->parse($static, $obj);
if (null === $statics_parsed[$name]->value) {
$statics_parsed[$name]->access_path = null;
}
$parser = $this->getParser();
foreach ($statics as $name => $_) {
$base = new BaseContext('$'.$name);
$base->depth = $cdepth + 1;
$base->reference = null !== ReflectionReference::fromArrayElement($statics, $name);
$statics_parsed[$name] = $parser->parse($statics[$name], $base);
}
$r = new Representation('Uses');
$r->contents = $statics_parsed;
$o->addRepresentation($r, 0);
$object->addRepresentation(new ContainerRepresentation('Uses', $statics_parsed), 0);
}
return $object;
}
}

View File

@ -27,10 +27,13 @@ declare(strict_types=1);
namespace Kint\Parser;
use Kint\Zval\Representation\ColorRepresentation;
use Kint\Zval\Value;
use InvalidArgumentException;
use Kint\Value\AbstractValue;
use Kint\Value\ColorValue;
use Kint\Value\Representation\ColorRepresentation;
use Kint\Value\StringValue;
class ColorPlugin extends AbstractPlugin
class ColorPlugin extends AbstractPlugin implements PluginCompleteInterface
{
public function getTypes(): array
{
@ -42,24 +45,34 @@ class ColorPlugin extends AbstractPlugin
return Parser::TRIGGER_SUCCESS;
}
public function parse(&$var, Value &$o, int $trigger): void
public function parseComplete(&$var, AbstractValue $v, int $trigger): AbstractValue
{
if (\strlen($var) > 32) {
return;
return $v;
}
if (!$v instanceof StringValue) {
return $v;
}
$trimmed = \strtolower(\trim($var));
if (!isset(ColorRepresentation::$color_map[$trimmed]) && !\preg_match('/^(?:(?:rgb|hsl)[^\\)]{6,}\\)|#[0-9a-fA-F]{3,8})$/', $trimmed)) {
return;
return $v;
}
$rep = new ColorRepresentation($var);
if ($rep->variant) {
$o->removeRepresentation($o->value);
$o->addRepresentation($rep, 0);
$o->hints[] = 'color';
try {
$rep = new ColorRepresentation($var);
} catch (InvalidArgumentException $e) {
return $v;
}
$out = new ColorValue($v->getContext(), $v->getValue(), $v->getEncoding());
$out->flags = $v->flags;
$out->appendRepresentations($v->getRepresentations());
$out->removeRepresentation('contents');
$out->addRepresentation($rep, 0);
return $out;
}
}

View File

@ -29,5 +29,5 @@ namespace Kint\Parser;
interface ConstructablePluginInterface extends PluginInterface
{
public function __construct();
public function __construct(Parser $p);
}

View File

@ -1,356 +0,0 @@
<?php
declare(strict_types=1);
/*
* The MIT License (MIT)
*
* Copyright (c) 2013 Jonathan Vollebregt (jnvsor@gmail.com), Rokas Šleinius (raveren@gmail.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
namespace Kint\Parser;
use DOMNamedNodeMap;
use DOMNode;
use DOMNodeList;
use InvalidArgumentException;
use Kint\Zval\BlobValue;
use Kint\Zval\InstanceValue;
use Kint\Zval\Representation\Representation;
use Kint\Zval\Value;
/**
* The DOMDocument parser plugin is particularly useful as it is both the only
* way to see inside the DOMNode without print_r, and the only way to see mixed
* text and node inside XML (SimpleXMLElement will strip out the text).
*/
class DOMDocumentPlugin extends AbstractPlugin
{
/**
* List of properties to skip parsing.
*
* The properties of a DOMNode can do a *lot* of damage to debuggers. The
* DOMNode contains not one, not two, not three, not four, not 5, not 6,
* not 7 but 8 different ways to recurse into itself:
* * firstChild
* * lastChild
* * previousSibling
* * nextSibling
* * ownerDocument
* * parentNode
* * childNodes
* * attributes
*
* All of this combined: the tiny SVGs used as the caret in Kint are already
* enough to make parsing and rendering take over a second, and send memory
* usage over 128 megs. So we blacklist every field we don't strictly need
* and hope that that's good enough.
*
* In retrospect - this is probably why print_r does the same
*
* @var array
*/
public static $blacklist = [
'parentNode' => 'DOMNode',
'firstChild' => 'DOMNode',
'lastChild' => 'DOMNode',
'previousSibling' => 'DOMNode',
'nextSibling' => 'DOMNode',
'ownerDocument' => 'DOMDocument',
];
/**
* Show all properties and methods.
*
* @var bool
*/
public static $verbose = false;
public function getTypes(): array
{
return ['object'];
}
public function getTriggers(): int
{
return Parser::TRIGGER_SUCCESS;
}
public function parse(&$var, Value &$o, int $trigger): void
{
if (!$o instanceof InstanceValue) {
return;
}
if ($var instanceof DOMNamedNodeMap || $var instanceof DOMNodeList) {
$this->parseList($var, $o, $trigger);
return;
}
if ($var instanceof DOMNode) {
$this->parseNode($var, $o);
return;
}
}
/**
* @param DOMNamedNodeMap|DOMNodeList &$var
*/
protected function parseList($var, InstanceValue &$o, int $trigger): void
{
if (!$var instanceof DOMNamedNodeMap && !$var instanceof DOMNodeList) {
return;
}
// Recursion should never happen, should always be stopped at the parent
// DOMNode. Depth limit on the other hand we're going to skip since
// that would show an empty iterator and rather useless. Let the depth
// limit hit the children (DOMNodeList only has DOMNode as children)
if ($trigger & Parser::TRIGGER_RECURSION) {
return;
}
$o->size = $var->length;
if (0 === $o->size) {
$o->replaceRepresentation(new Representation('Iterator'));
$o->size = null;
return;
}
// Depth limit
// Make empty iterator representation since we need it in DOMNode to point out depth limits
if ($this->parser->getDepthLimit() && $o->depth + 1 >= $this->parser->getDepthLimit()) {
$b = new Value();
$b->name = $o->classname.' Iterator Contents';
$b->access_path = 'iterator_to_array('.$o->access_path.')';
$b->depth = $o->depth + 1;
$b->hints[] = 'depth_limit';
$r = new Representation('Iterator');
$r->contents = [$b];
$o->replaceRepresentation($r, 0);
return;
}
$r = new Representation('Iterator');
$o->replaceRepresentation($r, 0);
foreach ($var as $key => $item) {
$base_obj = new Value();
$base_obj->depth = $o->depth + 1;
$base_obj->name = $item->nodeName;
if ($o->access_path) {
if ($var instanceof DOMNamedNodeMap) {
// We can't use getNamedItem() for attributes without a
// namespace because it will pick the first matching
// attribute of *any* namespace.
//
// Contrary to the PHP docs, getNamedItemNS takes null
// as a namespace argument for an unnamespaced item.
$base_obj->access_path = $o->access_path.'->getNamedItemNS(';
$base_obj->access_path .= \var_export($item->namespaceURI, true);
$base_obj->access_path .= ', ';
$base_obj->access_path .= \var_export($item->name, true);
$base_obj->access_path .= ')';
} else { // DOMNodeList
$base_obj->access_path = $o->access_path.'->item('.\var_export($key, true).')';
}
}
$r->contents[] = $this->parser->parse($item, $base_obj);
}
}
/**
* @psalm-param-out Value &$o
*/
protected function parseNode(DOMNode $var, InstanceValue &$o): void
{
// Fill the properties
// They can't be enumerated through reflection or casting,
// so we have to trust the docs and try them one at a time
$known_properties = [
'nodeValue',
'childNodes',
'attributes',
];
if (self::$verbose) {
$known_properties = [
'nodeName',
'nodeValue',
'nodeType',
'parentNode',
'childNodes',
'firstChild',
'lastChild',
'previousSibling',
'nextSibling',
'attributes',
'ownerDocument',
'namespaceURI',
'prefix',
'localName',
'baseURI',
'textContent',
];
}
$childNodes = null;
$attributes = null;
$rep = $o->value;
foreach ($known_properties as $prop) {
$prop_obj = $this->parseProperty($o, $prop, $var);
$rep->contents[] = $prop_obj;
if ('childNodes' === $prop) {
$childNodes = $prop_obj->getRepresentation('iterator');
} elseif ('attributes' === $prop) {
$attributes = $prop_obj->getRepresentation('iterator');
}
}
if (!self::$verbose) {
$o->removeRepresentation('methods');
$o->removeRepresentation('properties');
}
// Attributes and comments and text nodes don't
// need children or attributes of their own
if (\in_array($o->classname, ['DOMAttr', 'DOMText', 'DOMComment'], true)) {
$o = self::textualNodeToString($o);
return;
}
// Set the attributes
if ($attributes) {
$a = new Representation('Attributes');
foreach ($attributes->contents as $attribute) {
$a->contents[] = $attribute;
}
$o->addRepresentation($a, 0);
}
// Set the children
if ($childNodes) {
$c = new Representation('Children');
if (1 === \count($childNodes->contents) && ($node = \reset($childNodes->contents)) && \in_array('depth_limit', $node->hints, true)) {
$n = new InstanceValue();
$n->transplant($node);
$n->name = 'childNodes';
$n->classname = 'DOMNodeList';
$c->contents = [$n];
} else {
foreach ($childNodes->contents as $node) {
// Remove text nodes if theyre empty
if ($node instanceof BlobValue && '#text' === $node->name && (\ctype_space($node->value->contents) || '' === $node->value->contents)) {
continue;
}
$c->contents[] = $node;
}
}
$o->addRepresentation($c, 0);
}
if ($childNodes) {
$o->size = \count($childNodes->contents);
}
if (!$o->size) {
$o->size = null;
}
}
protected function parseProperty(InstanceValue $o, string $prop, DOMNode &$var): Value
{
// Duplicating (And slightly optimizing) the Parser::parseObject() code here
$base_obj = new Value();
$base_obj->depth = $o->depth + 1;
$base_obj->owner_class = $o->classname;
$base_obj->name = $prop;
$base_obj->operator = Value::OPERATOR_OBJECT;
$base_obj->access = Value::ACCESS_PUBLIC;
if (null !== $o->access_path) {
$base_obj->access_path = $o->access_path;
if (\preg_match('/^[A-Za-z0-9_]+$/', $base_obj->name)) {
$base_obj->access_path .= '->'.$base_obj->name;
} else {
$base_obj->access_path .= '->{'.\var_export($base_obj->name, true).'}';
}
}
if (!isset($var->{$prop})) {
$base_obj->type = 'null';
} elseif (isset(self::$blacklist[$prop])) {
$b = new InstanceValue();
$b->transplant($base_obj);
$base_obj = $b;
$base_obj->hints[] = 'blacklist';
$base_obj->classname = self::$blacklist[$prop];
} elseif ('attributes' === $prop) {
// Attributes are strings. If we're too deep set the
// depth limit to enable parsing them, but no deeper.
if ($this->parser->getDepthLimit() && $this->parser->getDepthLimit() - 2 < $base_obj->depth) {
$base_obj->depth = $this->parser->getDepthLimit() - 2;
}
$base_obj = $this->parser->parse($var->{$prop}, $base_obj);
} else {
$base_obj = $this->parser->parse($var->{$prop}, $base_obj);
}
return $base_obj;
}
protected static function textualNodeToString(InstanceValue $o): Value
{
if (empty($o->value) || empty($o->value->contents) || empty($o->classname)) {
throw new InvalidArgumentException('Invalid DOMNode passed to DOMDocumentPlugin::textualNodeToString');
}
if (!\in_array($o->classname, ['DOMText', 'DOMAttr', 'DOMComment'], true)) {
throw new InvalidArgumentException('Invalid DOMNode passed to DOMDocumentPlugin::textualNodeToString');
}
foreach ($o->value->contents as $property) {
if ('nodeValue' === $property->name) {
$ret = clone $property;
$ret->name = $o->name;
return $ret;
}
}
throw new InvalidArgumentException('Invalid DOMNode passed to DOMDocumentPlugin::textualNodeToString');
}
}

View File

@ -27,11 +27,13 @@ declare(strict_types=1);
namespace Kint\Parser;
use DateTime;
use Kint\Zval\DateTimeValue;
use Kint\Zval\Value;
use DateTimeInterface;
use Error;
use Kint\Value\AbstractValue;
use Kint\Value\DateTimeValue;
use Kint\Value\InstanceValue;
class DateTimePlugin extends AbstractPlugin
class DateTimePlugin extends AbstractPlugin implements PluginCompleteInterface
{
public function getTypes(): array
{
@ -43,15 +45,23 @@ class DateTimePlugin extends AbstractPlugin
return Parser::TRIGGER_SUCCESS;
}
public function parse(&$var, Value &$o, int $trigger): void
public function parseComplete(&$var, AbstractValue $v, int $trigger): AbstractValue
{
if (!$var instanceof DateTime) {
return;
if (!$var instanceof DateTimeInterface || !$v instanceof InstanceValue) {
return $v;
}
$object = new DateTimeValue($var);
$object->transplant($o);
try {
$dtv = new DateTimeValue($v->getContext(), $var);
} catch (Error $e) {
// Only happens if someone makes a DateTimeInterface with a private __clone
return $v;
}
$o = $object;
$dtv->setChildren($v->getChildren());
$dtv->flags = $v->flags;
$dtv->appendRepresentations($v->getRepresentations());
return $dtv;
}
}

View File

@ -0,0 +1,528 @@
<?php
declare(strict_types=1);
/*
* The MIT License (MIT)
*
* Copyright (c) 2013 Jonathan Vollebregt (jnvsor@gmail.com), Rokas Šleinius (raveren@gmail.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
namespace Kint\Parser;
use Dom\Attr;
use Dom\CharacterData;
use Dom\Document;
use Dom\DocumentType;
use Dom\Element;
use Dom\HTMLElement;
use Dom\NamedNodeMap;
use Dom\Node;
use Dom\NodeList;
use DOMAttr;
use DOMCharacterData;
use DOMDocumentType;
use DOMElement;
use DOMNamedNodeMap;
use DOMNode;
use DOMNodeList;
use Kint\Value\AbstractValue;
use Kint\Value\Context\BaseContext;
use Kint\Value\Context\ClassDeclaredContext;
use Kint\Value\Context\ContextInterface;
use Kint\Value\Context\PropertyContext;
use Kint\Value\DomNodeListValue;
use Kint\Value\DomNodeValue;
use Kint\Value\FixedWidthValue;
use Kint\Value\InstanceValue;
use Kint\Value\Representation\ContainerRepresentation;
use Kint\Value\StringValue;
use LogicException;
class DomPlugin extends AbstractPlugin implements PluginBeginInterface
{
/**
* Reflection doesn't work below 8.1, also it won't show readonly status.
*
* In order to ensure this is stable enough we're only going to provide
* properties for element and node. If subclasses like attr or document
* have their own fields then tough shit we're not showing them.
*
* @psalm-var non-empty-array<string, bool> Property names to readable status
*/
public const NODE_PROPS = [
'nodeType' => true,
'nodeName' => true,
'baseURI' => true,
'isConnected' => true,
'ownerDocument' => true,
'parentNode' => true,
'parentElement' => true,
'childNodes' => true,
'firstChild' => true,
'lastChild' => true,
'previousSibling' => true,
'nextSibling' => true,
'nodeValue' => true,
'textContent' => false,
];
/**
* @psalm-var non-empty-array<string, bool> Property names to readable status
*/
public const ELEMENT_PROPS = [
'namespaceURI' => true,
'prefix' => true,
'localName' => true,
'tagName' => true,
'id' => false,
'className' => false,
'classList' => true,
'attributes' => true,
'firstElementChild' => true,
'lastElementChild' => true,
'childElementCount' => true,
'previousElementSibling' => true,
'nextElementSibling' => true,
'innerHTML' => false,
'substitutedNodeValue' => false,
];
/**
* @psalm-var non-empty-array<string, bool> Property names to readable status
*/
public const DOMNODE_PROPS = [
'nodeName' => true,
'nodeValue' => false,
'nodeType' => true,
'parentNode' => true,
'parentElement' => true,
'childNodes' => true,
'firstChild' => true,
'lastChild' => true,
'previousSibling' => true,
'nextSibling' => true,
'attributes' => true,
'isConnected' => true,
'ownerDocument' => true,
'namespaceURI' => true,
'prefix' => false,
'localName' => true,
'baseURI' => true,
'textContent' => false,
];
/**
* @psalm-var non-empty-array<string, bool> Property names to readable status
*/
public const DOMELEMENT_PROPS = [
'tagName' => true,
'className' => false,
'id' => false,
'schemaTypeInfo' => true,
'firstElementChild' => true,
'lastElementChild' => true,
'childElementCount' => true,
'previousElementSibling' => true,
'nextElementSibling' => true,
];
public const DOM_VERSIONS = [
'parentElement' => KINT_PHP83,
'isConnected' => KINT_PHP83,
'className' => KINT_PHP83,
'id' => KINT_PHP83,
'firstElementChild' => KINT_PHP80,
'lastElementChild' => KINT_PHP80,
'childElementCount' => KINT_PHP80,
'previousElementSibling' => KINT_PHP80,
'nextElementSibling' => KINT_PHP80,
];
/**
* List of properties to skip parsing.
*
* The properties of a Dom\Node can do a *lot* of damage to debuggers. The
* Dom\Node contains not one, not two, but 13 different ways to recurse into itself:
* * parentNode
* * firstChild
* * lastChild
* * previousSibling
* * nextSibling
* * parentElement
* * firstElementChild
* * lastElementChild
* * previousElementSibling
* * nextElementSibling
* * childNodes
* * attributes
* * ownerDocument
*
* All of this combined: the tiny SVGs used as the caret in Kint were already
* enough to make parsing and rendering take over a second, and send memory
* usage over 128 megs, back in the old DOM API. So we blacklist every field
* we don't strictly need and hope that that's good enough.
*
* In retrospect -- this is probably why print_r does the same
*
* @psalm-var array<string, true>
*/
public static array $blacklist = [
'parentNode' => true,
'firstChild' => true,
'lastChild' => true,
'previousSibling' => true,
'nextSibling' => true,
'firstElementChild' => true,
'lastElementChild' => true,
'parentElement' => true,
'previousElementSibling' => true,
'nextElementSibling' => true,
'ownerDocument' => true,
];
/**
* Show all properties and methods.
*/
public static bool $verbose = false;
protected ClassMethodsPlugin $methods_plugin;
protected ClassStaticsPlugin $statics_plugin;
public function __construct(Parser $parser)
{
parent::__construct($parser);
$this->methods_plugin = new ClassMethodsPlugin($parser);
$this->statics_plugin = new ClassStaticsPlugin($parser);
}
public function setParser(Parser $p): void
{
parent::setParser($p);
$this->methods_plugin->setParser($p);
$this->statics_plugin->setParser($p);
}
public function getTypes(): array
{
return ['object'];
}
public function getTriggers(): int
{
return Parser::TRIGGER_BEGIN;
}
public function parseBegin(&$var, ContextInterface $c): ?AbstractValue
{
// Attributes and chardata (Which is parent of comments and text
// nodes) don't need children or attributes of their own
if ($var instanceof Attr || $var instanceof CharacterData || $var instanceof DOMAttr || $var instanceof DOMCharacterData) {
return $this->parseText($var, $c);
}
if ($var instanceof NamedNodeMap || $var instanceof NodeList || $var instanceof DOMNamedNodeMap || $var instanceof DOMNodeList) {
return $this->parseList($var, $c);
}
if ($var instanceof Node || $var instanceof DOMNode) {
return $this->parseNode($var, $c);
}
return null;
}
/** @psalm-param Node|DOMNode $var */
private function parseProperty(object $var, string $prop, ContextInterface $c): AbstractValue
{
if (!isset($var->{$prop})) {
return new FixedWidthValue($c, null);
}
$parser = $this->getParser();
$value = $var->{$prop};
if (\is_scalar($value)) {
return $parser->parse($value, $c);
}
if (isset(self::$blacklist[$prop])) {
$b = new InstanceValue($c, \get_class($value), \spl_object_hash($value), \spl_object_id($value));
$b->flags |= AbstractValue::FLAG_GENERATED | AbstractValue::FLAG_BLACKLIST;
return $b;
}
// Everything we can handle in parseBegin
if ($value instanceof Attr || $value instanceof CharacterData || $value instanceof DOMAttr || $value instanceof DOMCharacterData || $value instanceof NamedNodeMap || $value instanceof NodeList || $value instanceof DOMNamedNodeMap || $value instanceof DOMNodeList || $value instanceof Node || $value instanceof DOMNode) {
$out = $this->parseBegin($value, $c);
}
if (!isset($out)) {
// Shouldn't ever happen
$out = $parser->parse($value, $c); // @codeCoverageIgnore
}
$out->flags |= AbstractValue::FLAG_GENERATED;
return $out;
}
/** @psalm-param Attr|CharacterData|DOMAttr|DOMCharacterData $var */
private function parseText(object $var, ContextInterface $c): AbstractValue
{
if ($c instanceof BaseContext && null !== $c->access_path) {
$c->access_path .= '->nodeValue';
}
return $this->parseProperty($var, 'nodeValue', $c);
}
/** @psalm-param NamedNodeMap|NodeList|DOMNamedNodeMap|DOMNodeList $var */
private function parseList(object $var, ContextInterface $c): InstanceValue
{
if ($var instanceof NodeList || $var instanceof DOMNodeList) {
$v = new DomNodeListValue($c, $var);
} else {
$v = new InstanceValue($c, \get_class($var), \spl_object_hash($var), \spl_object_id($var));
}
$parser = $this->getParser();
$pdepth = $parser->getDepthLimit();
// Depth limit
// Use empty iterator representation since we need it to point out depth limits
if (($var instanceof NodeList || $var instanceof DOMNodeList) && $pdepth && $c->getDepth() >= $pdepth) {
$v->flags |= AbstractValue::FLAG_DEPTH_LIMIT;
return $v;
}
if (self::$verbose) {
$v = $this->methods_plugin->parseComplete($var, $v, Parser::TRIGGER_SUCCESS);
$v = $this->statics_plugin->parseComplete($var, $v, Parser::TRIGGER_SUCCESS);
}
if (0 === $var->length) {
$v->setChildren([]);
return $v;
}
$cdepth = $c->getDepth();
$ap = $c->getAccessPath();
$contents = [];
foreach ($var as $key => $item) {
$base_obj = new BaseContext($item->nodeName);
$base_obj->depth = $cdepth + 1;
if ($var instanceof NamedNodeMap || $var instanceof DOMNamedNodeMap) {
if (null !== $ap) {
$base_obj->access_path = $ap.'['.\var_export($item->nodeName, true).']';
}
} else { // NodeList
if (null !== $ap) {
$base_obj->access_path = $ap.'['.\var_export($key, true).']';
}
}
if ($item instanceof HTMLElement) {
$base_obj->name = $item->localName;
}
$item = $parser->parse($item, $base_obj);
$item->flags |= AbstractValue::FLAG_GENERATED;
$contents[] = $item;
}
$v->setChildren($contents);
if ($contents) {
$v->addRepresentation(new ContainerRepresentation('Iterator', $contents), 0);
}
return $v;
}
/** @psalm-param Node|DOMNode $var */
private function parseNode(object $var, ContextInterface $c): DomNodeValue
{
$class = \get_class($var);
$pdepth = $this->getParser()->getDepthLimit();
if ($pdepth && $c->getDepth() >= $pdepth) {
$v = new DomNodeValue($c, $var);
$v->flags |= AbstractValue::FLAG_DEPTH_LIMIT;
return $v;
}
if (($var instanceof DocumentType || $var instanceof DOMDocumentType) && $c instanceof BaseContext && $c->name === $var->nodeName) {
$c->name = '!DOCTYPE '.$c->name;
}
$cdepth = $c->getDepth();
$ap = $c->getAccessPath();
$properties = [];
$children = [];
$attributes = [];
foreach (self::getKnownProperties($var) as $prop => $readonly) {
$prop_c = new PropertyContext($prop, $class, ClassDeclaredContext::ACCESS_PUBLIC);
$prop_c->depth = $cdepth + 1;
$prop_c->readonly = KINT_PHP81 && $readonly;
if (null !== $ap) {
$prop_c->access_path = $ap.'->'.$prop;
}
$properties[] = $prop_obj = $this->parseProperty($var, $prop, $prop_c);
if ('childNodes' === $prop) {
if (!$prop_obj instanceof DomNodeListValue) {
throw new LogicException('childNodes property parsed incorrectly'); // @codeCoverageIgnore
}
$children = self::getChildren($prop_obj);
} elseif ('attributes' === $prop) {
$attributes = $prop_obj->getRepresentation('iterator');
$attributes = $attributes instanceof ContainerRepresentation ? $attributes->getContents() : [];
} elseif ('classList' === $prop) {
if ($iter = $prop_obj->getRepresentation('iterator')) {
$prop_obj->removeRepresentation($iter);
$prop_obj->addRepresentation($iter, 0);
}
}
}
$v = new DomNodeValue($c, $var);
// If we're in text mode, we can see children through the childNodes property
$v->setChildren($properties);
if ($children) {
$v->addRepresentation(new ContainerRepresentation('Children', $children, null, true));
}
if ($attributes) {
$v->addRepresentation(new ContainerRepresentation('Attributes', $attributes));
}
if (self::$verbose) {
$v->addRepresentation(new ContainerRepresentation('Properties', $properties));
$v = $this->methods_plugin->parseComplete($var, $v, Parser::TRIGGER_SUCCESS);
$v = $this->statics_plugin->parseComplete($var, $v, Parser::TRIGGER_SUCCESS);
}
return $v;
}
/**
* @psalm-param Node|DOMNode $var
*
* @psalm-return non-empty-array<string, bool>
*/
public static function getKnownProperties(object $var): array
{
if ($var instanceof Node) {
$known_properties = self::NODE_PROPS;
if ($var instanceof Element) {
$known_properties += self::ELEMENT_PROPS;
}
if ($var instanceof Document) {
$known_properties['textContent'] = true;
}
if ($var instanceof Attr || $var instanceof CharacterData) {
$known_properties['nodeValue'] = false;
}
} else {
$known_properties = self::DOMNODE_PROPS;
if ($var instanceof DOMElement) {
$known_properties += self::DOMELEMENT_PROPS;
}
foreach (self::DOM_VERSIONS as $key => $val) {
/**
* @psalm-var bool $val
* Psalm bug #4509
*/
if (false === $val) {
unset($known_properties[$key]); // @codeCoverageIgnore
}
}
}
/** @psalm-var non-empty-array $known_properties */
if (!self::$verbose) {
$known_properties = \array_intersect_key($known_properties, [
'nodeValue' => null,
'childNodes' => null,
'attributes' => null,
]);
}
return $known_properties;
}
/** @psalm-return list<AbstractValue> */
private static function getChildren(DomNodeListValue $property): array
{
if (0 === $property->getLength()) {
return [];
}
if ($property->flags & AbstractValue::FLAG_DEPTH_LIMIT) {
return [$property];
}
$list_items = $property->getChildren();
if (null === $list_items) {
// This is here for psalm but all DomNodeListValue should
// either be depth_limit or have array children
return []; // @codeCoverageIgnore
}
$children = [];
foreach ($list_items as $node) {
// Remove text nodes if theyre empty
if ($node instanceof StringValue && '#text' === $node->getContext()->getName()) {
/**
* @psalm-suppress InvalidArgument
* Psalm bug #11055
*/
if (\ctype_space($node->getValue()) || '' === $node->getValue()) {
continue;
}
}
$children[] = $node;
}
return $children;
}
}

View File

@ -27,15 +27,15 @@ declare(strict_types=1);
namespace Kint\Parser;
use BackedEnum;
use Kint\Zval\EnumValue;
use Kint\Zval\Representation\Representation;
use Kint\Zval\Value;
use Kint\Value\AbstractValue;
use Kint\Value\Context\BaseContext;
use Kint\Value\EnumValue;
use Kint\Value\Representation\ContainerRepresentation;
use UnitEnum;
class EnumPlugin extends AbstractPlugin
class EnumPlugin extends AbstractPlugin implements PluginCompleteInterface
{
private static $cache = [];
private array $cache = [];
public function getTypes(): array
{
@ -51,38 +51,34 @@ class EnumPlugin extends AbstractPlugin
return Parser::TRIGGER_SUCCESS;
}
public function parse(&$var, Value &$o, int $trigger): void
public function parseComplete(&$var, AbstractValue $v, int $trigger): AbstractValue
{
if (!$var instanceof UnitEnum) {
return;
return $v;
}
$c = $v->getContext();
$class = \get_class($var);
if (!isset(self::$cache[$class])) {
$cases = new Representation('Enum values', 'enum');
$cases->contents = [];
if (!isset($this->cache[$class])) {
$contents = [];
foreach ($var->cases() as $case) {
$base_obj = Value::blank($class.'::'.$case->name, '\\'.$class.'::'.$case->name);
$base_obj->depth = $o->depth + 1;
if ($var instanceof BackedEnum) {
$c = $case->value;
$cases->contents[] = $this->parser->parse($c, $base_obj);
} else {
$cases->contents[] = $base_obj;
}
$base = new BaseContext($case->name);
$base->access_path = '\\'.$class.'::'.$case->name;
$base->depth = $c->getDepth() + 1;
$contents[] = new EnumValue($base, $case);
}
self::$cache[$class] = $cases;
/** @psalm-var non-empty-array<EnumValue> $contents */
$this->cache[$class] = new ContainerRepresentation('Enum values', $contents, 'enum');
}
$object = new EnumValue($var);
$object->transplant($o);
$object = new EnumValue($c, $var);
$object->flags = $v->flags;
$object->appendRepresentations($v->getRepresentations());
$object->addRepresentation($this->cache[$class], 0);
$object->addRepresentation(self::$cache[$class], 0);
$o = $object;
return $object;
}
}

View File

@ -27,14 +27,14 @@ declare(strict_types=1);
namespace Kint\Parser;
use Kint\Zval\Representation\SplFileInfoRepresentation;
use Kint\Zval\Value;
use Kint\Value\AbstractValue;
use Kint\Value\Representation\SplFileInfoRepresentation;
use SplFileInfo;
use TypeError;
class FsPathPlugin extends AbstractPlugin
class FsPathPlugin extends AbstractPlugin implements PluginCompleteInterface
{
public static $blacklist = ['/', '.'];
public static array $blacklist = ['/', '.'];
public function getTypes(): array
{
@ -46,35 +46,35 @@ class FsPathPlugin extends AbstractPlugin
return Parser::TRIGGER_SUCCESS;
}
public function parse(&$var, Value &$o, int $trigger): void
public function parseComplete(&$var, AbstractValue $v, int $trigger): AbstractValue
{
if (\strlen($var) > 2048) {
return;
return $v;
}
if (!\preg_match('/[\\/\\'.DIRECTORY_SEPARATOR.']/', $var)) {
return;
return $v;
}
if (\preg_match('/[?<>"*|]/', $var)) {
return;
return $v;
}
try {
if (!@\file_exists($var)) {
return;
return $v;
}
} catch (TypeError $e) {// @codeCoverageIgnore
// Only possible in PHP 7
return; // @codeCoverageIgnore
return $v; // @codeCoverageIgnore
}
if (\in_array($var, self::$blacklist, true)) {
return;
return $v;
}
$r = new SplFileInfoRepresentation(new SplFileInfo($var));
$r->hints[] = 'fspath';
$o->addRepresentation($r, 0);
$v->addRepresentation(new SplFileInfoRepresentation(new SplFileInfo($var)), 0);
return $v;
}
}

View File

@ -0,0 +1,86 @@
<?php
declare(strict_types=1);
/*
* The MIT License (MIT)
*
* Copyright (c) 2013 Jonathan Vollebregt (jnvsor@gmail.com), Rokas Šleinius (raveren@gmail.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
namespace Kint\Parser;
use Dom\HTMLDocument;
use DOMException;
use Kint\Value\AbstractValue;
use Kint\Value\Context\BaseContext;
use Kint\Value\Representation\ContainerRepresentation;
use Kint\Value\Representation\ValueRepresentation;
class HtmlPlugin extends AbstractPlugin implements PluginCompleteInterface
{
public function getTypes(): array
{
return ['string'];
}
public function getTriggers(): int
{
if (!KINT_PHP84) {
return Parser::TRIGGER_NONE; // @codeCoverageIgnore
}
return Parser::TRIGGER_SUCCESS;
}
public function parseComplete(&$var, AbstractValue $v, int $trigger): AbstractValue
{
if ('<!doctype html>' !== \strtolower(\substr($var, 0, 15))) {
return $v;
}
try {
$html = HTMLDocument::createFromString($var, LIBXML_NOERROR);
} catch (DOMException $e) { // @codeCoverageIgnore
return $v; // @codeCoverageIgnore
}
$c = $v->getContext();
$base = new BaseContext('childNodes');
$base->depth = $c->getDepth();
if (null !== ($ap = $c->getAccessPath())) {
$base->access_path = '\\Dom\\HTMLDocument::createFromString('.$ap.')->childNodes';
}
$out = $this->getParser()->parse($html->childNodes, $base);
$iter = $out->getRepresentation('iterator');
if ($out->flags & AbstractValue::FLAG_DEPTH_LIMIT) {
$out->flags |= AbstractValue::FLAG_GENERATED;
$v->addRepresentation(new ValueRepresentation('HTML', $out), 0);
} elseif ($iter instanceof ContainerRepresentation) {
$v->addRepresentation(new ContainerRepresentation('HTML', $iter->getContents()), 0);
}
return $v;
}
}

View File

@ -27,11 +27,25 @@ declare(strict_types=1);
namespace Kint\Parser;
use Kint\Zval\Representation\Representation;
use Kint\Zval\Value;
use Dom\NamedNodeMap;
use Dom\NodeList;
use DOMNamedNodeMap;
use DOMNodeList;
use Kint\Value\AbstractValue;
use Kint\Value\ArrayValue;
use Kint\Value\Context\BaseContext;
use Kint\Value\InstanceValue;
use Kint\Value\Representation\ContainerRepresentation;
use Kint\Value\Representation\ValueRepresentation;
use Kint\Value\UninitializedValue;
use mysqli_result;
use PDOStatement;
use SimpleXMLElement;
use SplFileObject;
use Throwable;
use Traversable;
class IteratorPlugin extends AbstractPlugin
class IteratorPlugin extends AbstractPlugin implements PluginCompleteInterface
{
/**
* List of classes and interfaces to blacklist.
@ -40,14 +54,17 @@ class IteratorPlugin extends AbstractPlugin
* when traversed. Others are just huge. Either way, put them in here
* and you won't have to worry about them being parsed.
*
* @var array
* @psalm-var class-string<Traversable>[]
*/
public static $blacklist = [
'DOMNamedNodeMap',
'DOMNodeList',
'mysqli_result',
'PDOStatement',
'SplFileObject',
public static array $blacklist = [
NamedNodeMap::class,
NodeList::class,
DOMNamedNodeMap::class,
DOMNodeList::class,
mysqli_result::class,
PDOStatement::class,
SimpleXMLElement::class,
SplFileObject::class,
];
public function getTypes(): array
@ -60,48 +77,70 @@ class IteratorPlugin extends AbstractPlugin
return Parser::TRIGGER_SUCCESS;
}
public function parse(&$var, Value &$o, int $trigger): void
public function parseComplete(&$var, AbstractValue $v, int $trigger): AbstractValue
{
if (!$var instanceof Traversable) {
return;
if (!$var instanceof Traversable || !$v instanceof InstanceValue || $v->getRepresentation('iterator')) {
return $v;
}
$c = $v->getContext();
foreach (self::$blacklist as $class) {
/**
* @psalm-suppress RedundantCondition
* Psalm bug #11076
*/
if ($var instanceof $class) {
$b = new Value();
$b->name = $class.' Iterator Contents';
$b->access_path = 'iterator_to_array('.$o->access_path.', true)';
$b->depth = $o->depth + 1;
$b->hints[] = 'blacklist';
$base = new BaseContext($class.' Iterator Contents');
$base->depth = $c->getDepth() + 1;
if (null !== ($ap = $c->getAccessPath())) {
$base->access_path = 'iterator_to_array('.$ap.', false)';
}
$r = new Representation('Iterator');
$r->contents = [$b];
$b = new UninitializedValue($base);
$b->flags |= AbstractValue::FLAG_BLACKLIST;
$o->addRepresentation($r);
$v->addRepresentation(new ValueRepresentation('Iterator', $b));
return;
return $v;
}
}
$data = \iterator_to_array($var);
$base_obj = new Value();
$base_obj->depth = $o->depth;
if ($o->access_path) {
$base_obj->access_path = 'iterator_to_array('.$o->access_path.')';
try {
$data = \iterator_to_array($var, false);
} catch (Throwable $t) {
return $v;
}
$r = new Representation('Iterator');
$r->contents = $this->parser->parse($data, $base_obj);
$r->contents = $r->contents->value->contents;
if (!\count($data)) {
return $v;
}
$primary = $o->getRepresentations();
$primary = \reset($primary);
if ($primary && $primary === $o->value && [] === $primary->contents) {
$o->addRepresentation($r, 0);
$base = new BaseContext('Iterator Contents');
$base->depth = $c->getDepth();
if (null !== ($ap = $c->getAccessPath())) {
$base->access_path = 'iterator_to_array('.$ap.', false)';
}
$iter_val = $this->getParser()->parse($data, $base);
// Since we didn't get TRIGGER_DEPTH_LIMIT and set the iterator to the
// same depth we can assume at least 1 level deep will exist
if ($iter_val instanceof ArrayValue && $iterator_items = $iter_val->getContents()) {
$r = new ContainerRepresentation('Iterator', $iterator_items);
$iterator_items = \array_values($iterator_items);
} else {
$o->addRepresentation($r);
$r = new ValueRepresentation('Iterator', $iter_val);
$iterator_items = [$iter_val];
}
if ((bool) $v->getChildren()) {
$v->addRepresentation($r);
} else {
$v->setChildren($iterator_items);
$v->addRepresentation($r, 0);
}
return $v;
}
}

View File

@ -27,10 +27,14 @@ declare(strict_types=1);
namespace Kint\Parser;
use Kint\Zval\Representation\Representation;
use Kint\Zval\Value;
use JsonException;
use Kint\Value\AbstractValue;
use Kint\Value\ArrayValue;
use Kint\Value\Context\BaseContext;
use Kint\Value\Representation\ContainerRepresentation;
use Kint\Value\Representation\ValueRepresentation;
class JsonPlugin extends AbstractPlugin
class JsonPlugin extends AbstractPlugin implements PluginCompleteInterface
{
public function getTypes(): array
{
@ -42,34 +46,41 @@ class JsonPlugin extends AbstractPlugin
return Parser::TRIGGER_SUCCESS;
}
public function parse(&$var, Value &$o, int $trigger): void
public function parseComplete(&$var, AbstractValue $v, int $trigger): AbstractValue
{
if (!isset($var[0]) || ('{' !== $var[0] && '[' !== $var[0])) {
return;
return $v;
}
$json = \json_decode($var, true);
if (!$json) {
return;
try {
$json = \json_decode($var, true, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
return $v;
}
$json = (array) $json;
$base_obj = new Value();
$base_obj->depth = $o->depth;
$c = $v->getContext();
if ($o->access_path) {
$base_obj->access_path = 'json_decode('.$o->access_path.', true)';
$base = new BaseContext('JSON Decode');
$base->depth = $c->getDepth();
if (null !== ($ap = $c->getAccessPath())) {
$base->access_path = 'json_decode('.$ap.', true)';
}
$r = new Representation('Json');
$r->contents = $this->parser->parse($json, $base_obj);
$json = $this->getParser()->parse($json, $base);
if (!\in_array('depth_limit', $r->contents->hints, true)) {
$r->contents = $r->contents->value->contents;
if ($json instanceof ArrayValue && (~$json->flags & AbstractValue::FLAG_DEPTH_LIMIT) && $contents = $json->getContents()) {
foreach ($contents as $value) {
$value->flags |= AbstractValue::FLAG_GENERATED;
}
$v->addRepresentation(new ContainerRepresentation('Json', $contents), 0);
} else {
$json->flags |= AbstractValue::FLAG_GENERATED;
$v->addRepresentation(new ValueRepresentation('Json', $json), 0);
}
$o->addRepresentation($r, 0);
return $v;
}
}

View File

@ -27,15 +27,16 @@ declare(strict_types=1);
namespace Kint\Parser;
use Kint\Zval\Representation\MicrotimeRepresentation;
use Kint\Zval\Value;
use Kint\Value\AbstractValue;
use Kint\Value\MicrotimeValue;
use Kint\Value\Representation\MicrotimeRepresentation;
class MicrotimePlugin extends AbstractPlugin
class MicrotimePlugin extends AbstractPlugin implements PluginCompleteInterface
{
private static $last = null;
private static $start = null;
private static $times = 0;
private static $group = 0;
private static ?array $last = null;
private static ?float $start = null;
private static int $times = 0;
private static ?string $group = null;
public function getTypes(): array
{
@ -47,22 +48,24 @@ class MicrotimePlugin extends AbstractPlugin
return Parser::TRIGGER_SUCCESS;
}
public function parse(&$var, Value &$o, int $trigger): void
public function parseComplete(&$var, AbstractValue $v, int $trigger): AbstractValue
{
if (0 !== $o->depth) {
return;
$c = $v->getContext();
if ($c->getDepth() > 0) {
return $v;
}
if (\is_string($var)) {
if ('microtime()' !== $o->name || !\preg_match('/^0\\.[0-9]{8} [0-9]{10}$/', $var)) {
return;
if ('microtime()' !== $c->getName() || !\preg_match('/^0\\.[0-9]{8} [0-9]{10}$/', $var)) {
return $v;
}
$usec = (int) \substr($var, 2, 6);
$sec = (int) \substr($var, 11, 10);
} else {
if ('microtime(...)' !== $o->name) {
return;
if ('microtime(...)' !== $c->getName()) {
return $v;
}
$sec = (int) \floor($var);
@ -85,23 +88,38 @@ class MicrotimePlugin extends AbstractPlugin
if (null !== $lap) {
$total = $time - self::$start;
$r = new MicrotimeRepresentation($sec, $usec, self::$group, $lap, $total, self::$times);
$r = new MicrotimeRepresentation($sec, $usec, self::getGroup(), $lap, $total, self::$times);
} else {
$r = new MicrotimeRepresentation($sec, $usec, self::$group);
$r = new MicrotimeRepresentation($sec, $usec, self::getGroup());
}
$r->contents = $var;
$r->implicit_label = true;
$o->removeRepresentation($o->value);
$o->addRepresentation($r);
$o->hints[] = 'microtime';
$out = new MicrotimeValue($v);
$out->removeRepresentation('contents');
$out->addRepresentation($r);
return $out;
}
/** @psalm-api */
public static function clean(): void
{
self::$last = null;
self::$start = null;
self::$times = 0;
++self::$group;
self::newGroup();
}
private static function getGroup(): string
{
if (null === self::$group) {
return self::newGroup();
}
return self::$group;
}
private static function newGroup(): string
{
return self::$group = \bin2hex(\random_bytes(4));
}
}

View File

@ -27,9 +27,11 @@ declare(strict_types=1);
namespace Kint\Parser;
use Kint\Zval\Value;
use Kint\Value\AbstractValue;
use Kint\Value\Context\PropertyContext;
use Kint\Value\InstanceValue;
use Kint\Value\Representation\ContainerRepresentation;
use mysqli;
use ReflectionClass;
use Throwable;
/**
@ -38,24 +40,24 @@ use Throwable;
* Due to the way mysqli is implemented in PHP, this will cause
* warnings on certain mysqli objects if screaming is enabled.
*/
class MysqliPlugin extends AbstractPlugin
class MysqliPlugin extends AbstractPlugin implements PluginCompleteInterface
{
// These 'properties' are actually globals
protected $always_readable = [
public const ALWAYS_READABLE = [
'client_version' => true,
'connect_errno' => true,
'connect_error' => true,
];
// These are readable on empty mysqli objects, but not on failed connections
protected $empty_readable = [
public const EMPTY_READABLE = [
'client_info' => true,
'errno' => true,
'error' => true,
];
// These are only readable on connected mysqli objects
protected $connected_readable = [
public const CONNECTED_READABLE = [
'affected_rows' => true,
'error_list' => true,
'field_count' => true,
@ -80,20 +82,33 @@ class MysqliPlugin extends AbstractPlugin
return Parser::TRIGGER_COMPLETE;
}
public function parse(&$var, Value &$o, int $trigger): void
/**
* Before 8.1: Properties were nulls when cast to array
* After 8.1: Properties are readonly and uninitialized when cast to array (Aka missing).
*/
public function parseComplete(&$var, AbstractValue $v, int $trigger): AbstractValue
{
if (!$var instanceof mysqli) {
return;
if (!$var instanceof mysqli || !$v instanceof InstanceValue) {
return $v;
}
/** @psalm-var ?string $var->sqlstate */
$props = $v->getRepresentation('properties');
if (!$props instanceof ContainerRepresentation) {
return $v;
}
/**
* @psalm-var ?string $var->sqlstate
* @psalm-var ?string $var->client_info
* Psalm bug #4502
*/
try {
$connected = \is_string(@$var->sqlstate);
} catch (Throwable $t) {
$connected = false;
}
/** @psalm-var ?string $var->client_info */
try {
$empty = !$connected && \is_string(@$var->client_info);
} catch (Throwable $t) { // @codeCoverageIgnore
@ -102,93 +117,60 @@ class MysqliPlugin extends AbstractPlugin
$empty = false; // @codeCoverageIgnore
}
foreach ($o->value->contents as $key => $obj) {
if (isset($this->connected_readable[$obj->name])) {
$parser = $this->getParser();
$new_contents = [];
foreach ($props->getContents() as $key => $obj) {
$new_contents[$key] = $obj;
$c = $obj->getContext();
if (!$c instanceof PropertyContext) {
continue;
}
if (isset(self::CONNECTED_READABLE[$c->getName()])) {
$c->readonly = KINT_PHP81;
if (!$connected) {
// No failed connections after PHP 8.1
continue; // @codeCoverageIgnore
}
} elseif (isset($this->empty_readable[$obj->name])) {
} elseif (isset(self::EMPTY_READABLE[$c->getName()])) {
$c->readonly = KINT_PHP81;
// No failed connections after PHP 8.1
if (!$connected && !$empty) { // @codeCoverageIgnore
continue; // @codeCoverageIgnore
}
} elseif (!isset($this->always_readable[$obj->name])) {
} elseif (!isset(self::ALWAYS_READABLE[$c->getName()])) {
continue; // @codeCoverageIgnore
}
$c->readonly = KINT_PHP81;
// Only handle unparsed properties
if ((KINT_PHP81 ? 'uninitialized' : 'null') !== $obj->getType()) {
continue;
}
if ('null' !== $obj->type) {
continue;
$param = $var->{$c->getName()};
// If it really was a null
if (!KINT_PHP81 && null === $param) {
continue; // @codeCoverageIgnore
}
// @codeCoverageIgnoreStart
// All of this is irellevant after 8.1,
// we have separate logic for that below
$param = $var->{$obj->name};
if (null === $param) {
continue;
}
$base = Value::blank($obj->name, $obj->access_path);
$base->depth = $obj->depth;
$base->owner_class = $obj->owner_class;
$base->operator = $obj->operator;
$base->access = $obj->access;
$base->reference = $obj->reference;
$o->value->contents[$key] = $this->parser->parse($param, $base);
// @codeCoverageIgnoreEnd
$new_contents[$key] = $parser->parse($param, $c);
}
// PHP81 returns an empty array when casting a mysqli instance
if (KINT_PHP81) {
$r = new ReflectionClass(mysqli::class);
$new_contents = \array_values($new_contents);
$basepropvalues = [];
$v->setChildren($new_contents);
foreach ($r->getProperties() as $prop) {
if ($prop->isStatic()) {
continue; // @codeCoverageIgnore
}
$pname = $prop->getName();
$param = null;
if (isset($this->connected_readable[$pname])) {
if ($connected) {
$param = $var->{$pname};
}
} else {
$param = $var->{$pname};
}
$child = new Value();
$child->depth = $o->depth + 1;
$child->owner_class = mysqli::class;
$child->operator = Value::OPERATOR_OBJECT;
$child->name = $pname;
if ($prop->isPublic()) {
$child->access = Value::ACCESS_PUBLIC;
} elseif ($prop->isProtected()) { // @codeCoverageIgnore
$child->access = Value::ACCESS_PROTECTED; // @codeCoverageIgnore
} elseif ($prop->isPrivate()) { // @codeCoverageIgnore
$child->access = Value::ACCESS_PRIVATE; // @codeCoverageIgnore
}
// We only do base mysqli properties so we don't need to worry about complex names
if ($this->parser->childHasPath($o, $child)) {
$child->access_path .= $o->access_path.'->'.$child->name;
}
$basepropvalues[] = $this->parser->parse($param, $child);
}
$o->value->contents = \array_merge($basepropvalues, $o->value->contents);
if ($new_contents) {
$v->replaceRepresentation(new ContainerRepresentation('Properties', $new_contents));
}
return $v;
}
}

View File

@ -29,16 +29,33 @@ namespace Kint\Parser;
use DomainException;
use Exception;
use Kint\Zval\BlobValue;
use Kint\Zval\InstanceValue;
use Kint\Zval\Representation\Representation;
use Kint\Zval\ResourceValue;
use Kint\Zval\Value;
use InvalidArgumentException;
use Kint\Utils;
use Kint\Value\AbstractValue;
use Kint\Value\ArrayValue;
use Kint\Value\ClosedResourceValue;
use Kint\Value\Context\ArrayContext;
use Kint\Value\Context\ClassDeclaredContext;
use Kint\Value\Context\ClassOwnedContext;
use Kint\Value\Context\ContextInterface;
use Kint\Value\Context\PropertyContext;
use Kint\Value\FixedWidthValue;
use Kint\Value\InstanceValue;
use Kint\Value\Representation\ContainerRepresentation;
use Kint\Value\Representation\StringRepresentation;
use Kint\Value\ResourceValue;
use Kint\Value\StringValue;
use Kint\Value\UninitializedValue;
use Kint\Value\UnknownValue;
use Kint\Value\VirtualValue;
use ReflectionClass;
use ReflectionObject;
use ReflectionProperty;
use stdClass;
use TypeError;
use ReflectionReference;
/**
* @psalm-type ParserTrigger int-mask-of<Parser::TRIGGER_*>
*/
class Parser
{
/**
@ -52,44 +69,48 @@ class Parser
* DEPTH_LIMIT: After parsing cancelled by depth limit
* COMPLETE: SUCCESS | RECURSION | DEPTH_LIMIT
*
* While a plugin's getTriggers may return any of these
* While a plugin's getTriggers may return any of these only one should
* be given to the plugin when PluginInterface::parse is called
*/
public const TRIGGER_NONE = 0;
public const TRIGGER_BEGIN = 1;
public const TRIGGER_SUCCESS = 2;
public const TRIGGER_RECURSION = 4;
public const TRIGGER_DEPTH_LIMIT = 8;
public const TRIGGER_COMPLETE = 14;
public const TRIGGER_BEGIN = 1 << 0;
public const TRIGGER_SUCCESS = 1 << 1;
public const TRIGGER_RECURSION = 1 << 2;
public const TRIGGER_DEPTH_LIMIT = 1 << 3;
public const TRIGGER_COMPLETE = self::TRIGGER_SUCCESS | self::TRIGGER_RECURSION | self::TRIGGER_DEPTH_LIMIT;
protected $caller_class;
protected $depth_limit = 0;
protected $marker;
protected $object_hashes = [];
protected $parse_break = false;
protected $plugins = [];
/** @psalm-var ?class-string */
protected ?string $caller_class;
protected int $depth_limit = 0;
protected array $array_ref_stack = [];
protected array $object_hashes = [];
protected array $plugins = [];
/**
* @param int $depth_limit Maximum depth to parse data
* @param ?string $caller Caller class name
*
* @psalm-param ?class-string $caller
*/
public function __construct(int $depth_limit = 0, string $caller = null)
public function __construct(int $depth_limit = 0, ?string $caller = null)
{
$this->marker = "kint\0".\random_bytes(16);
$this->depth_limit = $depth_limit;
$this->caller_class = $caller;
}
/**
* Set the caller class.
*
* @psalm-param ?class-string $caller
*/
public function setCallerClass(string $caller = null): void
public function setCallerClass(?string $caller = null): void
{
$this->noRecurseCall();
$this->caller_class = $caller;
}
/** @psalm-return ?class-string */
public function getCallerClass(): ?string
{
return $this->caller_class;
@ -116,58 +137,67 @@ class Parser
* Parses a variable into a Kint object structure.
*
* @param mixed &$var The input variable
* @param Value $o The base object
*/
public function parse(&$var, Value $o): Value
public function parse(&$var, ContextInterface $c): AbstractValue
{
$o->type = \strtolower(\gettype($var));
$type = \strtolower(\gettype($var));
if (!$this->applyPlugins($var, $o, self::TRIGGER_BEGIN)) {
return $o;
if ($v = $this->applyPluginsBegin($var, $c, $type)) {
return $v;
}
switch ($o->type) {
switch ($type) {
case 'array':
return $this->parseArray($var, $o);
return $this->parseArray($var, $c);
case 'boolean':
case 'double':
case 'integer':
case 'null':
return $this->parseGeneric($var, $o);
return $this->parseFixedWidth($var, $c);
case 'object':
return $this->parseObject($var, $o);
return $this->parseObject($var, $c);
case 'resource':
return $this->parseResource($var, $o);
return $this->parseResource($var, $c);
case 'string':
return $this->parseString($var, $o);
case 'unknown type':
return $this->parseString($var, $c);
case 'resource (closed)':
return $this->parseResourceClosed($var, $c);
case 'unknown type': // @codeCoverageIgnore
default:
return $this->parseResourceClosed($var, $o);
// These should never happen. Unknown is resource (closed) from old
// PHP versions and there shouldn't be any other types.
return $this->parseUnknown($var, $c); // @codeCoverageIgnore
}
}
public function addPlugin(PluginInterface $p): bool
public function addPlugin(PluginInterface $p): void
{
if (!$types = $p->getTypes()) {
return false;
return;
}
if (!$triggers = $p->getTriggers()) {
return false;
return;
}
if ($triggers & self::TRIGGER_BEGIN && !$p instanceof PluginBeginInterface) {
throw new InvalidArgumentException('Parsers triggered on begin must implement PluginBeginInterface');
}
if ($triggers & self::TRIGGER_COMPLETE && !$p instanceof PluginCompleteInterface) {
throw new InvalidArgumentException('Parsers triggered on completion must implement PluginCompleteInterface');
}
$p->setParser($this);
foreach ($types as $type) {
if (!isset($this->plugins[$type])) {
$this->plugins[$type] = [
self::TRIGGER_BEGIN => [],
self::TRIGGER_SUCCESS => [],
self::TRIGGER_RECURSION => [],
self::TRIGGER_DEPTH_LIMIT => [],
];
}
$this->plugins[$type] ??= [
self::TRIGGER_BEGIN => [],
self::TRIGGER_SUCCESS => [],
self::TRIGGER_RECURSION => [],
self::TRIGGER_DEPTH_LIMIT => [],
];
foreach ($this->plugins[$type] as $trigger => &$pool) {
if ($triggers & $trigger) {
@ -175,8 +205,6 @@ class Parser
}
}
}
return true;
}
public function clearPlugins(): void
@ -184,61 +212,6 @@ class Parser
$this->plugins = [];
}
public function haltParse(): void
{
$this->parse_break = true;
}
public function childHasPath(InstanceValue $parent, Value $child): bool
{
if ('__PHP_Incomplete_Class' === $parent->classname) {
return false;
}
if ('object' === $parent->type && (null !== $parent->access_path || $child->static || $child->const)) {
if (Value::ACCESS_PUBLIC === $child->access) {
return true;
}
if (Value::ACCESS_PRIVATE === $child->access && $this->caller_class) {
if ($this->caller_class === $child->owner_class) {
return true;
}
} elseif (Value::ACCESS_PROTECTED === $child->access && $this->caller_class) {
if ($this->caller_class === $child->owner_class) {
return true;
}
if (\is_subclass_of($this->caller_class, $child->owner_class)) {
return true;
}
if (\is_subclass_of($child->owner_class, $this->caller_class)) {
return true;
}
}
}
return false;
}
/**
* Returns an array without the recursion marker in it.
*
* DO NOT pass an array that has had it's marker removed back
* into the parser, it will result in an extra recursion
*
* @param array $array Array potentially containing a recursion marker
*
* @return array Array with recursion marker removed
*/
public function getCleanArray(array $array): array
{
unset($array[$this->marker]);
return $array;
}
protected function noRecurseCall(): void
{
$bt = \debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT | DEBUG_BACKTRACE_IGNORE_ARGS);
@ -259,397 +232,360 @@ class Parser
}
/**
* @param null|bool|float|int &$var
* @psalm-param null|bool|float|int &$var
*/
private function parseGeneric(&$var, Value $o): Value
private function parseFixedWidth(&$var, ContextInterface $c): AbstractValue
{
$rep = new Representation('Contents');
$rep->contents = $var;
$rep->implicit_label = true;
$o->addRepresentation($rep);
$o->value = $rep;
$v = new FixedWidthValue($c, $var);
$this->applyPlugins($var, $o, self::TRIGGER_SUCCESS);
return $o;
return $this->applyPluginsComplete($var, $v, self::TRIGGER_SUCCESS);
}
/**
* Parses a string into a Kint BlobValue structure.
*
* @param string &$var The input variable
* @param Value $o The base object
*/
private function parseString(string &$var, Value $o): Value
private function parseString(string &$var, ContextInterface $c): AbstractValue
{
$string = new BlobValue();
$string->transplant($o);
$string->encoding = BlobValue::detectEncoding($var);
$string->size = \strlen($var);
$string = new StringValue($c, $var, Utils::detectEncoding($var));
$rep = new Representation('Contents');
$rep->contents = $var;
$rep->implicit_label = true;
if (false !== $string->getEncoding() && \strlen($var)) {
$string->addRepresentation(new StringRepresentation('Contents', $var, null, true));
}
$string->addRepresentation($rep);
$string->value = $rep;
$this->applyPlugins($var, $string, self::TRIGGER_SUCCESS);
return $string;
return $this->applyPluginsComplete($var, $string, self::TRIGGER_SUCCESS);
}
/**
* Parses an array into a Kint object structure.
*
* @param array &$var The input variable
* @param Value $o The base object
*/
private function parseArray(array &$var, Value $o): Value
private function parseArray(array &$var, ContextInterface $c): AbstractValue
{
$array = new Value();
$array->transplant($o);
$array->size = \count($var);
$size = \count($var);
$contents = [];
$parentRef = ReflectionReference::fromArrayElement([&$var], 0)->getId();
if (isset($var[$this->marker])) {
--$array->size;
$array->hints[] = 'recursion';
if (isset($this->array_ref_stack[$parentRef])) {
$array = new ArrayValue($c, $size, $contents);
$array->flags |= AbstractValue::FLAG_RECURSION;
$this->applyPlugins($var, $array, self::TRIGGER_RECURSION);
return $this->applyPluginsComplete($var, $array, self::TRIGGER_RECURSION);
}
$this->array_ref_stack[$parentRef] = true;
$cdepth = $c->getDepth();
$ap = $c->getAccessPath();
if ($size > 0 && $this->depth_limit && $cdepth >= $this->depth_limit) {
$array = new ArrayValue($c, $size, $contents);
$array->flags |= AbstractValue::FLAG_DEPTH_LIMIT;
$array = $this->applyPluginsComplete($var, $array, self::TRIGGER_DEPTH_LIMIT);
unset($this->array_ref_stack[$parentRef]);
return $array;
}
$rep = new Representation('Contents');
$rep->implicit_label = true;
$array->addRepresentation($rep);
$array->value = $rep;
foreach ($var as $key => $_) {
$child = new ArrayContext($key);
$child->depth = $cdepth + 1;
$child->reference = null !== ReflectionReference::fromArrayElement($var, $key);
if (!$array->size) {
$this->applyPlugins($var, $array, self::TRIGGER_SUCCESS);
if (null !== $ap) {
$child->access_path = $ap.'['.\var_export($key, true).']';
}
return $array;
$contents[$key] = $this->parse($var[$key], $child);
}
if ($this->depth_limit && $o->depth >= $this->depth_limit) {
$array->hints[] = 'depth_limit';
$array = new ArrayValue($c, $size, $contents);
$this->applyPlugins($var, $array, self::TRIGGER_DEPTH_LIMIT);
return $array;
if ($contents) {
$array->addRepresentation(new ContainerRepresentation('Contents', $contents, null, true));
}
$copy = \array_values($var);
$array = $this->applyPluginsComplete($var, $array, self::TRIGGER_SUCCESS);
// It's really really hard to access numeric string keys in arrays,
// and it's really really hard to access integer properties in
// objects, so we just use array_values and index by counter to get
// at it reliably for reference testing. This also affects access
// paths since it's pretty much impossible to access these things
// without complicated stuff you should never need to do.
$i = 0;
// Set the marker for recursion
$var[$this->marker] = $array->depth;
$refmarker = new stdClass();
foreach ($var as $key => &$val) {
if ($key === $this->marker) {
continue;
}
$child = new Value();
$child->name = $key;
$child->depth = $array->depth + 1;
$child->access = Value::ACCESS_NONE;
$child->operator = Value::OPERATOR_ARRAY;
if (null !== $array->access_path) {
if (\is_string($key) && (string) (int) $key === $key) {
$child->access_path = 'array_values('.$array->access_path.')['.$i.']'; // @codeCoverageIgnore
} else {
$child->access_path = $array->access_path.'['.\var_export($key, true).']';
}
}
$stash = $val;
try {
$copy[$i] = $refmarker;
} catch (TypeError $e) {
$child->reference = true;
}
if ($val === $refmarker) {
$child->reference = true;
$val = $stash;
}
$rep->contents[] = $this->parse($val, $child);
++$i;
}
$this->applyPlugins($var, $array, self::TRIGGER_SUCCESS);
unset($var[$this->marker]);
unset($this->array_ref_stack[$parentRef]);
return $array;
}
/**
* Parses an object into a Kint InstanceValue structure.
*
* @param object &$var The input variable
* @param Value $o The base object
* @psalm-return ReflectionProperty[]
*/
private function parseObject(&$var, Value $o): Value
private function getPropsOrdered(ReflectionClass $r): array
{
if ($parent = $r->getParentClass()) {
$props = self::getPropsOrdered($parent);
} else {
$props = [];
}
foreach ($r->getProperties() as $prop) {
if ($prop->isStatic()) {
continue;
}
if ($prop->isPrivate()) {
$props[] = $prop;
} else {
$props[$prop->name] = $prop;
}
}
return $props;
}
/**
* @codeCoverageIgnore
*
* @psalm-return ReflectionProperty[]
*/
private function getPropsOrderedOld(ReflectionClass $r): array
{
$props = [];
foreach ($r->getProperties() as $prop) {
if ($prop->isStatic()) {
continue;
}
$props[] = $prop;
}
while ($r = $r->getParentClass()) {
foreach ($r->getProperties(ReflectionProperty::IS_PRIVATE) as $prop) {
if ($prop->isStatic()) {
continue;
}
$props[] = $prop;
}
}
return $props;
}
private function parseObject(object &$var, ContextInterface $c): AbstractValue
{
$hash = \spl_object_hash($var);
$values = (array) $var;
$object = new InstanceValue();
$object->transplant($o);
$object->classname = \get_class($var);
$object->spl_object_hash = $hash;
$object->size = \count($values);
if (KINT_PHP72) {
$object->spl_object_id = \spl_object_id($var);
}
$classname = \get_class($var);
if (isset($this->object_hashes[$hash])) {
$object->hints[] = 'recursion';
$object = new InstanceValue($c, $classname, $hash, \spl_object_id($var));
$object->flags |= AbstractValue::FLAG_RECURSION;
$this->applyPlugins($var, $object, self::TRIGGER_RECURSION);
return $object;
return $this->applyPluginsComplete($var, $object, self::TRIGGER_RECURSION);
}
$this->object_hashes[$hash] = $object;
$this->object_hashes[$hash] = true;
if ($this->depth_limit && $o->depth >= $this->depth_limit) {
$object->hints[] = 'depth_limit';
$cdepth = $c->getDepth();
$ap = $c->getAccessPath();
if ($this->depth_limit && $cdepth >= $this->depth_limit) {
$object = new InstanceValue($c, $classname, $hash, \spl_object_id($var));
$object->flags |= AbstractValue::FLAG_DEPTH_LIMIT;
$object = $this->applyPluginsComplete($var, $object, self::TRIGGER_DEPTH_LIMIT);
$this->applyPlugins($var, $object, self::TRIGGER_DEPTH_LIMIT);
unset($this->object_hashes[$hash]);
return $object;
}
$reflector = new ReflectionObject($var);
if ($reflector->isUserDefined()) {
$object->filename = $reflector->getFileName();
$object->startline = $reflector->getStartLine();
if (KINT_PHP81) {
$props = $this->getPropsOrdered(new ReflectionObject($var));
} else {
$props = $this->getPropsOrderedOld(new ReflectionObject($var)); // @codeCoverageIgnore
}
$rep = new Representation('Properties');
$values = (array) $var;
$properties = [];
$readonly = [];
foreach ($props as $rprop) {
$rprop->setAccessible(true);
$name = $rprop->getName();
// Reflection is both slower and more painful to use than array casting
// We only use it to identify readonly and uninitialized properties
if (KINT_PHP74 && '__PHP_Incomplete_Class' != $object->classname) {
$rprops = $reflector->getProperties();
while ($reflector = $reflector->getParentClass()) {
$rprops = \array_merge($rprops, $reflector->getProperties(ReflectionProperty::IS_PRIVATE));
}
foreach ($rprops as $rprop) {
if ($rprop->isStatic()) {
continue;
}
$rprop->setAccessible(true);
if (KINT_PHP81 && $rprop->isReadOnly()) {
if ($rprop->isPublic()) {
$readonly[$rprop->getName()] = true;
} elseif ($rprop->isProtected()) {
$readonly["\0*\0".$rprop->getName()] = true;
} elseif ($rprop->isPrivate()) {
$readonly["\0".$rprop->getDeclaringClass()->getName()."\0".$rprop->getName()] = true;
}
}
if ($rprop->isInitialized($var)) {
continue;
}
$undefined = null;
$child = new Value();
$child->type = 'undefined';
$child->depth = $object->depth + 1;
$child->owner_class = $rprop->getDeclaringClass()->getName();
$child->operator = Value::OPERATOR_OBJECT;
$child->name = $rprop->getName();
$child->readonly = KINT_PHP81 && $rprop->isReadOnly();
if ($rprop->isPublic()) {
$child->access = Value::ACCESS_PUBLIC;
} elseif ($rprop->isProtected()) {
$child->access = Value::ACCESS_PROTECTED;
} elseif ($rprop->isPrivate()) {
$child->access = Value::ACCESS_PRIVATE;
}
// Can't dynamically add undefined properties, so no need to use var_export
if ($this->childHasPath($object, $child)) {
$child->access_path .= $object->access_path.'->'.$child->name;
}
if ($this->applyPlugins($undefined, $child, self::TRIGGER_BEGIN)) {
$this->applyPlugins($undefined, $child, self::TRIGGER_SUCCESS);
}
$rep->contents[] = $child;
}
}
$copy = \array_values($values);
$refmarker = new stdClass();
$i = 0;
// Reflection will not show parent classes private properties, and if a
// property was unset it will happly trigger a notice looking for it.
foreach ($values as $key => &$val) {
// Casting object to array:
// private properties show in the form "\0$owner_class_name\0$property_name";
// protected properties show in the form "\0*\0$property_name";
// public properties show in the form "$property_name";
// http://www.php.net/manual/en/language.types.array.php#language.types.array.casting
$child = new Value();
$child->depth = $object->depth + 1;
$child->owner_class = $object->classname;
$child->operator = Value::OPERATOR_OBJECT;
$child->access = Value::ACCESS_PUBLIC;
if (isset($readonly[$key])) {
$child->readonly = true;
$key = $name;
if ($rprop->isProtected()) {
$key = "\0*\0".$name;
} elseif ($rprop->isPrivate()) {
$key = "\0".$rprop->getDeclaringClass()->getName()."\0".$name;
}
$initialized = \array_key_exists($key, $values);
if ($key === (string) (int) $key) {
$key = (int) $key;
}
$split_key = \explode("\0", (string) $key, 3);
if ($rprop->isDefault()) {
$child = new PropertyContext(
$name,
$rprop->getDeclaringClass()->getName(),
ClassDeclaredContext::ACCESS_PUBLIC
);
if (3 === \count($split_key) && '' === $split_key[0]) {
$child->name = $split_key[2];
if ('*' === $split_key[1]) {
$child->access = Value::ACCESS_PROTECTED;
} else {
$child->access = Value::ACCESS_PRIVATE;
$child->owner_class = $split_key[1];
$child->readonly = KINT_PHP81 && $rprop->isReadOnly();
if ($rprop->isProtected()) {
$child->access = ClassDeclaredContext::ACCESS_PROTECTED;
} elseif ($rprop->isPrivate()) {
$child->access = ClassDeclaredContext::ACCESS_PRIVATE;
}
if (KINT_PHP84) {
if ($rprop->isProtectedSet()) {
$child->access_set = ClassDeclaredContext::ACCESS_PROTECTED;
} elseif ($rprop->isPrivateSet()) {
$child->access_set = ClassDeclaredContext::ACCESS_PRIVATE;
}
$hooks = $rprop->getHooks();
if (isset($hooks['get'])) {
$child->hooks |= PropertyContext::HOOK_GET;
if ($hooks['get']->returnsReference()) {
$child->hooks |= PropertyContext::HOOK_GET_REF;
}
}
if (isset($hooks['set'])) {
$child->hooks |= PropertyContext::HOOK_SET;
$child->hook_set_type = (string) $rprop->getSettableType();
if ($child->hook_set_type !== (string) $rprop->getType()) {
$child->hooks |= PropertyContext::HOOK_SET_TYPE;
} elseif ('' === $child->hook_set_type) {
$child->hook_set_type = null;
}
}
}
} elseif (KINT_PHP72) {
$child->name = (string) $key;
} else {
$child->name = $key; // @codeCoverageIgnore
$child = new ClassOwnedContext($name, $rprop->getDeclaringClass()->getName());
}
if ($this->childHasPath($object, $child)) {
$child->access_path = $object->access_path;
$child->reference = $initialized && null !== ReflectionReference::fromArrayElement($values, $key);
$child->depth = $cdepth + 1;
if (!KINT_PHP72 && \is_int($child->name)) {
$child->access_path = 'array_values((array) '.$child->access_path.')['.$i.']'; // @codeCoverageIgnore
} elseif (\preg_match('/^[a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*$/', $child->name)) {
$child->access_path .= '->'.$child->name;
if (null !== $ap && $child->isAccessible($this->caller_class)) {
/** @psalm-var string $child->name */
if (Utils::isValidPhpName($child->name)) {
$child->access_path = $ap.'->'.$child->name;
} else {
$child->access_path .= '->{'.\var_export((string) $child->name, true).'}';
$child->access_path = $ap.'->{'.\var_export($child->name, true).'}';
}
}
$stash = $val;
try {
$copy[$i] = $refmarker;
} catch (TypeError $e) {
$child->reference = true;
if (KINT_PHP84 && $rprop->isVirtual()) {
$properties[] = new VirtualValue($child);
} elseif (!$initialized) {
$properties[] = new UninitializedValue($child);
} else {
$properties[] = $this->parse($values[$key], $child);
}
if ($val === $refmarker) {
$child->reference = true;
$val = $stash;
}
$rep->contents[] = $this->parse($val, $child);
++$i;
}
$object->addRepresentation($rep);
$object->value = $rep;
$this->applyPlugins($var, $object, self::TRIGGER_SUCCESS);
$object = new InstanceValue($c, $classname, $hash, \spl_object_id($var));
if ($props) {
$object->setChildren($properties);
}
if ($properties) {
$object->addRepresentation(new ContainerRepresentation('Properties', $properties));
}
$object = $this->applyPluginsComplete($var, $object, self::TRIGGER_SUCCESS);
unset($this->object_hashes[$hash]);
return $object;
}
/**
* Parses a resource into a Kint ResourceValue structure.
*
* @param resource &$var The input variable
* @param Value $o The base object
* @psalm-param resource $var
*/
private function parseResource(&$var, Value $o): Value
private function parseResource(&$var, ContextInterface $c): AbstractValue
{
$resource = new ResourceValue();
$resource->transplant($o);
$resource->resource_type = \get_resource_type($var);
$resource = new ResourceValue($c, \get_resource_type($var));
$this->applyPlugins($var, $resource, self::TRIGGER_SUCCESS);
$resource = $this->applyPluginsComplete($var, $resource, self::TRIGGER_SUCCESS);
return $resource;
}
/**
* Parses a closed resource into a Kint object structure.
*
* @param mixed &$var The input variable
* @param Value $o The base object
* @psalm-param mixed $var
*/
private function parseResourceClosed(&$var, Value $o): Value
private function parseResourceClosed(&$var, ContextInterface $c): AbstractValue
{
$o->type = 'resource (closed)';
$this->applyPlugins($var, $o, self::TRIGGER_SUCCESS);
$v = new ClosedResourceValue($c);
return $o;
$v = $this->applyPluginsComplete($var, $v, self::TRIGGER_SUCCESS);
return $v;
}
/**
* Applies plugins for an object type.
* Catch-all for any unexpectedgettype.
*
* @param mixed &$var variable
* @param Value $o Kint object parsed so far
* @param int $trigger The trigger to check for the plugins
* This should never happen.
*
* @return bool Continue parsing
* @codeCoverageIgnore
*
* @psalm-param mixed $var
*/
private function applyPlugins(&$var, Value &$o, int $trigger): bool
private function parseUnknown(&$var, ContextInterface $c): AbstractValue
{
$break_stash = $this->parse_break;
$v = new UnknownValue($c);
/** @psalm-var bool */
$this->parse_break = false;
$v = $this->applyPluginsComplete($var, $v, self::TRIGGER_SUCCESS);
$plugins = [];
return $v;
}
if (isset($this->plugins[$o->type][$trigger])) {
$plugins = $this->plugins[$o->type][$trigger];
}
/**
* Applies plugins for a yet-unparsed value.
*
* @param mixed &$var The input variable
*/
private function applyPluginsBegin(&$var, ContextInterface $c, string $type): ?AbstractValue
{
$plugins = $this->plugins[$type][self::TRIGGER_BEGIN] ?? [];
foreach ($plugins as $plugin) {
try {
$plugin->parse($var, $o, $trigger);
if ($v = $plugin->parseBegin($var, $c)) {
return $v;
}
} catch (Exception $e) {
\trigger_error(
'An exception ('.\get_class($e).') was thrown in '.$e->getFile().' on line '.$e->getLine().' while executing Kint Parser Plugin "'.\get_class($plugin).'". Error message: '.$e->getMessage(),
'An exception ('.Utils::errorSanitizeString(\get_class($e)).') was thrown in '.$e->getFile().' on line '.$e->getLine().' while executing "'.Utils::errorSanitizeString(\get_class($plugin)).'"->parseBegin. Error message: '.Utils::errorSanitizeString($e->getMessage()),
E_USER_WARNING
);
}
}
if ($this->parse_break) {
$this->parse_break = $break_stash;
return null;
}
return false;
/**
* Applies plugins for a parsed AbstractValue.
*
* @param mixed &$var The input variable
*/
private function applyPluginsComplete(&$var, AbstractValue $v, int $trigger): AbstractValue
{
$plugins = $this->plugins[$v->getType()][$trigger] ?? [];
foreach ($plugins as $plugin) {
try {
$v = $plugin->parseComplete($var, $v, $trigger);
} catch (Exception $e) {
\trigger_error(
'An exception ('.Utils::errorSanitizeString(\get_class($e)).') was thrown in '.$e->getFile().' on line '.$e->getLine().' while executing "'.Utils::errorSanitizeString(\get_class($plugin)).'"->parseComplete. Error message: '.Utils::errorSanitizeString($e->getMessage()),
E_USER_WARNING
);
}
}
$this->parse_break = $break_stash;
return true;
return $v;
}
}

View File

@ -0,0 +1,39 @@
<?php
declare(strict_types=1);
/*
* The MIT License (MIT)
*
* Copyright (c) 2013 Jonathan Vollebregt (jnvsor@gmail.com), Rokas Šleinius (raveren@gmail.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
namespace Kint\Parser;
use Kint\Value\AbstractValue;
use Kint\Value\Context\ContextInterface;
interface PluginBeginInterface extends PluginInterface
{
/**
* @psalm-param mixed &$var
*/
public function parseBegin(&$var, ContextInterface $c): ?AbstractValue;
}

View File

@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
/*
* The MIT License (MIT)
*
* Copyright (c) 2013 Jonathan Vollebregt (jnvsor@gmail.com), Rokas Šleinius (raveren@gmail.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
namespace Kint\Parser;
use Kint\Value\AbstractValue;
/**
* @psalm-import-type ParserTrigger from Parser
*/
interface PluginCompleteInterface extends PluginInterface
{
/**
* @psalm-param mixed &$var
* @psalm-param ParserTrigger $trigger
*/
public function parseComplete(&$var, AbstractValue $v, int $trigger): AbstractValue;
}

View File

@ -27,18 +27,17 @@ declare(strict_types=1);
namespace Kint\Parser;
use Kint\Zval\Value;
/**
* @psalm-import-type ParserTrigger from Parser
*/
interface PluginInterface
{
public function setParser(Parser $p): void;
public function getTypes(): array;
public function getTriggers(): int;
/**
* @psalm-param mixed &$var
* @psalm-return ParserTrigger
*/
public function parse(&$var, Value &$o, int $trigger): void;
public function getTriggers(): int;
}

View File

@ -0,0 +1,174 @@
<?php
declare(strict_types=1);
/*
* The MIT License (MIT)
*
* Copyright (c) 2013 Jonathan Vollebregt (jnvsor@gmail.com), Rokas Šleinius (raveren@gmail.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
namespace Kint\Parser;
use Kint\Value\AbstractValue;
use Kint\Value\Context\BaseContext;
use Kint\Value\Context\ContextInterface;
use Kint\Value\FixedWidthValue;
use Kint\Value\InstanceValue;
use Kint\Value\Representation\ContainerRepresentation;
use Kint\Value\Representation\ProfileRepresentation;
/** @psalm-api */
class ProfilePlugin extends AbstractPlugin implements PluginBeginInterface, PluginCompleteInterface
{
protected array $instance_counts = [];
protected array $instance_complexity = [];
protected array $instance_count_stack = [];
protected array $class_complexity = [];
protected array $class_count_stack = [];
public function getTypes(): array
{
return ['string', 'object', 'array', 'integer', 'double', 'resource'];
}
public function getTriggers(): int
{
return Parser::TRIGGER_BEGIN | Parser::TRIGGER_COMPLETE;
}
public function parseBegin(&$var, ContextInterface $c): ?AbstractValue
{
if (0 === $c->getDepth()) {
$this->instance_counts = [];
$this->instance_complexity = [];
$this->instance_count_stack = [];
$this->class_complexity = [];
$this->class_count_stack = [];
}
if (\is_object($var)) {
$hash = \spl_object_hash($var);
$this->instance_counts[$hash] ??= 0;
$this->instance_complexity[$hash] ??= 0;
$this->instance_count_stack[$hash] ??= 0;
if (0 === $this->instance_count_stack[$hash]) {
foreach (\class_parents($var) as $class) {
$this->class_count_stack[$class] ??= 0;
++$this->class_count_stack[$class];
}
foreach (\class_implements($var) as $iface) {
$this->class_count_stack[$iface] ??= 0;
++$this->class_count_stack[$iface];
}
}
++$this->instance_count_stack[$hash];
}
return null;
}
public function parseComplete(&$var, AbstractValue $v, int $trigger): AbstractValue
{
if ($v instanceof InstanceValue) {
--$this->instance_count_stack[$v->getSplObjectHash()];
if (0 === $this->instance_count_stack[$v->getSplObjectHash()]) {
foreach (\class_parents($var) as $class) {
--$this->class_count_stack[$class];
}
foreach (\class_implements($var) as $iface) {
--$this->class_count_stack[$iface];
}
}
}
// Don't check subs if we're in recursion or array limit
if (~$trigger & Parser::TRIGGER_SUCCESS) {
return $v;
}
$sub_complexity = 1;
foreach ($v->getRepresentations() as $rep) {
if ($rep instanceof ContainerRepresentation) {
foreach ($rep->getContents() as $value) {
$profile = $value->getRepresentation('profiling');
$sub_complexity += $profile instanceof ProfileRepresentation ? $profile->complexity : 1;
}
} else {
++$sub_complexity;
}
}
if ($v instanceof InstanceValue) {
++$this->instance_counts[$v->getSplObjectHash()];
if (0 === $this->instance_count_stack[$v->getSplObjectHash()]) {
$this->instance_complexity[$v->getSplObjectHash()] += $sub_complexity;
$this->class_complexity[$v->getClassName()] ??= 0;
$this->class_complexity[$v->getClassName()] += $sub_complexity;
foreach (\class_parents($var) as $class) {
$this->class_complexity[$class] ??= 0;
if (0 === $this->class_count_stack[$class]) {
$this->class_complexity[$class] += $sub_complexity;
}
}
foreach (\class_implements($var) as $iface) {
$this->class_complexity[$iface] ??= 0;
if (0 === $this->class_count_stack[$iface]) {
$this->class_complexity[$iface] += $sub_complexity;
}
}
}
}
if (0 === $v->getContext()->getDepth()) {
$contents = [];
\arsort($this->class_complexity);
foreach ($this->class_complexity as $name => $complexity) {
$contents[] = new FixedWidthValue(new BaseContext($name), $complexity);
}
if ($contents) {
$v->addRepresentation(new ContainerRepresentation('Class complexity', $contents), 0);
}
}
$rep = new ProfileRepresentation($sub_complexity);
/** @psalm-suppress UnsupportedReferenceUsage */
if ($v instanceof InstanceValue) {
$rep->instance_counts = &$this->instance_counts[$v->getSplObjectHash()];
$rep->instance_complexity = &$this->instance_complexity[$v->getSplObjectHash()];
}
$v->addRepresentation($rep, 0);
return $v;
}
}

View File

@ -27,25 +27,29 @@ declare(strict_types=1);
namespace Kint\Parser;
use InvalidArgumentException;
use Kint\Zval\Value;
use Kint\Value\AbstractValue;
use Kint\Value\Context\ContextInterface;
class ProxyPlugin implements PluginInterface
/**
* @psalm-import-type ParserTrigger from Parser
*
* @psalm-api
*/
class ProxyPlugin implements PluginBeginInterface, PluginCompleteInterface
{
protected $parser;
protected $types;
protected $triggers;
protected array $types;
/** @psalm-var ParserTrigger */
protected int $triggers;
/** @psalm-var callable */
protected $callback;
private ?Parser $parser = null;
/**
* @param callable $callback
* @psalm-param ParserTrigger $triggers
* @psalm-param callable $callback
*/
public function __construct(array $types, int $triggers, $callback)
{
if (!\is_callable($callback)) {
throw new InvalidArgumentException('ProxyPlugin callback must be callable');
}
$this->types = $types;
$this->triggers = $triggers;
$this->callback = $callback;
@ -66,8 +70,23 @@ class ProxyPlugin implements PluginInterface
return $this->triggers;
}
public function parse(&$var, Value &$o, int $trigger): void
public function parseBegin(&$var, ContextInterface $c): ?AbstractValue
{
\call_user_func_array($this->callback, [&$var, &$o, $trigger, $this->parser]);
return \call_user_func_array($this->callback, [
&$var,
$c,
Parser::TRIGGER_BEGIN,
$this->parser,
]);
}
public function parseComplete(&$var, AbstractValue $v, int $trigger): AbstractValue
{
return \call_user_func_array($this->callback, [
&$var,
$v,
$trigger,
$this->parser,
]);
}
}

View File

@ -27,10 +27,13 @@ declare(strict_types=1);
namespace Kint\Parser;
use Kint\Zval\Representation\Representation;
use Kint\Zval\Value;
use Kint\Value\AbstractValue;
use Kint\Value\Context\BaseContext;
use Kint\Value\Representation\ValueRepresentation;
use Kint\Value\UninitializedValue;
class SerializePlugin extends AbstractPlugin
/** @psalm-api */
class SerializePlugin extends AbstractPlugin implements PluginCompleteInterface
{
/**
* Disables automatic unserialization on arrays and objects.
@ -43,13 +46,11 @@ class SerializePlugin extends AbstractPlugin
*
* The natural way to stop that from happening is to just refuse to unserialize
* stuff by default. Which is what we're doing for anything that's not scalar.
*
* @var bool
*/
public static $safe_mode = true;
public static bool $safe_mode = true;
/**
* @var bool|class-string[]
* @psalm-var bool|class-string[]
*/
public static $allowed_classes = false;
@ -63,47 +64,48 @@ class SerializePlugin extends AbstractPlugin
return Parser::TRIGGER_SUCCESS;
}
public function parse(&$var, Value &$o, int $trigger): void
public function parseComplete(&$var, AbstractValue $v, int $trigger): AbstractValue
{
$trimmed = \rtrim($var);
if ('N;' !== $trimmed && !\preg_match('/^(?:[COabis]:\\d+[:;]|d:\\d+(?:\\.\\d+);)/', $trimmed)) {
return;
return $v;
}
$options = ['allowed_classes' => self::$allowed_classes];
if (!self::$safe_mode || !\in_array($trimmed[0], ['C', 'O', 'a'], true)) {
$c = $v->getContext();
$base = new BaseContext('unserialize('.$c->getName().')');
$base->depth = $c->getDepth() + 1;
if (null !== ($ap = $c->getAccessPath())) {
$base->access_path = 'unserialize('.$ap;
if (true === self::$allowed_classes) {
$base->access_path .= ')';
} else {
$base->access_path .= ', '.\var_export($options, true).')';
}
}
if (self::$safe_mode && \in_array($trimmed[0], ['C', 'O', 'a'], true)) {
$data = new UninitializedValue($base);
$data->flags |= AbstractValue::FLAG_BLACKLIST;
} else {
// Suppress warnings on unserializeable variable
$data = @\unserialize($trimmed, $options);
if (false === $data && 'b:0;' !== \substr($trimmed, 0, 4)) {
return;
return $v;
}
$data = $this->getParser()->parse($data, $base);
}
$base_obj = new Value();
$base_obj->depth = $o->depth + 1;
$base_obj->name = 'unserialize('.$o->name.')';
$data->flags |= AbstractValue::FLAG_GENERATED;
if ($o->access_path) {
$base_obj->access_path = 'unserialize('.$o->access_path;
if (true === self::$allowed_classes) {
$base_obj->access_path .= ')';
} else {
$base_obj->access_path .= ', '.\var_export($options, true).')';
}
}
$v->addRepresentation(new ValueRepresentation('Serialized', $data), 0);
$r = new Representation('Serialized');
if (isset($data)) {
$r->contents = $this->parser->parse($data, $base_obj);
} else {
$base_obj->hints[] = 'blacklist';
$r->contents = $base_obj;
}
$o->addRepresentation($r, 0);
return $v;
}
}

View File

@ -27,20 +27,39 @@ declare(strict_types=1);
namespace Kint\Parser;
use Kint\Zval\BlobValue;
use Kint\Zval\Representation\Representation;
use Kint\Zval\SimpleXMLElementValue;
use Kint\Zval\Value;
use Kint\Utils;
use Kint\Value\AbstractValue;
use Kint\Value\Context\ArrayContext;
use Kint\Value\Context\BaseContext;
use Kint\Value\Context\ClassOwnedContext;
use Kint\Value\Context\ContextInterface;
use Kint\Value\Representation\ContainerRepresentation;
use Kint\Value\Representation\ValueRepresentation;
use Kint\Value\SimpleXMLElementValue;
use SimpleXMLElement;
class SimpleXMLElementPlugin extends AbstractPlugin
class SimpleXMLElementPlugin extends AbstractPlugin implements PluginBeginInterface
{
/**
* Show all properties and methods.
*
* @var bool
*/
public static $verbose = false;
public static bool $verbose = false;
protected ClassMethodsPlugin $methods_plugin;
public function __construct(Parser $parser)
{
parent::__construct($parser);
$this->methods_plugin = new ClassMethodsPlugin($parser);
}
public function setParser(Parser $p): void
{
parent::setParser($p);
$this->methods_plugin->setParser($p);
}
public function getTypes(): array
{
@ -49,173 +68,228 @@ class SimpleXMLElementPlugin extends AbstractPlugin
public function getTriggers(): int
{
return Parser::TRIGGER_SUCCESS;
// SimpleXMLElement is a weirdo. No recursion (Or rather everything is
// recursion) and depth limit will have to be handled manually anyway.
return Parser::TRIGGER_BEGIN;
}
public function parse(&$var, Value &$o, int $trigger): void
public function parseBegin(&$var, ContextInterface $c): ?AbstractValue
{
if (!$var instanceof SimpleXMLElement) {
return;
return null;
}
if (!self::$verbose) {
$o->removeRepresentation('properties');
$o->removeRepresentation('iterator');
$o->removeRepresentation('methods');
return $this->parseElement($var, $c);
}
protected function parseElement(SimpleXMLElement &$var, ContextInterface $c): SimpleXMLElementValue
{
$parser = $this->getParser();
$pdepth = $parser->getDepthLimit();
$cdepth = $c->getDepth();
$depthlimit = $pdepth && $cdepth >= $pdepth;
$has_children = self::hasChildElements($var);
if ($depthlimit && $has_children) {
$x = new SimpleXMLElementValue($c, $var, [], null);
$x->flags |= AbstractValue::FLAG_DEPTH_LIMIT;
return $x;
}
// An invalid SimpleXMLElement can gum up the works with
// warnings if we call stuff children/attributes on it.
if (!$var) {
$o->size = null;
$children = $this->getChildren($c, $var);
$attributes = $this->getAttributes($c, $var);
$toString = (string) $var;
$string_body = !$has_children && \strlen($toString);
return;
$x = new SimpleXMLElementValue($c, $var, $children, \strlen($toString) ? $toString : null);
if (self::$verbose) {
$x = $this->methods_plugin->parseComplete($var, $x, Parser::TRIGGER_SUCCESS);
}
$x = new SimpleXMLElementValue();
$x->transplant($o);
$namespaces = \array_merge([null], $var->getDocNamespaces());
// Attributes
$a = new Representation('Attributes');
$base_obj = new Value();
$base_obj->depth = $x->depth;
if ($x->access_path) {
$base_obj->access_path = '(string) '.$x->access_path;
if ($attributes) {
$x->addRepresentation(new ContainerRepresentation('Attributes', $attributes), 0);
}
// Attributes are strings. If we're too deep set the
// depth limit to enable parsing them, but no deeper.
if ($this->parser->getDepthLimit() && $this->parser->getDepthLimit() - 2 < $base_obj->depth) {
$base_obj->depth = $this->parser->getDepthLimit() - 2;
if ($string_body) {
$base = new BaseContext('(string) '.$c->getName());
$base->depth = $cdepth + 1;
if (null !== ($ap = $c->getAccessPath())) {
$base->access_path = '(string) '.$ap;
}
$toString = $parser->parse($toString, $base);
$x->addRepresentation(new ValueRepresentation('toString', $toString, null, true), 0);
}
$attribs = [];
if ($children) {
$x->addRepresentation(new ContainerRepresentation('Children', $children), 0);
}
foreach ($namespaces as $nsAlias => $nsUrl) {
if ($nsAttribs = $var->attributes($nsUrl)) {
$cleanAttribs = [];
return $x;
}
/** @psalm-return list<AbstractValue> */
protected function getAttributes(ContextInterface $c, SimpleXMLElement $var): array
{
$parser = $this->getParser();
$namespaces = \array_merge(['' => null], $var->getDocNamespaces());
$cdepth = $c->getDepth();
$ap = $c->getAccessPath();
$contents = [];
foreach ($namespaces as $nsAlias => $_) {
if ((bool) $nsAttribs = $var->attributes($nsAlias, true)) {
foreach ($nsAttribs as $name => $attrib) {
$cleanAttribs[(string) $name] = $attrib;
}
$obj = new ArrayContext($name);
$obj->depth = $cdepth + 1;
if (null === $nsUrl) {
$obj = clone $base_obj;
if ($obj->access_path) {
$obj->access_path .= '->attributes()';
if (null !== $ap) {
$obj->access_path = '(string) '.$ap;
if ('' !== $nsAlias) {
$obj->access_path .= '->attributes('.\var_export($nsAlias, true).', true)';
}
$obj->access_path .= '['.\var_export($name, true).']';
}
$a->contents = $this->parser->parse($cleanAttribs, $obj)->value->contents;
} else {
$obj = clone $base_obj;
if ($obj->access_path) {
$obj->access_path .= '->attributes('.\var_export($nsAlias, true).', true)';
if ('' !== $nsAlias) {
$obj->name = $nsAlias.':'.$obj->name;
}
$cleanAttribs = $this->parser->parse($cleanAttribs, $obj)->value->contents;
$string = (string) $attrib;
$attribute = $parser->parse($string, $obj);
foreach ($cleanAttribs as $attribute) {
$attribute->name = $nsAlias.':'.$attribute->name;
$a->contents[] = $attribute;
}
$contents[] = $attribute;
}
}
}
if ($a->contents) {
$x->addRepresentation($a, 0);
}
return $contents;
}
// Children
$c = new Representation('Children');
/**
* Alright kids, let's learn about SimpleXMLElement::children!
* children can take a namespace url or alias and provide a list of
* child nodes. This is great since just accessing the members through
* properties doesn't work on SimpleXMLElement when they have a
* namespace at all!
*
* Unfortunately SimpleXML decided to go the retarded route of
* categorizing elements by their tag name rather than by their local
* name (to put it in Dom terms) so if you have something like this:
*
* <root xmlns:localhost="http://localhost/">
* <tag />
* <tag xmlns="http://localhost/" />
* <localhost:tag />
* </root>
*
* * children(null) will get the first 2 results
* * children('', true) will get the first 2 results
* * children('http://localhost/') will get the last 2 results
* * children('localhost', true) will get the last result
*
* So let's just give up and stick to aliases because fuck that mess!
*
* @psalm-return list<SimpleXMLElementValue>
*/
protected function getChildren(ContextInterface $c, SimpleXMLElement $var): array
{
$namespaces = \array_merge(['' => null], $var->getDocNamespaces());
foreach ($namespaces as $nsAlias => $nsUrl) {
// This is doubling items because of the root namespace
// and the implicit namespace on its children.
$thisNs = $var->getNamespaces();
if (isset($thisNs['']) && $thisNs[''] === $nsUrl) {
continue;
}
$cdepth = $c->getDepth();
$ap = $c->getAccessPath();
if ($nsChildren = $var->children($nsUrl)) {
$contents = [];
foreach ($namespaces as $nsAlias => $_) {
if ((bool) $nsChildren = $var->children($nsAlias, true)) {
$nsap = [];
foreach ($nsChildren as $name => $child) {
$obj = new Value();
$obj->depth = $x->depth + 1;
$obj->name = (string) $name;
if ($x->access_path) {
if (null === $nsUrl) {
$obj->access_path = $x->access_path.'->children()->';
$base = new ClassOwnedContext((string) $name, SimpleXMLElement::class);
$base->depth = $cdepth + 1;
if ('' !== $nsAlias) {
$base->name = $nsAlias.':'.$name;
}
if (null !== $ap) {
if ('' === $nsAlias) {
$base->access_path = $ap.'->';
} else {
$obj->access_path = $x->access_path.'->children('.\var_export($nsAlias, true).', true)->';
$base->access_path = $ap.'->children('.\var_export($nsAlias, true).', true)->';
}
if (\preg_match('/^[a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]+$/', (string) $name)) {
$obj->access_path .= (string) $name;
if (Utils::isValidPhpName((string) $name)) {
$base->access_path .= (string) $name;
} else {
$obj->access_path .= '{'.\var_export((string) $name, true).'}';
$base->access_path .= '{'.\var_export((string) $name, true).'}';
}
if (isset($nsap[$obj->access_path])) {
++$nsap[$obj->access_path];
$obj->access_path .= '['.$nsap[$obj->access_path].']';
if (isset($nsap[$base->access_path])) {
++$nsap[$base->access_path];
$base->access_path .= '['.$nsap[$base->access_path].']';
} else {
$nsap[$obj->access_path] = 0;
$nsap[$base->access_path] = 0;
}
}
$value = $this->parser->parse($child, $obj);
if ($value->access_path && 'string' === $value->type) {
$value->access_path = '(string) '.$value->access_path;
}
$c->contents[] = $value;
$v = $this->parseElement($child, $base);
$v->flags |= AbstractValue::FLAG_GENERATED;
$contents[] = $v;
}
}
}
$x->size = \count($c->contents);
return $contents;
}
if ($x->size) {
$x->addRepresentation($c, 0);
} else {
$x->size = null;
/**
* More SimpleXMLElement bullshit.
*
* If we want to know if the element contains text we can cast to string.
* Except if it contains text mixed with elements simplexml for some stupid
* reason decides to concatenate the text from between those elements
* rather than all the text in the hierarchy...
*
* So we have NO way of getting text nodes between elements, but we can
* still tell if we have elements right? If we have elements we assume it's
* not a string and call it a day!
*
* Well if you cast the element to an array attributes will be on it so
* you'd have to remove that key, and if it's a string it'll also have the
* 0 index used for the string contents too...
*
* Wait, can we use the 0 index to tell if it's a string? Nope! CDATA
* doesn't show up AT ALL when casting to anything but string, and we'll
* still get those concatenated strings of mostly whitespace if we just do
* (string) and check the length.
*
* Luckily, I found the only way to do this reliably is through children().
* We still have to loop through all the namespaces and see if there's a
* match but then we have the problem of the attributes showing up again...
*
* Or at least that's what var_dump says. And when we cast the result to
* bool it's true too... But if we cast it to array then it's suddenly empty!
*
* Long story short the function below is the only way to reliably check if
* a SimpleXMLElement has children
*/
protected static function hasChildElements(SimpleXMLElement $var): bool
{
$namespaces = \array_merge(['' => null], $var->getDocNamespaces());
if (\strlen((string) $var)) {
$base_obj = new BlobValue();
$base_obj->depth = $x->depth + 1;
$base_obj->name = $x->name;
if ($x->access_path) {
$base_obj->access_path = '(string) '.$x->access_path;
}
$value = (string) $var;
$s = $this->parser->parse($value, $base_obj);
$srep = $s->getRepresentation('contents');
$svalrep = $s->value && 'contents' == $s->value->getName() ? $s->value : null;
if ($srep || $svalrep) {
$x->setIsStringValue(true);
$x->value = $srep ?: $svalrep;
if ($srep) {
$x->replaceRepresentation($srep, 0);
}
}
$reps = \array_reverse($s->getRepresentations());
foreach ($reps as $rep) {
$x->addRepresentation($rep, 0);
}
foreach ($namespaces as $nsAlias => $_) {
if ((array) $var->children($nsAlias, true)) {
return true;
}
}
$o = $x;
return false;
}
}

View File

@ -27,12 +27,14 @@ declare(strict_types=1);
namespace Kint\Parser;
use Kint\Zval\Representation\SplFileInfoRepresentation;
use Kint\Zval\Value;
use Kint\Value\AbstractValue;
use Kint\Value\InstanceValue;
use Kint\Value\Representation\SplFileInfoRepresentation;
use Kint\Value\SplFileInfoValue;
use SplFileInfo;
use SplFileObject;
class SplFileInfoPlugin extends AbstractPlugin
class SplFileInfoPlugin extends AbstractPlugin implements PluginCompleteInterface
{
public function getTypes(): array
{
@ -41,17 +43,26 @@ class SplFileInfoPlugin extends AbstractPlugin
public function getTriggers(): int
{
return Parser::TRIGGER_COMPLETE;
return Parser::TRIGGER_SUCCESS;
}
public function parse(&$var, Value &$o, int $trigger): void
public function parseComplete(&$var, AbstractValue $v, int $trigger): AbstractValue
{
// SplFileObject throws exceptions in normal use in places SplFileInfo doesn't
if (!$var instanceof SplFileInfo || $var instanceof SplFileObject) {
return;
return $v;
}
$r = new SplFileInfoRepresentation(clone $var);
$o->addRepresentation($r, 0);
$o->size = $r->getSize();
if (!$v instanceof InstanceValue) {
return $v;
}
$out = new SplFileInfoValue($v->getContext(), $var);
$out->setChildren($v->getChildren());
$out->flags = $v->flags;
$out->addRepresentation(new SplFileInfoRepresentation(clone $var));
$out->appendRepresentations($v->getRepresentations());
return $out;
}
}

View File

@ -27,12 +27,13 @@ declare(strict_types=1);
namespace Kint\Parser;
use Kint\Zval\Representation\Representation;
use Kint\Zval\ResourceValue;
use Kint\Zval\StreamValue;
use Kint\Zval\Value;
use Kint\Value\AbstractValue;
use Kint\Value\Context\ArrayContext;
use Kint\Value\ResourceValue;
use Kint\Value\StreamValue;
use TypeError;
class StreamPlugin extends AbstractPlugin
class StreamPlugin extends AbstractPlugin implements PluginCompleteInterface
{
public function getTypes(): array
{
@ -44,40 +45,44 @@ class StreamPlugin extends AbstractPlugin
return Parser::TRIGGER_SUCCESS;
}
public function parse(&$var, Value &$o, int $trigger): void
public function parseComplete(&$var, AbstractValue $v, int $trigger): AbstractValue
{
if (!$o instanceof ResourceValue || 'stream' !== $o->resource_type) {
return;
if (!$v instanceof ResourceValue) {
return $v;
}
// Doublecheck that the resource is open before we get the metadata
if (!\is_resource($var)) {
return;
return $v;
}
$meta = \stream_get_meta_data($var);
$rep = new Representation('Stream');
$rep->implicit_label = true;
$base_obj = new Value();
$base_obj->depth = $o->depth;
if ($o->access_path) {
$base_obj->access_path = 'stream_get_meta_data('.$o->access_path.')';
try {
$meta = \stream_get_meta_data($var);
} catch (TypeError $e) {
return $v;
}
$rep->contents = $this->parser->parse($meta, $base_obj);
$c = $v->getContext();
if (!\in_array('depth_limit', $rep->contents->hints, true)) {
$rep->contents = $rep->contents->value->contents;
$parser = $this->getParser();
$parsed_meta = [];
foreach ($meta as $key => $val) {
$base = new ArrayContext($key);
$base->depth = $c->getDepth() + 1;
if (null !== ($ap = $c->getAccessPath())) {
$base->access_path = 'stream_get_meta_data('.$ap.')['.\var_export($key, true).']';
}
$val = $parser->parse($val, $base);
$val->flags |= AbstractValue::FLAG_GENERATED;
$parsed_meta[] = $val;
}
$o->addRepresentation($rep, 0);
$o->value = $rep;
$stream = new StreamValue($c, $parsed_meta, $meta['uri'] ?? null);
$stream->flags = $v->flags;
$stream->appendRepresentations($v->getRepresentations());
$stream = new StreamValue($meta);
$stream->transplant($o);
$o = $stream;
return $stream;
}
}

View File

@ -27,8 +27,9 @@ declare(strict_types=1);
namespace Kint\Parser;
use Kint\Zval\Representation\Representation;
use Kint\Zval\Value;
use Kint\Value\AbstractValue;
use Kint\Value\ArrayValue;
use Kint\Value\Representation\TableRepresentation;
// Note: Interaction with ArrayLimitPlugin:
// Any array limited children will be shown in tables identically to
@ -36,8 +37,11 @@ use Kint\Zval\Value;
// and it's size anyway. Because ArrayLimitPlugin halts the parse on finding
// a limit all other plugins including this one are stopped, so you cannot get
// a tabular representation of an array that is longer than the limit.
class TablePlugin extends AbstractPlugin
class TablePlugin extends AbstractPlugin implements PluginCompleteInterface
{
public static int $max_width = 300;
public static int $min_width = 2;
public function getTypes(): array
{
return ['array'];
@ -48,48 +52,52 @@ class TablePlugin extends AbstractPlugin
return Parser::TRIGGER_SUCCESS;
}
public function parse(&$var, Value &$o, int $trigger): void
public function parseComplete(&$var, AbstractValue $v, int $trigger): AbstractValue
{
if (empty($o->value->contents)) {
return;
if (!$v instanceof ArrayValue) {
return $v;
}
$array = $this->parser->getCleanArray($var);
if (\count($array) < 2) {
return;
if (\count($var) < 2) {
return $v;
}
// Ensure this is an array of arrays and that all child arrays have the
// same keys. We don't care about their children - if there's another
// "table" inside we'll just make another one down the value tab
$keys = null;
foreach ($array as $elem) {
if (!\is_array($elem) || \count($elem) < 2) {
return;
foreach ($var as $elem) {
if (!\is_array($elem)) {
return $v;
}
if (null === $keys) {
if (\count($elem) < self::$min_width || \count($elem) > self::$max_width) {
return $v;
}
$keys = \array_keys($elem);
} elseif (\array_keys($elem) !== $keys) {
return;
return $v;
}
}
$children = $v->getContents();
if (!$children) {
return $v;
}
// Ensure none of the child arrays are recursion or depth limit. We
// don't care if their children are since they are the table cells
foreach ($o->value->contents as $childarray) {
if (empty($childarray->value->contents)) {
return;
foreach ($children as $childarray) {
if (!$childarray instanceof ArrayValue || empty($childarray->getContents())) {
return $v;
}
}
// Objects by reference for the win! We can do a copy-paste of the value
// representation contents and just slap a new hint on there and hey
// presto we have our table representation with no extra memory used!
$table = new Representation('Table');
$table->contents = $o->value->contents;
$table->hints[] = 'table';
$o->addRepresentation($table, 0);
$v->addRepresentation(new TableRepresentation($children), 0);
return $v;
}
}

View File

@ -27,12 +27,14 @@ declare(strict_types=1);
namespace Kint\Parser;
use Kint\Zval\Representation\SourceRepresentation;
use Kint\Zval\ThrowableValue;
use Kint\Zval\Value;
use Kint\Value\AbstractValue;
use Kint\Value\InstanceValue;
use Kint\Value\Representation\SourceRepresentation;
use Kint\Value\ThrowableValue;
use RuntimeException;
use Throwable;
class ThrowablePlugin extends AbstractPlugin
class ThrowablePlugin extends AbstractPlugin implements PluginCompleteInterface
{
public function getTypes(): array
{
@ -44,18 +46,22 @@ class ThrowablePlugin extends AbstractPlugin
return Parser::TRIGGER_SUCCESS;
}
public function parse(&$var, Value &$o, int $trigger): void
public function parseComplete(&$var, AbstractValue $v, int $trigger): AbstractValue
{
if (!$var instanceof Throwable) {
return;
if (!$var instanceof Throwable || !$v instanceof InstanceValue) {
return $v;
}
$throw = new ThrowableValue($var);
$throw->transplant($o);
$r = new SourceRepresentation($var->getFile(), $var->getLine());
$r->showfilename = true;
$throw->addRepresentation($r, 0);
$throw = new ThrowableValue($v->getContext(), $var);
$throw->setChildren($v->getChildren());
$throw->flags = $v->flags;
$throw->appendRepresentations($v->getRepresentations());
$o = $throw;
try {
$throw->addRepresentation(new SourceRepresentation($var->getFile(), $var->getLine(), null, true), 0);
} catch (RuntimeException $e) {
}
return $throw;
}
}

View File

@ -27,11 +27,15 @@ declare(strict_types=1);
namespace Kint\Parser;
use Kint\Zval\Value;
use DateTimeImmutable;
use Kint\Value\AbstractValue;
use Kint\Value\FixedWidthValue;
use Kint\Value\Representation\StringRepresentation;
use Kint\Value\StringValue;
class TimestampPlugin extends AbstractPlugin
class TimestampPlugin extends AbstractPlugin implements PluginCompleteInterface
{
public static $blacklist = [
public static array $blacklist = [
2147483648,
2147483647,
1073741824,
@ -48,30 +52,38 @@ class TimestampPlugin extends AbstractPlugin
return Parser::TRIGGER_SUCCESS;
}
public function parse(&$var, Value &$o, int $trigger): void
public function parseComplete(&$var, AbstractValue $v, int $trigger): AbstractValue
{
if (\is_string($var) && !\ctype_digit($var)) {
return;
return $v;
}
if ($var < 0) {
return;
return $v;
}
if (\in_array($var, self::$blacklist, true)) {
return;
return $v;
}
$len = \strlen((string) $var);
// Guess for anything between March 1973 and November 2286
if (9 === $len || 10 === $len) {
// If it's an int or string that's this short it probably has no other meaning
// Additionally it's highly unlikely the shortValue will be clipped for length
// If you're writing a plugin that interferes with this, just put your
// parser plugin further down the list so that it gets loaded afterwards.
$o->value->label = 'Timestamp';
$o->value->hints[] = 'timestamp';
if ($len < 9 || $len > 10) {
return $v;
}
if (!$v instanceof StringValue && !$v instanceof FixedWidthValue) {
return $v;
}
if (!$dt = DateTimeImmutable::createFromFormat('U', (string) $var)) {
return $v;
}
$v->removeRepresentation('contents');
$v->addRepresentation(new StringRepresentation('Timestamp', $dt->format('c'), null, true));
return $v;
}
}

View File

@ -27,15 +27,19 @@ declare(strict_types=1);
namespace Kint\Parser;
use Kint\Zval\Representation\Representation;
use Kint\Zval\Value;
use Kint\Value\AbstractValue;
use Kint\Value\Context\BaseContext;
use Kint\Value\Representation\ValueRepresentation;
use ReflectionClass;
use SimpleXMLElement;
use SplFileInfo;
use Throwable;
class ToStringPlugin extends AbstractPlugin
class ToStringPlugin extends AbstractPlugin implements PluginCompleteInterface
{
public static $blacklist = [
'SimpleXMLElement',
'SplFileObject',
public static array $blacklist = [
SimpleXMLElement::class,
SplFileInfo::class,
];
public function getTypes(): array
@ -48,22 +52,37 @@ class ToStringPlugin extends AbstractPlugin
return Parser::TRIGGER_SUCCESS;
}
public function parse(&$var, Value &$o, int $trigger): void
public function parseComplete(&$var, AbstractValue $v, int $trigger): AbstractValue
{
$reflection = new ReflectionClass($var);
if (!$reflection->hasMethod('__toString')) {
return;
return $v;
}
foreach (self::$blacklist as $class) {
if ($var instanceof $class) {
return;
return $v;
}
}
$r = new Representation('toString');
$r->contents = (string) $var;
try {
$string = (string) $var;
} catch (Throwable $t) {
return $v;
}
$o->addRepresentation($r);
$c = $v->getContext();
$base = new BaseContext($c->getName());
$base->depth = $c->getDepth() + 1;
if (null !== ($ap = $c->getAccessPath())) {
$base->access_path = '(string) '.$ap;
}
$string = $this->getParser()->parse($string, $base);
$v->addRepresentation(new ValueRepresentation('toString', $string));
return $v;
}
}

View File

@ -28,14 +28,23 @@ declare(strict_types=1);
namespace Kint\Parser;
use Kint\Utils;
use Kint\Zval\TraceFrameValue;
use Kint\Zval\TraceValue;
use Kint\Zval\Value;
use Kint\Value\AbstractValue;
use Kint\Value\ArrayValue;
use Kint\Value\Context\ArrayContext;
use Kint\Value\Representation\ContainerRepresentation;
use Kint\Value\Representation\SourceRepresentation;
use Kint\Value\Representation\ValueRepresentation;
use Kint\Value\TraceFrameValue;
use Kint\Value\TraceValue;
use RuntimeException;
class TracePlugin extends AbstractPlugin
/**
* @psalm-import-type TraceFrame from TraceFrameValue
*/
class TracePlugin extends AbstractPlugin implements PluginCompleteInterface
{
public static $blacklist = ['spl_autoload_call'];
public static $path_blacklist = [];
public static array $blacklist = ['spl_autoload_call'];
public static array $path_blacklist = [];
public function getTypes(): array
{
@ -47,42 +56,46 @@ class TracePlugin extends AbstractPlugin
return Parser::TRIGGER_SUCCESS;
}
public function parse(&$var, Value &$o, int $trigger): void
public function parseComplete(&$var, AbstractValue $v, int $trigger): AbstractValue
{
if (!$o->value) {
return;
if (!$v instanceof ArrayValue) {
return $v;
}
$trace = $this->parser->getCleanArray($var);
// Shallow copy so we don't have to worry about touching var
$trace = $var;
if (\count($trace) !== \count($o->value->contents) || !Utils::isTrace($trace)) {
return;
if (!Utils::isTrace($trace)) {
return $v;
}
$traceobj = new TraceValue();
$traceobj->transplant($o);
$rep = $traceobj->value;
$pdepth = $this->getParser()->getDepthLimit();
$c = $v->getContext();
$old_trace = $rep->contents;
// We need at least 2 levels in order to get $trace[n]['args']
if ($pdepth && $c->getDepth() + 2 >= $pdepth) {
return $v;
}
Utils::normalizeAliases(self::$blacklist);
$contents = $v->getContents();
self::$blacklist = Utils::normalizeAliases(self::$blacklist);
$path_blacklist = self::normalizePaths(self::$path_blacklist);
$rep->contents = [];
$frames = [];
foreach ($old_trace as $frame) {
$index = $frame->name;
if (!isset($trace[$index]['function'])) {
// Something's very very wrong here, but it's probably a plugin's fault
foreach ($contents as $frame) {
if (!$frame instanceof ArrayValue || !$frame->getContext() instanceof ArrayContext) {
continue;
}
if (Utils::traceFrameIsListed($trace[$index], self::$blacklist)) {
$index = $frame->getContext()->getName();
if (!isset($trace[$index]) || Utils::traceFrameIsListed($trace[$index], self::$blacklist)) {
continue;
}
if (isset($trace[$index]['file']) && ($realfile = \realpath($trace[$index]['file']))) {
if (isset($trace[$index]['file']) && false !== ($realfile = \realpath($trace[$index]['file']))) {
foreach ($path_blacklist as $path) {
if (0 === \strpos($realfile, $path)) {
continue 2;
@ -90,16 +103,39 @@ class TracePlugin extends AbstractPlugin
}
}
$rep->contents[$index] = new TraceFrameValue($frame, $trace[$index]);
$frame = new TraceFrameValue($frame, $trace[$index]);
if (null !== ($file = $frame->getFile()) && null !== ($line = $frame->getLine())) {
try {
$frame->addRepresentation(new SourceRepresentation($file, $line));
} catch (RuntimeException $e) {
}
}
if ($args = $frame->getArgs()) {
$frame->addRepresentation(new ContainerRepresentation('Arguments', $args));
}
if ($obj = $frame->getObject()) {
$frame->addRepresentation(
new ValueRepresentation(
'Callee object ['.$obj->getClassName().']',
$obj,
'callee_object'
)
);
}
$frames[$index] = $frame;
}
\ksort($rep->contents);
$rep->contents = \array_values($rep->contents);
$traceobj = new TraceValue($c, \count($frames), $frames);
$traceobj->clearRepresentations();
$traceobj->addRepresentation($rep);
$traceobj->size = \count($rep->contents);
$o = $traceobj;
if ($frames) {
$traceobj->addRepresentation(new ContainerRepresentation('Contents', $frames, null, true));
}
return $traceobj;
}
protected static function normalizePaths(array $paths): array

View File

@ -27,12 +27,19 @@ declare(strict_types=1);
namespace Kint\Parser;
use Dom\Node;
use Dom\XMLDocument;
use DOMDocument;
use Exception;
use Kint\Zval\Representation\Representation;
use Kint\Zval\Value;
use DOMException;
use DOMNode;
use InvalidArgumentException;
use Kint\Value\AbstractValue;
use Kint\Value\Context\BaseContext;
use Kint\Value\Context\ContextInterface;
use Kint\Value\Representation\ValueRepresentation;
use Throwable;
class XmlPlugin extends AbstractPlugin
class XmlPlugin extends AbstractPlugin implements PluginCompleteInterface
{
/**
* Which method to parse the variable with.
@ -41,9 +48,9 @@ class XmlPlugin extends AbstractPlugin
* however it's memory usage is very high and it takes longer to parse and
* render. Plus it's a pain to work with. So SimpleXML is the default.
*
* @var string
* @psalm-var 'SimpleXML'|'DOMDocument'|'XMLDocument'
*/
public static $parse_method = 'SimpleXML';
public static string $parse_method = 'SimpleXML';
public function getTypes(): array
{
@ -55,59 +62,54 @@ class XmlPlugin extends AbstractPlugin
return Parser::TRIGGER_SUCCESS;
}
public function parse(&$var, Value &$o, int $trigger): void
public function parseComplete(&$var, AbstractValue $v, int $trigger): AbstractValue
{
if ('<?xml' !== \substr($var, 0, 5)) {
return;
return $v;
}
if (!\method_exists(\get_class($this), 'xmlTo'.self::$parse_method)) {
return;
if (!\method_exists($this, 'xmlTo'.self::$parse_method)) {
return $v;
}
$xml = \call_user_func([\get_class($this), 'xmlTo'.self::$parse_method], $var, $o->access_path);
$c = $v->getContext();
if (empty($xml)) {
return;
$out = \call_user_func([$this, 'xmlTo'.self::$parse_method], $var, $c);
if (null === $out) {
return $v;
}
[$xml, $access_path, $name] = $xml;
$out->flags |= AbstractValue::FLAG_GENERATED;
$base_obj = new Value();
$base_obj->depth = $o->depth + 1;
$base_obj->name = $name;
$base_obj->access_path = $access_path;
$v->addRepresentation(new ValueRepresentation('XML', $out), 0);
$r = new Representation('XML');
$r->contents = $this->parser->parse($xml, $base_obj);
$o->addRepresentation($r, 0);
return $v;
}
protected static function xmlToSimpleXML(string $var, ?string $parent_path): ?array
/** @psalm-suppress PossiblyUnusedMethod */
protected function xmlToSimpleXML(string $var, ContextInterface $c): ?AbstractValue
{
$errors = \libxml_use_internal_errors(true);
try {
$xml = \simplexml_load_string($var);
} catch (Exception $e) {
if (!(bool) $xml) {
throw new InvalidArgumentException('Bad XML parse in XmlPlugin::xmlToSimpleXML');
}
} catch (Throwable $t) {
return null;
} finally {
\libxml_use_internal_errors($errors);
\libxml_clear_errors();
}
if (false === $xml) {
return null;
$base = new BaseContext($xml->getName());
$base->depth = $c->getDepth() + 1;
if (null !== ($ap = $c->getAccessPath())) {
$base->access_path = 'simplexml_load_string('.$ap.')';
}
if (null === $parent_path) {
$access_path = null;
} else {
$access_path = 'simplexml_load_string('.$parent_path.')';
}
$name = $xml->getName();
return [$xml, $access_path, $name];
return $this->getParser()->parse($xml, $base);
}
/**
@ -115,38 +117,63 @@ class XmlPlugin extends AbstractPlugin
*
* If it errors loading then we wouldn't have gotten this far in the first place.
*
* @psalm-param non-empty-string $var The XML string
* @psalm-suppress PossiblyUnusedMethod
*
* @param ?string $parent_path The path to the parent, in this case the XML string
*
* @return ?array The root element DOMNode, the access path, and the root element name
* @psalm-param non-empty-string $var
*/
protected static function xmlToDOMDocument(string $var, ?string $parent_path): ?array
protected function xmlToDOMDocument(string $var, ContextInterface $c): ?AbstractValue
{
// There's no way to check validity in DOMDocument without making errors. For shame!
if (!self::xmlToSimpleXML($var, $parent_path)) {
try {
$xml = new DOMDocument();
$check = $xml->loadXML($var, LIBXML_NOWARNING | LIBXML_NOERROR);
if (false === $check) {
throw new InvalidArgumentException('Bad XML parse in XmlPlugin::xmlToDOMDocument');
}
} catch (Throwable $t) {
return null;
}
$xml = new DOMDocument();
$xml->loadXML($var);
$xml = $xml->firstChild;
if ($xml->childNodes->count() > 1) {
$xml = $xml->childNodes;
$access_path = 'childNodes';
} else {
$xml = $xml->firstChild;
$access_path = 'firstChild';
/**
* @psalm-var DOMNode $xml
* Psalm bug #11120
*/
$base = new BaseContext($xml->nodeName);
$base->depth = $c->getDepth() + 1;
if (null !== ($ap = $c->getAccessPath())) {
$base->access_path = '(function($s){$x = new \\DomDocument(); $x->loadXML($s); return $x;})('.$ap.')->firstChild';
}
if (null === $parent_path) {
$access_path = null;
} else {
$access_path = '(function($s){$x = new \\DomDocument(); $x->loadXML($s); return $x;})('.$parent_path.')->'.$access_path;
return $this->getParser()->parse($xml, $base);
}
/** @psalm-suppress PossiblyUnusedMethod */
protected function xmlToXMLDocument(string $var, ContextInterface $c): ?AbstractValue
{
if (!KINT_PHP84) {
return null; // @codeCoverageIgnore
}
$name = $xml->nodeName ?? null;
try {
$xml = XMLDocument::createFromString($var, LIBXML_NOWARNING | LIBXML_NOERROR);
} catch (DOMException $e) {
return null;
}
return [$xml, $access_path, $name];
$xml = $xml->firstChild;
/**
* @psalm-var Node $xml
* Psalm bug #11120
*/
$base = new BaseContext($xml->nodeName);
$base->depth = $c->getDepth() + 1;
if (null !== ($ap = $c->getAccessPath())) {
$base->access_path = '\\Dom\\XMLDocument::createFromString('.$ap.')->firstChild';
}
return $this->getParser()->parse($xml, $base);
}
}

View File

@ -27,67 +27,38 @@ declare(strict_types=1);
namespace Kint\Renderer;
use Kint\Zval\InstanceValue;
use Kint\Zval\Value;
/**
* @psalm-type PluginMap array<string, class-string>
*
* @psalm-consistent-constructor
*/
abstract class AbstractRenderer implements RendererInterface
abstract class AbstractRenderer implements ConstructableRendererInterface
{
public const SORT_NONE = 0;
public const SORT_VISIBILITY = 1;
public const SORT_FULL = 2;
public static ?string $js_nonce = null;
public static ?string $css_nonce = null;
protected $call_info = [];
protected $statics = [];
protected $show_trace = true;
/** @psalm-var ?non-empty-string */
public static ?string $file_link_format = null;
protected bool $show_trace = true;
protected ?array $callee = null;
protected array $trace = [];
protected bool $render_spl_ids = true;
public function __construct()
{
}
public function shouldRenderObjectIds(): bool
{
return $this->render_spl_ids;
}
public function setCallInfo(array $info): void
{
if (!isset($info['modifiers']) || !\is_array($info['modifiers'])) {
$info['modifiers'] = [];
}
if (!isset($info['trace']) || !\is_array($info['trace'])) {
$info['trace'] = [];
}
$this->call_info = [
'params' => $info['params'] ?? null,
'modifiers' => $info['modifiers'],
'callee' => $info['callee'] ?? null,
'caller' => $info['caller'] ?? null,
'trace' => $info['trace'],
];
}
public function getCallInfo(): array
{
return $this->call_info;
$this->callee = $info['callee'] ?? null;
$this->trace = $info['trace'] ?? [];
}
public function setStatics(array $statics): void
{
$this->statics = $statics;
$this->setShowTrace(!empty($statics['display_called_from']));
}
public function getStatics(): array
{
return $this->statics;
}
public function setShowTrace(bool $show_trace): void
{
$this->show_trace = $show_trace;
}
public function getShowTrace(): bool
{
return $this->show_trace;
$this->show_trace = !empty($statics['display_called_from']);
}
public function filterParserPlugins(array $plugins): array
@ -105,71 +76,12 @@ abstract class AbstractRenderer implements RendererInterface
return '';
}
/**
* Returns the first compatible plugin available.
*
* @psalm-param PluginMap $plugins Array of hints to class strings
* @psalm-param string[] $hints Array of object hints
*
* @psalm-return PluginMap Array of hints to class strings filtered and sorted by object hints
*/
public function matchPlugins(array $plugins, array $hints): array
public static function getFileLink(string $file, int $line): ?string
{
$out = [];
foreach ($hints as $key) {
if (isset($plugins[$key])) {
$out[$key] = $plugins[$key];
}
if (null === self::$file_link_format) {
return null;
}
return $out;
}
public static function sortPropertiesFull(Value $a, Value $b): int
{
$sort = Value::sortByAccess($a, $b);
if ($sort) {
return $sort;
}
$sort = Value::sortByName($a, $b);
if ($sort) {
return $sort;
}
return InstanceValue::sortByHierarchy($a->owner_class, $b->owner_class);
}
/**
* Sorts an array of Value.
*
* @param Value[] $contents Object properties to sort
*
* @return Value[]
*/
public static function sortProperties(array $contents, int $sort): array
{
switch ($sort) {
case self::SORT_VISIBILITY:
// Containers to quickly stable sort by type
$containers = [
Value::ACCESS_PUBLIC => [],
Value::ACCESS_PROTECTED => [],
Value::ACCESS_PRIVATE => [],
Value::ACCESS_NONE => [],
];
foreach ($contents as $item) {
$containers[$item->access][] = $item;
}
return \call_user_func_array('array_merge', $containers);
case self::SORT_FULL:
\usort($contents, [self::class, 'sortPropertiesFull']);
// no break
default:
return $contents;
}
return \str_replace(['%f', '%l'], [$file, $line], self::$file_link_format);
}
}

View File

@ -25,50 +25,40 @@ declare(strict_types=1);
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
namespace Kint\Zval;
namespace Kint\Renderer;
class InstanceValue extends Value
trait AssetRendererTrait
{
public $type = 'object';
public $classname;
public $spl_object_hash;
public $spl_object_id = null;
public $filename;
public $startline;
public $hints = ['object'];
public static ?string $theme = null;
public function getType(): ?string
/** @psalm-var array{js?:string, css?:array<path, string>} */
private static array $assetCache = [];
/** @psalm-api */
public static function renderJs(): string
{
return $this->classname;
if (!isset(self::$assetCache['js'])) {
self::$assetCache['js'] = \file_get_contents(KINT_DIR.'/resources/compiled/main.js');
}
return self::$assetCache['js'];
}
public function transplant(Value $old): void
/** @psalm-api */
public static function renderCss(): ?string
{
parent::transplant($old);
if ($old instanceof self) {
$this->classname = $old->classname;
$this->spl_object_hash = $old->spl_object_hash;
$this->spl_object_id = $old->spl_object_id;
$this->filename = $old->filename;
$this->startline = $old->startline;
}
}
/**
* @psalm-param class-string $a
* @psalm-param class-string $b
*/
public static function sortByHierarchy(string $a, string $b): int
{
if (\is_subclass_of($a, $b)) {
return -1;
if (!isset(self::$theme)) {
return null;
}
if (\is_subclass_of($b, $a)) {
return 1;
if (!isset(self::$assetCache['css'][self::$theme])) {
if (\file_exists(KINT_DIR.'/resources/compiled/'.self::$theme)) {
self::$assetCache['css'][self::$theme] = \file_get_contents(KINT_DIR.'/resources/compiled/'.self::$theme);
} else {
self::$assetCache['css'][self::$theme] = \file_get_contents(self::$theme);
}
}
return 0;
return self::$assetCache['css'][self::$theme];
}
}

View File

@ -27,7 +27,7 @@ declare(strict_types=1);
namespace Kint\Renderer;
use Kint\Zval\Value;
use Kint\Value\AbstractValue;
use Throwable;
class CliRenderer extends TextRenderer
@ -35,52 +35,46 @@ class CliRenderer extends TextRenderer
/**
* @var bool enable colors
*/
public static $cli_colors = true;
/**
* Forces utf8 output on windows.
*
* @var bool
*/
public static $force_utf8 = false;
public static bool $cli_colors = true;
/**
* Detects the terminal width on startup.
*
* @var bool
*/
public static $detect_width = true;
public static bool $detect_width = true;
/**
* The minimum width to detect terminal size as.
*
* Less than this is ignored and falls back to default width.
*
* @var int
*/
public static $min_terminal_width = 40;
public static int $min_terminal_width = 40;
/**
* Forces utf8 output on windows.
*/
public static bool $force_utf8 = false;
/**
* Which stream to check for VT100 support on windows.
*
* uses STDOUT by default if it's defined
*
* @var ?resource
* @psalm-var ?resource
*/
public static $windows_stream = null;
protected static $terminal_width = null;
protected static ?int $terminal_width = null;
protected $windows_output = false;
protected bool $windows_output = false;
protected $colors = false;
protected bool $colors = false;
public function __construct()
{
parent::__construct();
if (!self::$force_utf8 && KINT_WIN) {
if (!KINT_PHP72 || !\function_exists('sapi_windows_vt100_support')) {
if (!\function_exists('sapi_windows_vt100_support')) {
$this->windows_output = true;
} else {
$stream = self::$windows_stream;
@ -97,16 +91,23 @@ class CliRenderer extends TextRenderer
}
}
if (!self::$terminal_width) {
if (!KINT_WIN && self::$detect_width) {
if (null === self::$terminal_width) {
if (self::$detect_width) {
try {
self::$terminal_width = (int) \exec('tput cols');
$tput = KINT_WIN ? \exec('tput cols 2>nul') : \exec('tput cols 2>/dev/null');
if ((bool) $tput) {
/**
* @psalm-suppress InvalidCast
* Psalm bug #11080
*/
self::$terminal_width = (int) $tput;
}
} catch (Throwable $t) {
self::$terminal_width = self::$default_width;
}
}
if (self::$terminal_width < self::$min_terminal_width) {
if (!isset(self::$terminal_width) || self::$terminal_width < self::$min_terminal_width) {
self::$terminal_width = self::$default_width;
}
}
@ -143,13 +144,13 @@ class CliRenderer extends TextRenderer
return "\x1b[36m".\str_replace("\n", "\x1b[0m\n\x1b[36m", $string)."\x1b[0m";
}
public function renderTitle(Value $o): string
public function renderTitle(AbstractValue $v): string
{
if ($this->windows_output) {
return $this->utf8ToWindows(parent::renderTitle($o));
return $this->utf8ToWindows(parent::renderTitle($v));
}
return parent::renderTitle($o);
return parent::renderTitle($v);
}
public function preRender(): string

View File

@ -25,14 +25,9 @@ declare(strict_types=1);
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
namespace Kint\Renderer\Text;
namespace Kint\Renderer;
use Kint\Zval\Value;
class DepthLimitPlugin extends AbstractPlugin
interface ConstructableRendererInterface extends RendererInterface
{
public function render(Value $o): string
{
return $this->renderLockedHeader($o, 'DEPTH LIMIT');
}
public function __construct();
}

View File

@ -27,46 +27,38 @@ declare(strict_types=1);
namespace Kint\Renderer;
use Kint\Kint;
use Kint\Zval\BlobValue;
use Kint\Zval\Value;
use Kint\Utils;
use Kint\Value\AbstractValue;
class PlainRenderer extends TextRenderer
{
public static $pre_render_sources = [
use AssetRendererTrait;
public static array $pre_render_sources = [
'script' => [
['Kint\\Renderer\\PlainRenderer', 'renderJs'],
['Kint\\Renderer\\Text\\MicrotimePlugin', 'renderJs'],
[self::class, 'renderJs'],
],
'style' => [
['Kint\\Renderer\\PlainRenderer', 'renderCss'],
[self::class, 'renderCss'],
],
'raw' => [],
];
/**
* Path to the CSS file to load by default.
*
* @var string
*/
public static $theme = 'plain.css';
/**
* Output htmlentities instead of utf8.
*
* @var bool
*/
public static $disable_utf8 = false;
public static bool $disable_utf8 = false;
public static $needs_pre_render = true;
public static bool $needs_pre_render = true;
public static $always_pre_render = false;
public static bool $always_pre_render = false;
protected $force_pre_render = false;
protected bool $force_pre_render = false;
public function __construct()
{
parent::__construct();
self::$theme ??= 'plain.css';
$this->setForcePreRender(self::$always_pre_render);
}
@ -74,7 +66,7 @@ class PlainRenderer extends TextRenderer
{
parent::setCallInfo($info);
if (\in_array('@', $this->call_info['modifiers'], true)) {
if (\in_array('@', $info['modifiers'], true)) {
$this->setForcePreRender(true);
}
}
@ -118,13 +110,13 @@ class PlainRenderer extends TextRenderer
return '<u>'.$string.'</u>';
}
public function renderTitle(Value $o): string
public function renderTitle(AbstractValue $v): string
{
if (self::$disable_utf8) {
return $this->utf8ToHtmlentity(parent::renderTitle($o));
return $this->utf8ToHtmlentity(parent::renderTitle($v));
}
return parent::renderTitle($o);
return parent::renderTitle($v);
}
public function preRender(): string
@ -144,10 +136,18 @@ class PlainRenderer extends TextRenderer
switch ($type) {
case 'script':
$output .= '<script class="kint-plain-script">'.$contents.'</script>';
$output .= '<script class="kint-plain-script"';
if (null !== self::$js_nonce) {
$output .= ' nonce="'.\htmlspecialchars(self::$js_nonce).'"';
}
$output .= '>'.$contents.'</script>';
break;
case 'style':
$output .= '<style class="kint-plain-style">'.$contents.'</style>';
$output .= '<style class="kint-plain-style"';
if (null !== self::$css_nonce) {
$output .= ' nonce="'.\htmlspecialchars(self::$css_nonce).'"';
}
$output .= '>'.$contents.'</style>';
break;
default:
$output .= $contents;
@ -174,26 +174,20 @@ class PlainRenderer extends TextRenderer
public function ideLink(string $file, int $line): string
{
$path = $this->escape(Kint::shortenPath($file)).':'.$line;
$ideLink = Kint::getIdeLink($file, $line);
$path = $this->escape(Utils::shortenPath($file)).':'.$line;
$ideLink = self::getFileLink($file, $line);
if (!$ideLink) {
if (null === $ideLink) {
return $path;
}
$class = '';
if (\preg_match('/https?:\\/\\//i', $ideLink)) {
$class = 'class="kint-ide-link" ';
}
return '<a '.$class.'href="'.$this->escape($ideLink).'">'.$path.'</a>';
return '<a href="'.$this->escape($ideLink).'">'.$path.'</a>';
}
public function escape(string $string, $encoding = false): string
{
if (false === $encoding) {
$encoding = BlobValue::detectEncoding($string);
$encoding = Utils::detectEncoding($string);
}
$original_encoding = $encoding;
@ -220,18 +214,4 @@ class PlainRenderer extends TextRenderer
$string
);
}
protected static function renderJs(): string
{
return \file_get_contents(KINT_DIR.'/resources/compiled/shared.js').\file_get_contents(KINT_DIR.'/resources/compiled/plain.js');
}
protected static function renderCss(): string
{
if (\file_exists(KINT_DIR.'/resources/compiled/'.self::$theme)) {
return \file_get_contents(KINT_DIR.'/resources/compiled/'.self::$theme);
}
return \file_get_contents(self::$theme);
}
}

View File

@ -27,28 +27,18 @@ declare(strict_types=1);
namespace Kint\Renderer;
use Kint\Zval\Value;
use Kint\Value\AbstractValue;
interface RendererInterface
{
public function __construct();
public function render(AbstractValue $v): string;
public function render(Value $o): string;
public function renderNothing(): string;
public function shouldRenderObjectIds(): bool;
public function setCallInfo(array $info): void;
public function getCallInfo(): array;
public function setStatics(array $statics): void;
public function getStatics(): array;
public function setShowTrace(bool $show_trace): void;
public function getShowTrace(): bool;
public function filterParserPlugins(array $plugins): array;
public function preRender(): string;

View File

@ -28,15 +28,14 @@ declare(strict_types=1);
namespace Kint\Renderer\Rich;
use Kint\Renderer\RichRenderer;
use Kint\Zval\InstanceValue;
use Kint\Zval\Value;
use Kint\Value\AbstractValue;
use Kint\Value\Context\ClassDeclaredContext;
use Kint\Value\Context\PropertyContext;
use Kint\Value\InstanceValue;
/**
* @psalm-consistent-constructor
*/
abstract class AbstractPlugin implements PluginInterface
{
protected $renderer;
protected RichRenderer $renderer;
public function __construct(RichRenderer $r)
{
@ -46,47 +45,51 @@ abstract class AbstractPlugin implements PluginInterface
/**
* @param string $content The replacement for the getValueShort contents
*/
public function renderLockedHeader(Value $o, string $content): string
public function renderLockedHeader(AbstractValue $v, string $content): string
{
$header = '<dt class="kint-parent kint-locked">';
if (RichRenderer::$access_paths && $o->depth > 0 && $ap = $o->getAccessPath()) {
$c = $v->getContext();
if (RichRenderer::$access_paths && $c->getDepth() > 0 && null !== ($ap = $c->getAccessPath())) {
$header .= '<span class="kint-access-path-trigger" title="Show access path">&rlarr;</span>';
}
$header .= '<span class="kint-popup-trigger" title="Open in new window">&boxbox;</span><nav></nav>';
$header .= '<nav></nav>';
if (null !== ($s = $o->getModifiers())) {
$header .= '<var>'.$s.'</var> ';
if ($c instanceof ClassDeclaredContext) {
$header .= '<var>'.$c->getModifiers().'</var> ';
}
if (null !== ($s = $o->getName())) {
$header .= '<dfn>'.$this->renderer->escape($s).'</dfn> ';
$header .= '<dfn>'.$this->renderer->escape($v->getDisplayName()).'</dfn> ';
if ($s = $o->getOperator()) {
$header .= $this->renderer->escape($s, 'ASCII').' ';
}
if ($c instanceof PropertyContext && null !== ($s = $c->getHooks())) {
$header .= '<var>'.$this->renderer->escape($s).'</var> ';
}
if (null !== ($s = $o->getType())) {
if (RichRenderer::$escape_types) {
$s = $this->renderer->escape($s);
}
if ($o->reference) {
$s = '&amp;'.$s;
}
$header .= '<var>'.$s.'</var>';
if ($o instanceof InstanceValue && isset($o->spl_object_id)) {
$header .= '#'.((int) $o->spl_object_id);
}
$header .= ' ';
if (null !== ($s = $c->getOperator())) {
$header .= $this->renderer->escape($s, 'ASCII').' ';
}
if (null !== ($s = $o->getSize())) {
$s = $v->getDisplayType();
if (RichRenderer::$escape_types) {
$s = $this->renderer->escape($s);
}
if ($c->isRef()) {
$s = '&amp;'.$s;
}
$header .= '<var>'.$s.'</var>';
if ($v instanceof InstanceValue && $this->renderer->shouldRenderObjectIds()) {
$header .= '#'.$v->getSplObjectId();
}
$header .= ' ';
if (null !== ($s = $v->getDisplaySize())) {
if (RichRenderer::$escape_types) {
$s = $this->renderer->escape($s);
}

View File

@ -27,29 +27,31 @@ declare(strict_types=1);
namespace Kint\Renderer\Rich;
use Kint\Zval\Representation\Representation;
use Kint\Value\AbstractValue;
use Kint\Value\Representation\BinaryRepresentation;
use Kint\Value\Representation\RepresentationInterface;
class BinaryPlugin extends AbstractPlugin implements TabPluginInterface
{
/** @psalm-var positive-int */
public static $line_length = 0x10;
public static int $line_length = 0x10;
/** @psalm-var positive-int */
public static $chunk_length = 0x4;
public static int $chunk_length = 0x2;
public function renderTab(Representation $r): ?string
public function renderTab(RepresentationInterface $r, AbstractValue $v): ?string
{
if (!\is_string($r->contents)) {
if (!$r instanceof BinaryRepresentation) {
return null;
}
$out = '<pre>';
$lines = \str_split($r->contents, self::$line_length);
$lines = \str_split($r->getValue(), self::$line_length);
foreach ($lines as $index => $line) {
$out .= \sprintf('%08X', $index * self::$line_length).":\t";
$chunks = \str_split(\str_pad(\bin2hex($line), 2 * self::$line_length, ' '), self::$chunk_length);
$chunks = \str_split(\str_pad(\bin2hex($line), 2 * self::$line_length, ' '), 2 * self::$chunk_length);
$out .= \implode(' ', $chunks);
$out .= "\t".\preg_replace('/[^\\x20-\\x7E]/', '.', $line)."\n";

View File

@ -27,38 +27,38 @@ declare(strict_types=1);
namespace Kint\Renderer\Rich;
use Kint\Kint;
use Kint\Zval\ClosureValue;
use Kint\Zval\Value;
use Kint\Utils;
use Kint\Value\AbstractValue;
use Kint\Value\MethodValue;
use Kint\Value\Representation\CallableDefinitionRepresentation;
use Kint\Value\Representation\RepresentationInterface;
class ClosurePlugin extends AbstractPlugin implements ValuePluginInterface
class CallableDefinitionPlugin extends AbstractPlugin implements TabPluginInterface
{
public function renderValue(Value $o): ?string
public function renderTab(RepresentationInterface $r, AbstractValue $v): ?string
{
if (!$o instanceof ClosureValue) {
if (!$r instanceof CallableDefinitionRepresentation) {
return null;
}
$children = $this->renderer->renderChildren($o);
$docstring = [];
$header = '';
if ($v instanceof MethodValue) {
$c = $v->getContext();
if (null !== ($s = $o->getModifiers())) {
$header .= '<var>'.$s.'</var> ';
if ($c->inherited) {
$docstring[] = 'Inherited from '.$this->renderer->escape($c->owner_class);
}
}
if (null !== ($s = $o->getName())) {
$header .= '<dfn>'.$this->renderer->escape($s).'('.$this->renderer->escape($o->getParams()).')</dfn> ';
$docstring[] = 'Defined in '.$this->renderer->escape(Utils::shortenPath($r->getFileName())).':'.$r->getLine();
$docstring = '<small>'.\implode("\n", $docstring).'</small>';
if (null !== ($trimmed = $r->getDocstringTrimmed())) {
$docstring = $this->renderer->escape($trimmed)."\n\n".$docstring;
}
$header .= '<var>Closure</var>';
if (isset($o->spl_object_id)) {
$header .= '#'.((int) $o->spl_object_id);
}
$header .= ' '.$this->renderer->escape(Kint::shortenPath($o->filename)).':'.(int) $o->startline;
$header = $this->renderer->renderHeaderWrapper($o, (bool) \strlen($children), $header);
return '<dl>'.$header.$children.'</dl>';
return '<pre>'.$docstring.'</pre>';
}
}

View File

@ -29,101 +29,76 @@ namespace Kint\Renderer\Rich;
use Kint\Renderer\RichRenderer;
use Kint\Utils;
use Kint\Zval\ClosureValue;
use Kint\Zval\MethodValue;
use Kint\Zval\Value;
use Kint\Value\AbstractValue;
use Kint\Value\Context\MethodContext;
use Kint\Value\MethodValue;
class CallablePlugin extends ClosurePlugin
class CallablePlugin extends AbstractPlugin implements ValuePluginInterface
{
protected static $method_cache = [];
protected static array $method_cache = [];
protected $closure_plugin = null;
public function renderValue(Value $o): ?string
public function renderValue(AbstractValue $v): ?string
{
if ($o instanceof MethodValue) {
return $this->renderMethod($o);
if (!$v instanceof MethodValue) {
return null;
}
if ($o instanceof ClosureValue) {
return parent::renderValue($o);
$c = $v->getContext();
if (!$c instanceof MethodContext) {
return null;
}
return null;
}
if (!isset(self::$method_cache[$c->owner_class][$c->name])) {
$children = $this->renderer->renderChildren($v);
protected function renderMethod(MethodValue $o): string
{
if (!empty(self::$method_cache[$o->owner_class][$o->name])) {
$children = self::$method_cache[$o->owner_class][$o->name]['children'];
$header = '<var>'.$c->getModifiers();
$header = $this->renderer->renderHeaderWrapper(
$o,
(bool) \strlen($children),
self::$method_cache[$o->owner_class][$o->name]['header']
);
return '<dl>'.$header.$children.'</dl>';
}
$children = $this->renderer->renderChildren($o);
$header = '';
if (null !== ($s = $o->getModifiers()) || $o->return_reference) {
$header .= '<var>'.$s;
if ($o->return_reference) {
if ($s) {
$header .= ' ';
}
$header .= $this->renderer->escape('&');
if ($v->getCallableBag()->return_reference) {
$header .= ' &amp;';
}
$header .= '</var> ';
}
if (null !== ($s = $o->getName())) {
$function = $this->renderer->escape($s).'('.$this->renderer->escape($o->getParams()).')';
$function = $this->renderer->escape($v->getDisplayName());
if (null !== ($url = $o->getPhpDocUrl())) {
if (null !== ($url = $v->getPhpDocUrl())) {
$function = '<a href="'.$url.'" target=_blank>'.$function.'</a>';
}
$header .= '<dfn>'.$function.'</dfn>';
}
if (!empty($o->returntype)) {
$header .= ': <var>';
if ($o->return_reference) {
$header .= $this->renderer->escape('&');
}
$header .= $this->renderer->escape($o->returntype).'</var>';
} elseif ($o->docstring) {
if (\preg_match('/@return\\s+(.*)\\r?\\n/m', $o->docstring, $matches)) {
if (\trim($matches[1])) {
$header .= ': <var>'.$this->renderer->escape(\trim($matches[1])).'</var>';
if (null !== ($rt = $v->getCallableBag()->returntype)) {
$header .= ': <var>';
$header .= $this->renderer->escape($rt).'</var>';
} elseif (null !== ($ds = $v->getCallableBag()->docstring)) {
if (\preg_match('/@return\\s+(.*)\\r?\\n/m', $ds, $matches)) {
if (\trim($matches[1])) {
$header .= ': <var>'.$this->renderer->escape(\trim($matches[1])).'</var>';
}
}
}
}
if (null !== ($s = $o->getValueShort())) {
if (RichRenderer::$strlen_max) {
$s = Utils::truncateString($s, RichRenderer::$strlen_max);
if (null !== ($s = $v->getDisplayValue())) {
if (RichRenderer::$strlen_max) {
$s = Utils::truncateString($s, RichRenderer::$strlen_max);
}
$header .= ' '.$this->renderer->escape($s);
}
$header .= ' '.$this->renderer->escape($s);
}
if (\strlen($o->owner_class) && \strlen($o->name)) {
self::$method_cache[$o->owner_class][$o->name] = [
self::$method_cache[$c->owner_class][$c->name] = [
'header' => $header,
'children' => $children,
];
}
$header = $this->renderer->renderHeaderWrapper($o, (bool) \strlen($children), $header);
$children = self::$method_cache[$c->owner_class][$c->name]['children'];
$header = $this->renderer->renderHeaderWrapper(
$c,
(bool) \strlen($children),
self::$method_cache[$c->owner_class][$c->name]['header']
);
return '<dl>'.$header.$children.'</dl>';
}

View File

@ -27,33 +27,33 @@ declare(strict_types=1);
namespace Kint\Renderer\Rich;
use Kint\Zval\Representation\ColorRepresentation;
use Kint\Zval\Representation\Representation;
use Kint\Zval\Value;
use Kint\Value\AbstractValue;
use Kint\Value\Representation\ColorRepresentation;
use Kint\Value\Representation\RepresentationInterface;
class ColorPlugin extends AbstractPlugin implements TabPluginInterface, ValuePluginInterface
{
public function renderValue(Value $o): ?string
public function renderValue(AbstractValue $v): ?string
{
$r = $o->getRepresentation('color');
$r = $v->getRepresentation('color');
if (!$r instanceof ColorRepresentation) {
return null;
}
$children = $this->renderer->renderChildren($o);
$children = $this->renderer->renderChildren($v);
$header = $this->renderer->renderHeader($o);
$header = $this->renderer->renderHeader($v);
$header .= '<div class="kint-color-preview"><div style="background:';
$header .= $r->getColor(ColorRepresentation::COLOR_RGBA);
$header .= '"></div></div>';
$header = $this->renderer->renderHeaderWrapper($o, (bool) \strlen($children), $header);
$header = $this->renderer->renderHeaderWrapper($v->getContext(), (bool) \strlen($children), $header);
return '<dl>'.$header.$children.'</dl>';
}
public function renderTab(Representation $r): ?string
public function renderTab(RepresentationInterface $r, AbstractValue $v): ?string
{
if (!$r instanceof ColorRepresentation) {
return null;

View File

@ -0,0 +1,49 @@
<?php
declare(strict_types=1);
/*
* The MIT License (MIT)
*
* Copyright (c) 2013 Jonathan Vollebregt (jnvsor@gmail.com), Rokas Šleinius (raveren@gmail.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
namespace Kint\Renderer\Rich;
use Kint\Value\AbstractValue;
class LockPlugin extends AbstractPlugin implements ValuePluginInterface
{
public function renderValue(AbstractValue $v): ?string
{
switch ($v->getHint()) {
case 'blacklist':
return '<dl>'.$this->renderLockedHeader($v, '<var>Blacklisted</var>').'</dl>';
case 'recursion':
return '<dl>'.$this->renderLockedHeader($v, '<var>Recursion</var>').'</dl>';
case 'depth_limit':
return '<dl>'.$this->renderLockedHeader($v, '<var>Depth Limit</var>').'</dl>';
case 'array_limit':
return '<dl>'.$this->renderLockedHeader($v, '<var>Array Limit</var>').'</dl>';
}
return null;
}
}

View File

@ -28,47 +28,39 @@ declare(strict_types=1);
namespace Kint\Renderer\Rich;
use Kint\Utils;
use Kint\Zval\Representation\MicrotimeRepresentation;
use Kint\Zval\Representation\Representation;
use Kint\Value\AbstractValue;
use Kint\Value\Representation\MicrotimeRepresentation;
use Kint\Value\Representation\RepresentationInterface;
class MicrotimePlugin extends AbstractPlugin implements TabPluginInterface
{
public function renderTab(Representation $r): ?string
public function renderTab(RepresentationInterface $r, AbstractValue $v): ?string
{
if (!$r instanceof MicrotimeRepresentation || !($dt = $r->getDateTime())) {
return null;
}
$out = $dt->format('Y-m-d H:i:s.u');
if (null !== $r->lap) {
$out .= '<br><b>SINCE LAST CALL:</b> <span class="kint-microtime-lap">'.\round($r->lap, 4).'</span>s.';
if (null !== ($lap = $r->getLapTime())) {
$out .= '<br><b>SINCE LAST CALL:</b> <span class="kint-microtime-lap">'.\round($lap, 4).'</span>s.';
}
if (null !== $r->total) {
$out .= '<br><b>SINCE START:</b> '.\round($r->total, 4).'s.';
if (null !== ($total = $r->getTotalTime())) {
$out .= '<br><b>SINCE START:</b> '.\round($total, 4).'s.';
}
if (null !== $r->avg) {
$out .= '<br><b>AVERAGE DURATION:</b> <span class="kint-microtime-avg">'.\round($r->avg, 4).'</span>s.';
if (null !== ($avg = $r->getAverageTime())) {
$out .= '<br><b>AVERAGE DURATION:</b> <span class="kint-microtime-avg">'.\round($avg, 4).'</span>s.';
}
$bytes = Utils::getHumanReadableBytes($r->mem);
$out .= '<br><b>MEMORY USAGE:</b> '.$r->mem.' bytes ('.\round($bytes['value'], 3).' '.$bytes['unit'].')';
$bytes = Utils::getHumanReadableBytes($r->mem_real);
$bytes = Utils::getHumanReadableBytes($r->getMemoryUsage());
$out .= '<br><b>MEMORY USAGE:</b> '.$r->getMemoryUsage().' bytes ('.\round($bytes['value'], 3).' '.$bytes['unit'].')';
$bytes = Utils::getHumanReadableBytes($r->getMemoryUsageReal());
$out .= ' (real '.\round($bytes['value'], 3).' '.$bytes['unit'].')';
$bytes = Utils::getHumanReadableBytes($r->mem_peak);
$out .= '<br><b>PEAK MEMORY USAGE:</b> '.$r->mem_peak.' bytes ('.\round($bytes['value'], 3).' '.$bytes['unit'].')';
$bytes = Utils::getHumanReadableBytes($r->mem_peak_real);
$bytes = Utils::getHumanReadableBytes($r->getMemoryPeakUsage());
$out .= '<br><b>PEAK MEMORY USAGE:</b> '.$r->getMemoryPeakUsage().' bytes ('.\round($bytes['value'], 3).' '.$bytes['unit'].')';
$bytes = Utils::getHumanReadableBytes($r->getMemoryPeakUsageReal());
$out .= ' (real '.\round($bytes['value'], 3).' '.$bytes['unit'].')';
return '<pre data-kint-microtime-group="'.$r->group.'">'.$out.'</pre>';
}
public static function renderJs(): string
{
if (\is_string($out = \file_get_contents(KINT_DIR.'/resources/compiled/microtime.js'))) {
return $out;
}
return '';
return '<pre data-kint-microtime-group="'.$r->getGroup().'">'.$out.'</pre>';
}
}

View File

@ -27,30 +27,30 @@ declare(strict_types=1);
namespace Kint\Renderer\Rich;
use Kint\Zval\BlobValue;
use Kint\Zval\SimpleXMLElementValue;
use Kint\Zval\Value;
use Kint\Value\AbstractValue;
use Kint\Value\Representation\ProfileRepresentation;
use Kint\Value\Representation\RepresentationInterface;
class SimpleXMLElementPlugin extends AbstractPlugin implements ValuePluginInterface
class ProfilePlugin extends AbstractPlugin implements TabPluginInterface
{
public function renderValue(Value $o): ?string
public function renderTab(RepresentationInterface $r, AbstractValue $v): ?string
{
if (!($o instanceof SimpleXMLElementValue)) {
if (!$r instanceof ProfileRepresentation) {
return null;
}
if (!$o->isStringValue() || !empty($o->getRepresentation('attributes')->contents)) {
return null;
$out = '<pre>';
$out .= 'Complexity: '.$r->complexity.PHP_EOL;
if (isset($r->instance_counts)) {
$out .= 'Instance repetitions: '.\var_export($r->instance_counts, true).PHP_EOL;
}
if (isset($r->instance_complexity)) {
$out .= 'Instance complexity: '.\var_export($r->instance_complexity, true).PHP_EOL;
}
$b = new BlobValue();
$b->transplant($o);
$b->type = 'string';
$out .= '</pre>';
$children = $this->renderer->renderChildren($b);
$header = $this->renderer->renderHeader($o);
$header = $this->renderer->renderHeaderWrapper($o, (bool) \strlen($children), $header);
return '<dl>'.$header.$children.'</dl>';
return $out;
}
}

View File

@ -27,22 +27,23 @@ declare(strict_types=1);
namespace Kint\Renderer\Rich;
use Kint\Zval\Representation\Representation;
use Kint\Zval\Representation\SourceRepresentation;
use Kint\Value\AbstractValue;
use Kint\Value\Representation\RepresentationInterface;
use Kint\Value\Representation\SourceRepresentation;
class SourcePlugin extends AbstractPlugin implements TabPluginInterface
{
public function renderTab(Representation $r): ?string
public function renderTab(RepresentationInterface $r, AbstractValue $v): ?string
{
if (!($r instanceof SourceRepresentation) || empty($r->source)) {
if (!$r instanceof SourceRepresentation) {
return null;
}
$source = $r->source;
$source = $r->getSourceLines();
// Trim empty lines from the start and end of the source
foreach ($source as $linenum => $line) {
if (\strlen(\trim($line)) || $linenum === $r->line) {
if (\strlen(\trim($line)) || $linenum === $r->getLine()) {
break;
}
@ -50,7 +51,7 @@ class SourcePlugin extends AbstractPlugin implements TabPluginInterface
}
foreach (\array_reverse($source, true) as $linenum => $line) {
if (\strlen(\trim($line)) || $linenum === $r->line) {
if (\strlen(\trim($line)) || $linenum === $r->getLine()) {
break;
}
@ -60,7 +61,7 @@ class SourcePlugin extends AbstractPlugin implements TabPluginInterface
$output = '';
foreach ($source as $linenum => $line) {
if ($linenum === $r->line) {
if ($linenum === $r->getLine()) {
$output .= '<div class="kint-highlight">'.$this->renderer->escape($line)."\n".'</div>';
} else {
$output .= '<div>'.$this->renderer->escape($line)."\n".'</div>';
@ -68,14 +69,12 @@ class SourcePlugin extends AbstractPlugin implements TabPluginInterface
}
if ($output) {
\reset($source);
$data = '';
if ($r->showfilename) {
$data = ' data-kint-filename="'.$this->renderer->escape($r->filename).'"';
if ($r->showFileName()) {
$data = ' data-kint-filename="'.$this->renderer->escape($r->getFileName()).'"';
}
return '<div><pre class="kint-source"'.$data.' style="counter-reset: kint-l '.((int) \key($source) - 1).';">'.$output.'</pre></div><div></div>';
return '<div><pre class="kint-source"'.$data.' style="counter-reset: kint-l '.((int) \array_key_first($source) - 1).';">'.$output.'</pre></div><div></div>';
}
return null;

View File

@ -27,9 +27,10 @@ declare(strict_types=1);
namespace Kint\Renderer\Rich;
use Kint\Zval\Representation\Representation;
use Kint\Value\AbstractValue;
use Kint\Value\Representation\RepresentationInterface;
interface TabPluginInterface extends PluginInterface
{
public function renderTab(Representation $r): ?string;
public function renderTab(RepresentationInterface $r, AbstractValue $v): ?string;
}

View File

@ -29,102 +29,93 @@ namespace Kint\Renderer\Rich;
use Kint\Renderer\RichRenderer;
use Kint\Utils;
use Kint\Zval\Representation\Representation;
use Kint\Value\AbstractValue;
use Kint\Value\ArrayValue;
use Kint\Value\FixedWidthValue;
use Kint\Value\Representation\RepresentationInterface;
use Kint\Value\Representation\TableRepresentation;
use Kint\Value\StringValue;
class TablePlugin extends AbstractPlugin implements TabPluginInterface
{
public static $respect_str_length = true;
public static bool $respect_str_length = true;
public function renderTab(Representation $r): string
public function renderTab(RepresentationInterface $r, AbstractValue $v): ?string
{
if (!$r instanceof TableRepresentation) {
return null;
}
$contents = $r->getContents();
$firstrow = \reset($contents);
if (!$firstrow instanceof ArrayValue) {
return null;
}
$out = '<pre><table><thead><tr><th></th>';
$firstrow = \reset($r->contents);
foreach ($firstrow->value->contents as $field) {
$out .= '<th>';
if (null !== ($s = $field->getName())) {
$out .= $this->renderer->escape($s);
}
$out .= '</th>';
foreach ($firstrow->getContents() as $field) {
$out .= '<th>'.$this->renderer->escape($field->getDisplayName()).'</th>';
}
$out .= '</tr></thead><tbody>';
foreach ($r->contents as $row) {
$out .= '<tr><th>';
if (null !== ($s = $row->getName())) {
$out .= $this->renderer->escape($s);
foreach ($contents as $row) {
if (!$row instanceof ArrayValue) {
return null;
}
$out .= '</th>';
foreach ($row->value->contents as $field) {
$out .= '<td';
$type = '';
$size = '';
$ref = '';
$out .= '<tr><th>'.$this->renderer->escape($row->getDisplayName()).'</th>';
if (null !== ($s = $field->getType())) {
$type = $this->renderer->escape($s);
foreach ($row->getContents() as $field) {
$ref = $field->getContext()->isRef() ? '&amp;' : '';
$type = $this->renderer->escape($field->getDisplayType());
if ($field->reference) {
$ref = '&amp;';
$type = $ref.$type;
}
$out .= '<td title="'.$ref.$type;
if (null !== ($s = $field->getSize())) {
$size .= ' ('.$this->renderer->escape($s).')';
}
if (null !== ($size = $field->getDisplaySize())) {
$size = $this->renderer->escape($size);
$out .= ' ('.$size.')';
}
if ($type) {
$out .= ' title="'.$type.$size.'"';
}
$out .= '">';
$out .= '>';
switch ($field->type) {
case 'boolean':
$out .= $field->value->contents ? '<var>'.$ref.'true</var>' : '<var>'.$ref.'false</var>';
break;
case 'integer':
case 'double':
$out .= (string) $field->value->contents;
break;
case 'null':
if ($field instanceof FixedWidthValue) {
if (null === ($dv = $field->getDisplayValue())) {
$out .= '<var>'.$ref.'null</var>';
break;
case 'string':
if ($field->encoding) {
$val = $field->value->contents;
if (RichRenderer::$strlen_max && self::$respect_str_length) {
$val = Utils::truncateString($val, RichRenderer::$strlen_max);
}
} elseif ('boolean' === $field->getType()) {
$out .= '<var>'.$ref.$dv.'</var>';
} else {
$out .= $dv;
}
} elseif ($field instanceof StringValue) {
if (false !== $field->getEncoding()) {
$val = $field->getValueUtf8();
$out .= $this->renderer->escape($val);
} else {
$out .= '<var>'.$type.'</var>';
if (RichRenderer::$strlen_max && self::$respect_str_length) {
$val = Utils::truncateString($val, RichRenderer::$strlen_max, 'UTF-8');
}
break;
case 'array':
$out .= '<var>'.$ref.'array</var>'.$size;
break;
case 'object':
$out .= '<var>'.$ref.$this->renderer->escape($field->classname).'</var>'.$size;
break;
case 'resource':
$out .= '<var>'.$ref.'resource</var>';
break;
default:
$out .= '<var>'.$ref.'unknown</var>';
break;
$out .= $this->renderer->escape($val);
} else {
$out .= '<var>'.$ref.$type.'</var>';
}
} elseif ($field instanceof ArrayValue) {
$out .= '<var>'.$ref.'array</var> ('.$field->getSize().')';
} else {
$out .= '<var>'.$ref.$type.'</var>';
if (null !== $size) {
$out .= ' ('.$size.')';
}
}
if (\in_array('blacklist', $field->hints, true)) {
if ($field->flags & AbstractValue::FLAG_BLACKLIST) {
$out .= ' <var>Blacklisted</var>';
} elseif (\in_array('recursion', $field->hints, true)) {
} elseif ($field->flags & AbstractValue::FLAG_RECURSION) {
$out .= ' <var>Recursion</var>';
} elseif (\in_array('depth_limit', $field->hints, true)) {
} elseif ($field->flags & AbstractValue::FLAG_DEPTH_LIMIT) {
$out .= ' <var>Depth Limit</var>';
}

View File

@ -27,43 +27,35 @@ declare(strict_types=1);
namespace Kint\Renderer\Rich;
use Kint\Zval\TraceFrameValue;
use Kint\Zval\Value;
use Kint\Value\AbstractValue;
use Kint\Value\TraceFrameValue;
class TraceFramePlugin extends AbstractPlugin implements ValuePluginInterface
{
public function renderValue(Value $o): ?string
public function renderValue(AbstractValue $v): ?string
{
if (!$o instanceof TraceFrameValue) {
if (!$v instanceof TraceFrameValue) {
return null;
}
if (!empty($o->trace['file']) && !empty($o->trace['line'])) {
$header = '<var>'.$this->renderer->ideLink($o->trace['file'], (int) $o->trace['line']).'</var> ';
if (null !== ($file = $v->getFile()) && null !== ($line = $v->getLine())) {
$header = '<var>'.$this->renderer->ideLink($file, $line).'</var> ';
} else {
$header = '<var>PHP internal call</var> ';
}
if ($o->trace['class']) {
$header .= $this->renderer->escape($o->trace['class'].$o->trace['type']);
}
if ($callable = $v->getCallable()) {
$function = $this->renderer->escape($callable->getDisplayName());
if (\is_string($o->trace['function'])) {
$function = $this->renderer->escape($o->trace['function'].'()');
} else {
$function = $this->renderer->escape(
$o->trace['function']->getName().'('.$o->trace['function']->getParams().')'
);
if (null !== ($url = $o->trace['function']->getPhpDocUrl())) {
if (null !== ($url = $callable->getPhpDocUrl())) {
$function = '<a href="'.$url.'" target=_blank>'.$function.'</a>';
}
$header .= $function;
}
$header .= '<dfn>'.$function.'</dfn>';
$children = $this->renderer->renderChildren($o);
$header = $this->renderer->renderHeaderWrapper($o, (bool) \strlen($children), $header);
$children = $this->renderer->renderChildren($v);
$header = $this->renderer->renderHeaderWrapper($v->getContext(), (bool) \strlen($children), $header);
return '<dl>'.$header.$children.'</dl>';
}

View File

@ -27,9 +27,9 @@ declare(strict_types=1);
namespace Kint\Renderer\Rich;
use Kint\Zval\Value;
use Kint\Value\AbstractValue;
interface ValuePluginInterface extends PluginInterface
{
public function renderValue(Value $o): ?string;
public function renderValue(AbstractValue $v): ?string;
}

View File

@ -27,57 +27,61 @@ declare(strict_types=1);
namespace Kint\Renderer;
use Kint\Kint;
use Kint\Renderer\Rich\PluginInterface;
use Kint\Renderer\Rich\TabPluginInterface;
use Kint\Renderer\Rich\ValuePluginInterface;
use Kint\Utils;
use Kint\Zval\BlobValue;
use Kint\Zval\InstanceValue;
use Kint\Zval\Representation\Representation;
use Kint\Zval\Value;
use Kint\Value\AbstractValue;
use Kint\Value\Context\ClassDeclaredContext;
use Kint\Value\Context\ContextInterface;
use Kint\Value\Context\PropertyContext;
use Kint\Value\InstanceValue;
use Kint\Value\Representation;
use Kint\Value\Representation\ContainerRepresentation;
use Kint\Value\Representation\RepresentationInterface;
use Kint\Value\Representation\StringRepresentation;
use Kint\Value\Representation\ValueRepresentation;
use Kint\Value\StringValue;
/**
* @psalm-import-type Encoding from BlobValue
* @psalm-import-type PluginMap from AbstractRenderer
* @psalm-import-type Encoding from StringValue
*/
class RichRenderer extends AbstractRenderer
{
use AssetRendererTrait;
/**
* RichRenderer value plugins should implement ValuePluginInterface.
*
* @psalm-var PluginMap
* @psalm-var class-string<ValuePluginInterface>[]
*/
public static $value_plugins = [
'array_limit' => Rich\ArrayLimitPlugin::class,
'blacklist' => Rich\BlacklistPlugin::class,
public static array $value_plugins = [
'array_limit' => Rich\LockPlugin::class,
'blacklist' => Rich\LockPlugin::class,
'callable' => Rich\CallablePlugin::class,
'color' => Rich\ColorPlugin::class,
'depth_limit' => Rich\DepthLimitPlugin::class,
'recursion' => Rich\RecursionPlugin::class,
'simplexml_element' => Rich\SimpleXMLElementPlugin::class,
'depth_limit' => Rich\LockPlugin::class,
'recursion' => Rich\LockPlugin::class,
'trace_frame' => Rich\TraceFramePlugin::class,
];
/**
* RichRenderer tab plugins should implement TabPluginInterface.
*
* @psalm-var PluginMap
* @psalm-var array<string, class-string<TabPluginInterface>>
*/
public static $tab_plugins = [
public static array $tab_plugins = [
'binary' => Rich\BinaryPlugin::class,
'callable' => Rich\CallableDefinitionPlugin::class,
'color' => Rich\ColorPlugin::class,
'method_definition' => Rich\MethodDefinitionPlugin::class,
'microtime' => Rich\MicrotimePlugin::class,
'profiling' => Rich\ProfilePlugin::class,
'source' => Rich\SourcePlugin::class,
'table' => Rich\TablePlugin::class,
'timestamp' => Rich\TimestampPlugin::class,
];
public static $pre_render_sources = [
public static array $pre_render_sources = [
'script' => [
[self::class, 'renderJs'],
[Rich\MicrotimePlugin::class, 'renderJs'],
],
'style' => [
[self::class, 'renderCss'],
@ -85,6 +89,18 @@ class RichRenderer extends AbstractRenderer
'raw' => [],
];
/**
* The maximum length of a string before it is truncated.
*
* Falsey to disable
*/
public static int $strlen_max = 80;
/**
* Timestamp to print in footer in date() format.
*/
public static ?string $timestamp = null;
/**
* Whether or not to render access paths.
*
@ -95,88 +111,49 @@ class RichRenderer extends AbstractRenderer
* If this is an unacceptably large amount and your browser is groaning
* under the weight of the access paths - your first order of buisiness
* should be to get a new browser. Failing that, use this to turn them off.
*
* @var bool
*/
public static $access_paths = true;
/**
* The maximum length of a string before it is truncated.
*
* Falsey to disable
*
* @var int
*/
public static $strlen_max = 80;
/**
* Path to the CSS file to load by default.
*
* @var string
*/
public static $theme = 'original.css';
public static bool $access_paths = true;
/**
* Assume types and sizes don't need to be escaped.
*
* Turn this off if you use anything but ascii in your class names,
* but it'll cause a slowdown of around 10%
*
* @var bool
*/
public static $escape_types = false;
public static bool $escape_types = false;
/**
* Move all dumps to a folder at the bottom of the body.
*
* @var bool
*/
public static $folder = false;
public static bool $folder = false;
/**
* Sort mode for object properties.
*
* @var int
*/
public static $sort = self::SORT_NONE;
public static bool $needs_pre_render = true;
public static bool $always_pre_render = false;
/**
* Timestamp to print in footer in date() format.
*
* @var ?string
*/
public static $timestamp = null;
public static $needs_pre_render = true;
public static $needs_folder_render = true;
public static $always_pre_render = false;
public static $js_nonce = null;
public static $css_nonce = null;
protected $plugin_objs = [];
protected $expand = false;
protected $force_pre_render = false;
protected $use_folder = false;
protected array $plugin_objs = [];
protected bool $expand = false;
protected bool $force_pre_render = false;
protected bool $use_folder = false;
public function __construct()
{
$this->setUseFolder(self::$folder);
$this->setForcePreRender(self::$always_pre_render);
parent::__construct();
self::$theme ??= 'original.css';
$this->use_folder = self::$folder;
$this->force_pre_render = self::$always_pre_render;
}
public function setCallInfo(array $info): void
{
parent::setCallInfo($info);
if (\in_array('!', $this->call_info['modifiers'], true)) {
$this->setExpand(true);
$this->setUseFolder(false);
if (\in_array('!', $info['modifiers'], true)) {
$this->expand = true;
$this->use_folder = false;
}
if (\in_array('@', $this->call_info['modifiers'], true)) {
$this->setForcePreRender(true);
if (\in_array('@', $info['modifiers'], true)) {
$this->force_pre_render = true;
}
}
@ -185,82 +162,56 @@ class RichRenderer extends AbstractRenderer
parent::setStatics($statics);
if (!empty($statics['expanded'])) {
$this->setExpand(true);
$this->expand = true;
}
if (!empty($statics['return'])) {
$this->setForcePreRender(true);
$this->force_pre_render = true;
}
}
public function setExpand(bool $expand): void
{
$this->expand = $expand;
}
public function getExpand(): bool
{
return $this->expand;
}
public function setForcePreRender(bool $force_pre_render): void
{
$this->force_pre_render = $force_pre_render;
}
public function getForcePreRender(): bool
{
return $this->force_pre_render;
}
public function setUseFolder(bool $use_folder): void
{
$this->use_folder = $use_folder;
}
public function getUseFolder(): bool
{
return $this->use_folder;
}
public function shouldPreRender(): bool
{
return $this->getForcePreRender() || self::$needs_pre_render;
return $this->force_pre_render || self::$needs_pre_render;
}
public function shouldFolderRender(): bool
public function render(AbstractValue $v): string
{
return $this->getUseFolder() && ($this->getForcePreRender() || self::$needs_folder_render);
}
$render_spl_ids_stash = $this->render_spl_ids;
public function render(Value $o): string
{
if (($plugin = $this->getPlugin(self::$value_plugins, $o->hints)) && $plugin instanceof ValuePluginInterface) {
$output = $plugin->renderValue($o);
if ($this->render_spl_ids && $v->flags & AbstractValue::FLAG_GENERATED) {
$this->render_spl_ids = false;
}
if ($plugin = $this->getValuePlugin($v)) {
$output = $plugin->renderValue($v);
if (null !== $output && \strlen($output)) {
if (!$this->render_spl_ids && $render_spl_ids_stash) {
$this->render_spl_ids = true;
}
return $output;
}
}
$children = $this->renderChildren($o);
$header = $this->renderHeaderWrapper($o, (bool) \strlen($children), $this->renderHeader($o));
$children = $this->renderChildren($v);
$header = $this->renderHeaderWrapper($v->getContext(), (bool) \strlen($children), $this->renderHeader($v));
if (!$this->render_spl_ids && $render_spl_ids_stash) {
$this->render_spl_ids = true;
}
return '<dl>'.$header.$children.'</dl>';
}
public function renderNothing(): string
{
return '<dl><dt><var>No argument</var></dt></dl>';
}
public function renderHeaderWrapper(Value $o, bool $has_children, string $contents): string
public function renderHeaderWrapper(ContextInterface $c, bool $has_children, string $contents): string
{
$out = '<dt';
if ($has_children) {
$out .= ' class="kint-parent';
if ($this->getExpand()) {
if ($this->expand) {
$out .= ' kint-show';
}
@ -269,14 +220,15 @@ class RichRenderer extends AbstractRenderer
$out .= '>';
if (self::$access_paths && $o->depth > 0 && ($ap = $o->getAccessPath())) {
if (self::$access_paths && $c->getDepth() > 0 && null !== ($ap = $c->getAccessPath())) {
$out .= '<span class="kint-access-path-trigger" title="Show access path">&rlarr;</span>';
}
if ($has_children) {
$out .= '<span class="kint-popup-trigger" title="Open in new window">&boxbox;</span>';
if (0 === $o->depth) {
if (0 === $c->getDepth()) {
if (!$this->use_folder) {
$out .= '<span class="kint-folder-trigger" title="Move to folder">&mapstodown;</span>';
}
$out .= '<span class="kint-search-trigger" title="Show search box">&telrec;</span>';
$out .= '<input type="text" class="kint-search" value="">';
}
@ -293,48 +245,51 @@ class RichRenderer extends AbstractRenderer
return $out.'</dt>';
}
public function renderHeader(Value $o): string
public function renderHeader(AbstractValue $v): string
{
$c = $v->getContext();
$output = '';
if (null !== ($s = $o->getModifiers())) {
$output .= '<var>'.$s.'</var> ';
if ($c instanceof ClassDeclaredContext) {
$output .= '<var>'.$c->getModifiers().'</var> ';
}
if (null !== ($s = $o->getName())) {
$output .= '<dfn>'.$this->escape($s).'</dfn> ';
$output .= '<dfn>'.$this->escape($v->getDisplayName()).'</dfn> ';
if ($s = $o->getOperator()) {
$output .= $this->escape($s, 'ASCII').' ';
}
if ($c instanceof PropertyContext && null !== ($s = $c->getHooks())) {
$output .= '<var>'.$this->escape($s).'</var> ';
}
if (null !== ($s = $o->getType())) {
if (self::$escape_types) {
$s = $this->escape($s);
}
if ($o->reference) {
$s = '&amp;'.$s;
}
$output .= '<var>'.$s.'</var>';
if ($o instanceof InstanceValue && isset($o->spl_object_id)) {
$output .= '#'.((int) $o->spl_object_id);
}
$output .= ' ';
if (null !== ($s = $c->getOperator())) {
$output .= $this->escape($s, 'ASCII').' ';
}
if (null !== ($s = $o->getSize())) {
$s = $v->getDisplayType();
if (self::$escape_types) {
$s = $this->escape($s);
}
if ($c->isRef()) {
$s = '&amp;'.$s;
}
$output .= '<var>'.$s.'</var>';
if ($v instanceof InstanceValue && $this->shouldRenderObjectIds()) {
$output .= '#'.$v->getSplObjectId();
}
$output .= ' ';
if (null !== ($s = $v->getDisplaySize())) {
if (self::$escape_types) {
$s = $this->escape($s);
}
$output .= '('.$s.') ';
}
if (null !== ($s = $o->getValueShort())) {
if (null !== ($s = $v->getDisplayValue())) {
$s = \preg_replace('/\\s+/', ' ', $s);
if (self::$strlen_max) {
@ -347,13 +302,13 @@ class RichRenderer extends AbstractRenderer
return \trim($output);
}
public function renderChildren(Value $o): string
public function renderChildren(AbstractValue $v): string
{
$contents = [];
$tabs = [];
foreach ($o->getRepresentations() as $rep) {
$result = $this->renderTab($o, $rep);
foreach ($v->getRepresentations() as $rep) {
$result = $this->renderTab($v, $rep);
if (\strlen($result)) {
$contents[] = $result;
$tabs[] = $rep;
@ -435,22 +390,14 @@ class RichRenderer extends AbstractRenderer
}
// Don't pre-render on every dump
if (!$this->getForcePreRender()) {
if (!$this->force_pre_render) {
self::$needs_pre_render = false;
}
}
if ($this->shouldFolderRender()) {
$output .= $this->renderFolder();
if (!$this->getForcePreRender()) {
self::$needs_folder_render = false;
}
}
$output .= '<div class="kint-rich';
if ($this->getUseFolder()) {
if ($this->use_folder) {
$output .= ' kint-file';
}
@ -466,17 +413,20 @@ class RichRenderer extends AbstractRenderer
}
$output = '<footer>';
$output .= '<span class="kint-popup-trigger" title="Open in new window">&boxbox;</span> ';
if (!empty($this->call_info['trace']) && \count($this->call_info['trace']) > 1) {
if (!$this->use_folder) {
$output .= '<span class="kint-folder-trigger" title="Move to folder">&mapstodown;</span>';
}
if (!empty($this->trace) && \count($this->trace) > 1) {
$output .= '<nav></nav>';
}
$output .= $this->calledFrom();
if (!empty($this->call_info['trace']) && \count($this->call_info['trace']) > 1) {
if (!empty($this->trace) && \count($this->trace) > 1) {
$output .= '<ol>';
foreach ($this->call_info['trace'] as $index => $step) {
foreach ($this->trace as $index => $step) {
if (!$index) {
continue;
}
@ -505,7 +455,7 @@ class RichRenderer extends AbstractRenderer
public function escape(string $string, $encoding = false): string
{
if (false === $encoding) {
$encoding = BlobValue::detectEncoding($string);
$encoding = Utils::detectEncoding($string);
}
$original_encoding = $encoding;
@ -526,48 +476,42 @@ class RichRenderer extends AbstractRenderer
public function ideLink(string $file, int $line): string
{
$path = $this->escape(Kint::shortenPath($file)).':'.$line;
$ideLink = Kint::getIdeLink($file, $line);
$path = $this->escape(Utils::shortenPath($file)).':'.$line;
$ideLink = self::getFileLink($file, $line);
if (!$ideLink) {
if (null === $ideLink) {
return $path;
}
$class = '';
if (\preg_match('/https?:\\/\\//i', $ideLink)) {
$class = 'class="kint-ide-link" ';
}
return '<a '.$class.'href="'.$this->escape($ideLink).'">'.$path.'</a>';
return '<a href="'.$this->escape($ideLink).'">'.$path.'</a>';
}
protected function calledFrom(): string
{
$output = '';
if (isset($this->call_info['callee']['file'])) {
if (isset($this->callee['file'])) {
$output .= ' '.$this->ideLink(
$this->call_info['callee']['file'],
$this->call_info['callee']['line']
$this->callee['file'],
$this->callee['line']
);
}
if (
isset($this->call_info['callee']['function']) &&
isset($this->callee['function']) &&
(
!empty($this->call_info['callee']['class']) ||
!empty($this->callee['class']) ||
!\in_array(
$this->call_info['callee']['function'],
$this->callee['function'],
['include', 'include_once', 'require', 'require_once'],
true
)
)
) {
$output .= ' [';
$output .= $this->call_info['callee']['class'] ?? '';
$output .= $this->call_info['callee']['type'] ?? '';
$output .= $this->call_info['callee']['function'].'()]';
$output .= $this->callee['class'] ?? '';
$output .= $this->callee['type'] ?? '';
$output .= $this->callee['function'].'()]';
}
if ('' !== $output) {
@ -581,97 +525,93 @@ class RichRenderer extends AbstractRenderer
return $output;
}
protected function renderTab(Value $o, Representation $rep): string
protected function renderTab(AbstractValue $v, RepresentationInterface $rep): string
{
if (($plugin = $this->getPlugin(self::$tab_plugins, $rep->hints)) && $plugin instanceof TabPluginInterface) {
$output = $plugin->renderTab($rep);
if (null !== $output && \strlen($output)) {
if ($plugin = $this->getTabPlugin($rep)) {
$output = $plugin->renderTab($rep, $v);
if (null !== $output) {
return $output;
}
}
if (\is_array($rep->contents)) {
if ($rep instanceof ValueRepresentation) {
return $this->render($rep->getValue());
}
if ($rep instanceof ContainerRepresentation) {
$output = '';
if ($o instanceof InstanceValue && 'properties' === $rep->getName()) {
foreach (self::sortProperties($rep->contents, self::$sort) as $obj) {
$output .= $this->render($obj);
}
} else {
foreach ($rep->contents as $obj) {
$output .= $this->render($obj);
}
foreach ($rep->getContents() as $obj) {
$output .= $this->render($obj);
}
return $output;
}
if (\is_string($rep->contents)) {
$show_contents = false;
// If it is the value representation of a string and its whitespace
// was truncated in the header, always display the full string
if ('string' !== $o->type || $o->value !== $rep) {
$show_contents = true;
} else {
if (\preg_match('/(:?[\\r\\n\\t\\f\\v]| {2})/', $rep->contents)) {
if ($rep instanceof StringRepresentation) {
// If we're dealing with the content representation
if ($v instanceof StringValue && $rep->getValue() === $v->getValue()) {
// Only show the contents if:
if (\preg_match('/(:?[\\r\\n\\t\\f\\v]| {2})/', $rep->getValue())) {
// We have unrepresentable whitespace (Without whitespace preservation)
$show_contents = true;
} elseif (self::$strlen_max && null !== ($vs = $o->getValueShort()) && BlobValue::strlen($vs) > self::$strlen_max) {
} elseif (self::$strlen_max && Utils::strlen($v->getDisplayValue()) > self::$strlen_max) {
// We had to truncate getDisplayValue
$show_contents = true;
}
if (empty($o->encoding)) {
} else {
$show_contents = false;
}
} else {
$show_contents = true;
}
if ($show_contents) {
return '<pre>'.$this->escape($rep->contents)."\n</pre>";
return '<pre>'.$this->escape($rep->getValue())."\n</pre>";
}
}
if ($rep->contents instanceof Value) {
return $this->render($rep->contents);
}
return '';
}
/**
* @psalm-param PluginMap $plugins
* @psalm-param string[] $hints
*/
protected function getPlugin(array $plugins, array $hints): ?PluginInterface
protected function getValuePlugin(AbstractValue $v): ?ValuePluginInterface
{
if ($plugins = $this->matchPlugins($plugins, $hints)) {
$plugin = \end($plugins);
$hint = $v->getHint();
if (!isset($this->plugin_objs[$plugin]) && \is_subclass_of($plugin, PluginInterface::class)) {
$this->plugin_objs[$plugin] = new $plugin($this);
}
return $this->plugin_objs[$plugin];
if (null === $hint || !isset(self::$value_plugins[$hint])) {
return null;
}
return null;
}
$plugin = self::$value_plugins[$hint];
protected static function renderJs(): string
{
return \file_get_contents(KINT_DIR.'/resources/compiled/shared.js').\file_get_contents(KINT_DIR.'/resources/compiled/rich.js');
}
protected static function renderCss(): string
{
if (\file_exists(KINT_DIR.'/resources/compiled/'.self::$theme)) {
return \file_get_contents(KINT_DIR.'/resources/compiled/'.self::$theme);
if (!\is_a($plugin, ValuePluginInterface::class, true)) {
return null;
}
return \file_get_contents(self::$theme);
if (!isset($this->plugin_objs[$plugin])) {
$this->plugin_objs[$plugin] = new $plugin($this);
}
return $this->plugin_objs[$plugin];
}
protected static function renderFolder(): string
protected function getTabPlugin(RepresentationInterface $r): ?TabPluginInterface
{
return '<div class="kint-rich kint-folder"><dl><dt class="kint-parent"><nav></nav>Kint</dt><dd class="kint-foldout"></dd></dl></div>';
$hint = $r->getHint();
if (null === $hint || !isset(self::$tab_plugins[$hint])) {
return null;
}
$plugin = self::$tab_plugins[$hint];
if (!\is_a($plugin, TabPluginInterface::class, true)) {
return null;
}
if (!isset($this->plugin_objs[$plugin])) {
$this->plugin_objs[$plugin] = new $plugin($this);
}
return $this->plugin_objs[$plugin];
}
}

View File

@ -28,29 +28,26 @@ declare(strict_types=1);
namespace Kint\Renderer\Text;
use Kint\Renderer\TextRenderer;
use Kint\Zval\Value;
use Kint\Value\AbstractValue;
/**
* @psalm-consistent-constructor
*/
abstract class AbstractPlugin implements PluginInterface
{
protected $renderer;
protected TextRenderer $renderer;
public function __construct(TextRenderer $r)
{
$this->renderer = $r;
}
public function renderLockedHeader(Value $o, string $content = null): string
public function renderLockedHeader(AbstractValue $v, ?string $content = null): string
{
$out = '';
if (0 == $o->depth) {
$out .= $this->renderer->colorTitle($this->renderer->renderTitle($o)).PHP_EOL;
if (0 === $v->getContext()->getDepth()) {
$out .= $this->renderer->colorTitle($this->renderer->renderTitle($v)).PHP_EOL;
}
$out .= $this->renderer->renderHeader($o);
$out .= $this->renderer->renderHeader($v);
if (null !== $content) {
$out .= ' '.$this->renderer->colorValue($content);

View File

@ -0,0 +1,49 @@
<?php
declare(strict_types=1);
/*
* The MIT License (MIT)
*
* Copyright (c) 2013 Jonathan Vollebregt (jnvsor@gmail.com), Rokas Šleinius (raveren@gmail.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
namespace Kint\Renderer\Text;
use Kint\Value\AbstractValue;
class LockPlugin extends AbstractPlugin
{
public function render(AbstractValue $v): ?string
{
switch ($v->getHint()) {
case 'blacklist':
return $this->renderLockedHeader($v, 'BLACKLISTED');
case 'recursion':
return $this->renderLockedHeader($v, 'RECURSION');
case 'depth_limit':
return $this->renderLockedHeader($v, 'DEPTH LIMIT');
case 'array_limit':
return $this->renderLockedHeader($v, 'ARRAY LIMIT');
}
return null;
}
}

View File

@ -28,15 +28,14 @@ declare(strict_types=1);
namespace Kint\Renderer\Text;
use Kint\Renderer\PlainRenderer;
use Kint\Renderer\Rich\MicrotimePlugin as RichPlugin;
use Kint\Renderer\TextRenderer;
use Kint\Utils;
use Kint\Zval\Representation\MicrotimeRepresentation;
use Kint\Zval\Value;
use Kint\Value\AbstractValue;
use Kint\Value\Representation\MicrotimeRepresentation;
class MicrotimePlugin extends AbstractPlugin
{
protected $useJs = false;
protected bool $useJs = false;
public function __construct(TextRenderer $r)
{
@ -47,36 +46,38 @@ class MicrotimePlugin extends AbstractPlugin
}
}
public function render(Value $o): ?string
public function render(AbstractValue $v): ?string
{
$r = $o->getRepresentation('microtime');
$r = $v->getRepresentation('microtime');
if (!$r instanceof MicrotimeRepresentation || !($dt = $r->getDateTime())) {
return null;
}
$c = $v->getContext();
$out = '';
if (0 == $o->depth) {
$out .= $this->renderer->colorTitle($this->renderer->renderTitle($o)).PHP_EOL;
if (0 === $c->getDepth()) {
$out .= $this->renderer->colorTitle($this->renderer->renderTitle($v)).PHP_EOL;
}
$out .= $this->renderer->renderHeader($o);
$out .= $this->renderer->renderChildren($o).PHP_EOL;
$out .= $this->renderer->renderHeader($v);
$out .= $this->renderer->renderChildren($v).PHP_EOL;
$indent = \str_repeat(' ', ($o->depth + 1) * $this->renderer->indent_width);
$indent = \str_repeat(' ', ($c->getDepth() + 1) * $this->renderer->indent_width);
if ($this->useJs) {
$out .= '<span data-kint-microtime-group="'.$r->group.'">';
$out .= '<span data-kint-microtime-group="'.$r->getGroup().'">';
}
$out .= $indent.$this->renderer->colorType('TIME:').' ';
$out .= $this->renderer->colorValue($dt->format('Y-m-d H:i:s.u')).PHP_EOL;
if (null !== $r->lap) {
if (null !== ($lap = $r->getLapTime())) {
$out .= $indent.$this->renderer->colorType('SINCE LAST CALL:').' ';
$lap = \round($r->lap, 4);
$lap = \round($lap, 4);
if ($this->useJs) {
$lap = '<span class="kint-microtime-lap">'.$lap.'</span>';
@ -84,14 +85,14 @@ class MicrotimePlugin extends AbstractPlugin
$out .= $this->renderer->colorValue($lap.'s').'.'.PHP_EOL;
}
if (null !== $r->total) {
if (null !== ($total = $r->getTotalTime())) {
$out .= $indent.$this->renderer->colorType('SINCE START:').' ';
$out .= $this->renderer->colorValue(\round($r->total, 4).'s').'.'.PHP_EOL;
$out .= $this->renderer->colorValue(\round($total, 4).'s').'.'.PHP_EOL;
}
if (null !== $r->avg) {
if (null !== ($avg = $r->getAverageTime())) {
$out .= $indent.$this->renderer->colorType('AVERAGE DURATION:').' ';
$avg = \round($r->avg, 4);
$avg = \round($avg, 4);
if ($this->useJs) {
$avg = '<span class="kint-microtime-avg">'.$avg.'</span>';
@ -100,17 +101,17 @@ class MicrotimePlugin extends AbstractPlugin
$out .= $this->renderer->colorValue($avg.'s').'.'.PHP_EOL;
}
$bytes = Utils::getHumanReadableBytes($r->mem);
$mem = $r->mem.' bytes ('.\round($bytes['value'], 3).' '.$bytes['unit'].')';
$bytes = Utils::getHumanReadableBytes($r->mem_real);
$bytes = Utils::getHumanReadableBytes($r->getMemoryUsage());
$mem = $r->getMemoryUsage().' bytes ('.\round($bytes['value'], 3).' '.$bytes['unit'].')';
$bytes = Utils::getHumanReadableBytes($r->getMemoryUsageReal());
$mem .= ' (real '.\round($bytes['value'], 3).' '.$bytes['unit'].')';
$out .= $indent.$this->renderer->colorType('MEMORY USAGE:').' ';
$out .= $this->renderer->colorValue($mem).'.'.PHP_EOL;
$bytes = Utils::getHumanReadableBytes($r->mem_peak);
$mem = $r->mem_peak.' bytes ('.\round($bytes['value'], 3).' '.$bytes['unit'].')';
$bytes = Utils::getHumanReadableBytes($r->mem_peak_real);
$bytes = Utils::getHumanReadableBytes($r->getMemoryPeakUsage());
$mem = $r->getMemoryPeakUsage().' bytes ('.\round($bytes['value'], 3).' '.$bytes['unit'].')';
$bytes = Utils::getHumanReadableBytes($r->getMemoryPeakUsageReal());
$mem .= ' (real '.\round($bytes['value'], 3).' '.$bytes['unit'].')';
$out .= $indent.$this->renderer->colorType('PEAK MEMORY USAGE:').' ';
@ -122,9 +123,4 @@ class MicrotimePlugin extends AbstractPlugin
return $out;
}
public static function renderJs(): string
{
return RichPlugin::renderJs();
}
}

View File

@ -28,11 +28,11 @@ declare(strict_types=1);
namespace Kint\Renderer\Text;
use Kint\Renderer\TextRenderer;
use Kint\Zval\Value;
use Kint\Value\AbstractValue;
interface PluginInterface
{
public function __construct(TextRenderer $r);
public function render(Value $o): ?string;
public function render(AbstractValue $v): ?string;
}

View File

@ -0,0 +1,59 @@
<?php
declare(strict_types=1);
/*
* The MIT License (MIT)
*
* Copyright (c) 2013 Jonathan Vollebregt (jnvsor@gmail.com), Rokas Šleinius (raveren@gmail.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
namespace Kint\Renderer\Text;
use Kint\Value\AbstractValue;
use Kint\Value\Representation\SplFileInfoRepresentation;
class SplFileInfoPlugin extends AbstractPlugin
{
public function render(AbstractValue $v): ?string
{
$r = $v->getRepresentation('splfileinfo');
if (!$r instanceof SplFileInfoRepresentation) {
return null;
}
$out = '';
$c = $v->getContext();
if (0 === $c->getDepth()) {
$out .= $this->renderer->colorTitle($this->renderer->renderTitle($v)).PHP_EOL;
}
$out .= $this->renderer->renderHeader($v);
if (null !== $v->getDisplayValue()) {
$out .= ' =>';
}
$out .= ' '.$this->renderer->colorValue($this->renderer->escape($r->getValue())).PHP_EOL;
return $out;
}
}

View File

@ -27,59 +27,63 @@ declare(strict_types=1);
namespace Kint\Renderer\Text;
use Kint\Zval\MethodValue;
use Kint\Zval\Value;
use Kint\Value\AbstractValue;
use Kint\Value\MethodValue;
use Kint\Value\Representation\SourceRepresentation;
use Kint\Value\TraceFrameValue;
use Kint\Value\TraceValue;
class TracePlugin extends AbstractPlugin
{
public function render(Value $o): string
public function render(AbstractValue $v): ?string
{
$out = '';
if (0 == $o->depth) {
$out .= $this->renderer->colorTitle($this->renderer->renderTitle($o)).PHP_EOL;
if (!$v instanceof TraceValue) {
return null;
}
$out .= $this->renderer->renderHeader($o).':'.PHP_EOL;
$c = $v->getContext();
$indent = \str_repeat(' ', ($o->depth + 1) * $this->renderer->indent_width);
$out = '';
if (0 === $c->getDepth()) {
$out .= $this->renderer->colorTitle($this->renderer->renderTitle($v)).PHP_EOL;
}
$out .= $this->renderer->renderHeader($v).':'.PHP_EOL;
$indent = \str_repeat(' ', ($c->getDepth() + 1) * $this->renderer->indent_width);
$i = 1;
foreach ($o->value->contents as $frame) {
foreach ($v->getContents() as $frame) {
if (!$frame instanceof TraceFrameValue) {
continue;
}
$framedesc = $indent.\str_pad($i.': ', 4, ' ');
if ($frame->trace['file']) {
$framedesc .= $this->renderer->ideLink($frame->trace['file'], $frame->trace['line']).PHP_EOL;
if (null !== ($file = $frame->getFile()) && null !== ($line = $frame->getLine())) {
$framedesc .= $this->renderer->ideLink($file, $line).PHP_EOL;
} else {
$framedesc .= 'PHP internal call'.PHP_EOL;
}
$framedesc .= $indent.' ';
if ($callable = $frame->getCallable()) {
$framedesc .= $indent.' ';
if ($frame->trace['class']) {
$framedesc .= $this->renderer->escape($frame->trace['class']);
if ($frame->trace['object']) {
$framedesc .= $this->renderer->escape('->');
} else {
$framedesc .= '::';
if ($callable instanceof MethodValue) {
$framedesc .= $this->renderer->escape($callable->getContext()->owner_class.$callable->getContext()->getOperator());
}
}
if (\is_string($frame->trace['function'])) {
$framedesc .= $this->renderer->escape($frame->trace['function']).'(...)';
} elseif ($frame->trace['function'] instanceof MethodValue) {
if (null !== ($s = $frame->trace['function']->getName())) {
$framedesc .= $this->renderer->escape($s);
$framedesc .= '('.$this->renderer->escape($frame->trace['function']->getParams()).')';
}
$framedesc .= $this->renderer->escape($callable->getDisplayName());
}
$out .= $this->renderer->colorType($framedesc).PHP_EOL.PHP_EOL;
if ($source = $frame->getRepresentation('source')) {
$line_wanted = $source->line;
$source = $source->source;
$source = $frame->getRepresentation('source');
if ($source instanceof SourceRepresentation) {
$line_wanted = $source->getLine();
$source = $source->getSourceLines();
// Trim empty lines from the start and end of the source
foreach ($source as $linenum => $line) {
@ -99,7 +103,7 @@ class TracePlugin extends AbstractPlugin
}
foreach ($source as $lineno => $line) {
if ($lineno == $line_wanted) {
if ($lineno === $line_wanted) {
$out .= $indent.$this->renderer->colorValue($this->renderer->escape($line)).PHP_EOL;
} else {
$out .= $indent.$this->renderer->escape($line).PHP_EOL;

View File

@ -27,31 +27,35 @@ declare(strict_types=1);
namespace Kint\Renderer;
use Kint\Kint;
use Kint\Parser;
use Kint\Parser\PluginInterface as ParserPluginInterface;
use Kint\Renderer\Text\PluginInterface;
use Kint\Utils;
use Kint\Zval\InstanceValue;
use Kint\Zval\Value;
use Kint\Value\AbstractValue;
use Kint\Value\ArrayValue;
use Kint\Value\Context\ArrayContext;
use Kint\Value\Context\ClassDeclaredContext;
use Kint\Value\Context\PropertyContext;
use Kint\Value\InstanceValue;
use Kint\Value\StringValue;
/**
* @psalm-import-type Encoding from \Kint\Zval\BlobValue
* @psalm-import-type PluginMap from AbstractRenderer
* @psalm-import-type Encoding from StringValue
*/
class TextRenderer extends AbstractRenderer
{
/**
* TextRenderer plugins should implement PluginInterface.
*
* @psalm-var PluginMap
* @psalm-var class-string<PluginInterface>[]
*/
public static $plugins = [
'array_limit' => Text\ArrayLimitPlugin::class,
'blacklist' => Text\BlacklistPlugin::class,
'depth_limit' => Text\DepthLimitPlugin::class,
'enum' => Text\EnumPlugin::class,
public static array $plugins = [
'array_limit' => Text\LockPlugin::class,
'blacklist' => Text\LockPlugin::class,
'depth_limit' => Text\LockPlugin::class,
'splfileinfo' => Text\SplFileInfoPlugin::class,
'microtime' => Text\MicrotimePlugin::class,
'recursion' => Text\RecursionPlugin::class,
'recursion' => Text\LockPlugin::class,
'trace' => Text\TracePlugin::class,
];
@ -59,14 +63,21 @@ class TextRenderer extends AbstractRenderer
* Parser plugins must be instanceof one of these or
* it will be removed for performance reasons.
*
* @psalm-var class-string[]
* @psalm-var class-string<ParserPluginInterface>[]
*/
public static $parser_plugin_whitelist = [
public static array $parser_plugin_whitelist = [
Parser\ArrayLimitPlugin::class,
Parser\ArrayObjectPlugin::class,
Parser\BlacklistPlugin::class,
Parser\ClosurePlugin::class,
Parser\DateTimePlugin::class,
Parser\DomPlugin::class,
Parser\EnumPlugin::class,
Parser\IteratorPlugin::class,
Parser\MicrotimePlugin::class,
Parser\MysqliPlugin::class,
Parser\SimpleXMLElementPlugin::class,
Parser\SplFileInfoPlugin::class,
Parser\StreamPlugin::class,
Parser\TracePlugin::class,
];
@ -75,89 +86,82 @@ class TextRenderer extends AbstractRenderer
* The maximum length of a string before it is truncated.
*
* Falsey to disable
*
* @var int
*/
public static $strlen_max = 0;
/**
* The default width of the terminal for headers.
*
* @var int
*/
public static $default_width = 80;
/**
* Indentation width.
*
* @var int
*/
public static $default_indent = 4;
/**
* Decorate the header and footer.
*
* @var bool
*/
public static $decorations = true;
/**
* Sort mode for object properties.
*
* @var int
*/
public static $sort = self::SORT_NONE;
public static int $strlen_max = 0;
/**
* Timestamp to print in footer in date() format.
*
* @var ?string
*/
public static $timestamp = null;
public static ?string $timestamp = null;
public $header_width = 80;
public $indent_width = 4;
/**
* The default width of the terminal for headers.
*/
public static int $default_width = 80;
protected $plugin_objs = [];
/**
* Indentation width.
*/
public static int $default_indent = 4;
/**
* Decorate the header and footer.
*/
public static bool $decorations = true;
public int $header_width = 80;
public int $indent_width = 4;
protected array $plugin_objs = [];
public function __construct()
{
parent::__construct();
$this->header_width = self::$default_width;
$this->indent_width = self::$default_indent;
}
public function render(Value $o): string
public function render(AbstractValue $v): string
{
if ($plugin = $this->getPlugin(self::$plugins, $o->hints)) {
$output = $plugin->render($o);
$render_spl_ids_stash = $this->render_spl_ids;
if ($this->render_spl_ids && ($v->flags & AbstractValue::FLAG_GENERATED)) {
$this->render_spl_ids = false;
}
if ($plugin = $this->getPlugin($v)) {
$output = $plugin->render($v);
if (null !== $output && \strlen($output)) {
if (!$this->render_spl_ids && $render_spl_ids_stash) {
$this->render_spl_ids = true;
}
return $output;
}
}
$out = '';
if (0 == $o->depth) {
$out .= $this->colorTitle($this->renderTitle($o)).PHP_EOL;
$c = $v->getContext();
if (0 === $c->getDepth()) {
$out .= $this->colorTitle($this->renderTitle($v)).PHP_EOL;
}
$out .= $this->renderHeader($o);
$out .= $this->renderChildren($o).PHP_EOL;
$out .= $header = $this->renderHeader($v);
$out .= $this->renderChildren($v);
if (\strlen($header)) {
$out .= PHP_EOL;
}
if (!$this->render_spl_ids && $render_spl_ids_stash) {
$this->render_spl_ids = true;
}
return $out;
}
public function renderNothing(): string
{
if (self::$decorations) {
return $this->colorTitle(
$this->boxText('No argument', $this->header_width)
).PHP_EOL;
}
return $this->colorTitle('No argument').PHP_EOL;
}
public function boxText(string $text, int $width): string
{
$out = '┌'.\str_repeat('─', $width - 2).'┐'.PHP_EOL;
@ -174,96 +178,99 @@ class TextRenderer extends AbstractRenderer
return $out;
}
public function renderTitle(Value $o): string
public function renderTitle(AbstractValue $v): string
{
$name = (string) $o->getName();
if (self::$decorations) {
return $this->boxText($name, $this->header_width);
return $this->boxText($v->getDisplayName(), $this->header_width);
}
return Utils::truncateString($name, $this->header_width);
return Utils::truncateString($v->getDisplayName(), $this->header_width);
}
public function renderHeader(Value $o): string
public function renderHeader(AbstractValue $v): string
{
$output = [];
if ($o->depth) {
if (null !== ($s = $o->getModifiers())) {
$output[] = $s;
$c = $v->getContext();
if ($c->getDepth() > 0) {
if ($c instanceof ClassDeclaredContext) {
$output[] = $this->colorType($c->getModifiers());
}
if (null !== $o->name) {
$output[] = $this->escape(\var_export($o->name, true));
if ($c instanceof ArrayContext) {
$output[] = $this->escape(\var_export($c->getName(), true));
} else {
$output[] = $this->escape((string) $c->getName());
}
if (null !== ($s = $o->getOperator())) {
$output[] = $this->escape($s);
}
if ($c instanceof PropertyContext && null !== ($s = $c->getHooks())) {
$output[] = $this->colorType($this->escape($s));
}
if (null !== ($s = $c->getOperator())) {
$output[] = $this->escape($s);
}
}
if (null !== ($s = $o->getType())) {
if ($o->reference) {
$s = '&'.$s;
}
$s = $this->colorType($this->escape($s));
if ($o instanceof InstanceValue && isset($o->spl_object_id)) {
$s .= '#'.((int) $o->spl_object_id);
}
$output[] = $s;
$s = $v->getDisplayType();
if ($c->isRef()) {
$s = '&'.$s;
}
if (null !== ($s = $o->getSize())) {
$s = $this->colorType($this->escape($s));
if ($v instanceof InstanceValue && $this->shouldRenderObjectIds()) {
$s .= '#'.$v->getSplObjectId();
}
$output[] = $s;
if (null !== ($s = $v->getDisplaySize())) {
$output[] = '('.$this->escape($s).')';
}
if (null !== ($s = $o->getValueShort())) {
if (null !== ($s = $v->getDisplayValue())) {
if (self::$strlen_max) {
$s = Utils::truncateString($s, self::$strlen_max);
}
$output[] = $this->colorValue($this->escape($s));
}
return \str_repeat(' ', $o->depth * $this->indent_width).\implode(' ', $output);
return \str_repeat(' ', $c->getDepth() * $this->indent_width).\implode(' ', $output);
}
public function renderChildren(Value $o): string
public function renderChildren(AbstractValue $v): string
{
if ('array' === $o->type) {
$output = ' [';
} elseif ('object' === $o->type) {
$output = ' (';
} else {
$children = $v->getDisplayChildren();
if (!$children) {
if ($v instanceof ArrayValue) {
return ' []';
}
return '';
}
$children = '';
if ($o->value && \is_array($o->value->contents)) {
if ($o instanceof InstanceValue && 'properties' === $o->value->getName()) {
foreach (self::sortProperties($o->value->contents, self::$sort) as $obj) {
$children .= $this->render($obj);
}
} else {
foreach ($o->value->contents as $child) {
$children .= $this->render($child);
}
}
}
if ($children) {
$output .= PHP_EOL.$children;
$output .= \str_repeat(' ', $o->depth * $this->indent_width);
}
if ('array' === $o->type) {
$output .= ']';
if ($v instanceof ArrayValue) {
$output = ' [';
} elseif ($v instanceof InstanceValue) {
$output = ' (';
} else {
$output .= ')';
$output = '';
}
$output .= PHP_EOL;
foreach ($children as $child) {
$output .= $this->render($child);
}
$indent = \str_repeat(' ', $v->getContext()->getDepth() * $this->indent_width);
if ($v instanceof ArrayValue) {
$output .= $indent.']';
} elseif ($v instanceof InstanceValue) {
$output .= $indent.')';
}
return $output;
@ -321,7 +328,7 @@ class TextRenderer extends AbstractRenderer
public function ideLink(string $file, int $line): string
{
return $this->escape(Kint::shortenPath($file)).':'.$line;
return $this->escape(Utils::shortenPath($file)).':'.$line;
}
/**
@ -336,28 +343,28 @@ class TextRenderer extends AbstractRenderer
{
$output = '';
if (isset($this->call_info['callee']['file'])) {
if (isset($this->callee['file'])) {
$output .= 'Called from '.$this->ideLink(
$this->call_info['callee']['file'],
$this->call_info['callee']['line']
$this->callee['file'],
$this->callee['line']
);
}
if (
isset($this->call_info['callee']['function']) &&
isset($this->callee['function']) &&
(
!empty($this->call_info['callee']['class']) ||
!empty($this->callee['class']) ||
!\in_array(
$this->call_info['callee']['function'],
$this->callee['function'],
['include', 'include_once', 'require', 'require_once'],
true
)
)
) {
$output .= ' [';
$output .= $this->call_info['callee']['class'] ?? '';
$output .= $this->call_info['callee']['type'] ?? '';
$output .= $this->call_info['callee']['function'].'()]';
$output .= $this->callee['class'] ?? '';
$output .= $this->callee['type'] ?? '';
$output .= $this->callee['function'].'()]';
}
if (null !== self::$timestamp) {
@ -370,22 +377,24 @@ class TextRenderer extends AbstractRenderer
return $output;
}
/**
* @psalm-param PluginMap $plugins
* @psalm-param string[] $hints
*/
protected function getPlugin(array $plugins, array $hints): ?PluginInterface
protected function getPlugin(AbstractValue $v): ?PluginInterface
{
if ($plugins = $this->matchPlugins($plugins, $hints)) {
$plugin = \end($plugins);
$hint = $v->getHint();
if (!isset($this->plugin_objs[$plugin]) && \is_subclass_of($plugin, PluginInterface::class)) {
$this->plugin_objs[$plugin] = new $plugin($this);
}
return $this->plugin_objs[$plugin];
if (null === $hint || !isset(self::$plugins[$hint])) {
return null;
}
return null;
$plugin = self::$plugins[$hint];
if (!\is_a($plugin, PluginInterface::class, true)) {
return null;
}
if (!isset($this->plugin_objs[$plugin])) {
$this->plugin_objs[$plugin] = new $plugin($this);
}
return $this->plugin_objs[$plugin];
}
}

View File

@ -27,7 +27,8 @@ declare(strict_types=1);
namespace Kint;
use Kint\Zval\BlobValue;
use Kint\Value\StringValue;
use Kint\Value\TraceFrameValue;
use ReflectionNamedType;
use ReflectionType;
use UnexpectedValueException;
@ -35,12 +36,98 @@ use UnexpectedValueException;
/**
* A collection of utility methods. Should all be static methods with no dependencies.
*
* @psalm-import-type Encoding from BlobValue
* @psalm-import-type Encoding from StringValue
* @psalm-import-type TraceFrame from TraceFrameValue
*/
final class Utils
{
public const BT_STRUCTURE = [
'function' => 'string',
'line' => 'integer',
'file' => 'string',
'class' => 'string',
'object' => 'object',
'type' => 'string',
'args' => 'array',
];
public const BYTE_UNITS = ['B', 'KB', 'MB', 'GB', 'TB'];
/**
* @var array Character encodings to detect
*
* @see https://secure.php.net/function.mb-detect-order
*
* In practice, mb_detect_encoding can only successfully determine the
* difference between the following common charsets at once without
* breaking things for one of the other charsets:
* - ASCII
* - UTF-8
* - SJIS
* - EUC-JP
*
* The order of the charsets is significant. If you put UTF-8 before ASCII
* it will never match ASCII, because UTF-8 is a superset of ASCII.
* Similarly, SJIS and EUC-JP frequently match UTF-8 strings, so you should
* check UTF-8 first. SJIS and EUC-JP seem to work either way, but SJIS is
* more common so it should probably be first.
*
* While you're free to experiment with other charsets, remember to keep
* this behavior in mind when setting up your char_encodings array.
*
* This depends on the mbstring extension
*/
public static array $char_encodings = [
'ASCII',
'UTF-8',
];
/**
* @var array Legacy character encodings to detect
*
* @see https://secure.php.net/function.iconv
*
* Assuming the other encoding checks fail, this will perform a
* simple iconv conversion to check for invalid bytes. If any are
* found it will not match.
*
* This can be useful for ambiguous single byte encodings like
* windows-125x and iso-8859-x which have practically undetectable
* differences because they use every single byte available.
*
* This is *NOT* reliable and should not be trusted implicitly. Since it
* works by triggering and suppressing conversion warnings, your error
* handler may complain.
*
* As with char_encodings, the order of the charsets is significant.
*
* This depends on the iconv extension
*/
public static array $legacy_encodings = [];
/**
* @var array Path aliases that will be displayed instead of the full path.
*
* Keys are paths, values are replacement strings
*
* Example for laravel:
*
* Utils::$path_aliases = [
* base_path() => '<BASE>',
* app_path() => '<APP>',
* base_path().'/vendor' => '<VENDOR>',
* ];
*
* Defaults to [$_SERVER['DOCUMENT_ROOT'] => '<ROOT>']
*
* @psalm-var array<non-empty-string, string>
*/
public static array $path_aliases = [];
/**
* @codeCoverageIgnore
*
* @psalm-suppress UnusedConstructor
*/
private function __construct()
{
@ -52,11 +139,13 @@ final class Utils
* @param int $value Amount of bytes
*
* @return array Human readable value and unit
*
* @psalm-return array{value: float, unit: 'B'|'KB'|'MB'|'GB'|'TB'}
*
* @psalm-pure
*/
public static function getHumanReadableBytes(int $value): array
{
static $unit = ['B', 'KB', 'MB', 'GB', 'TB'];
$negative = $value < 0;
$value = \abs($value);
@ -83,20 +172,331 @@ final class Utils
return [
'value' => \round($value, 1),
'unit' => $unit[$i],
'unit' => self::BYTE_UNITS[$i],
];
}
/** @psalm-pure */
public static function isSequential(array $array): bool
{
return \array_keys($array) === \range(0, \count($array) - 1);
}
/** @psalm-pure */
public static function isAssoc(array $array): bool
{
return (bool) \count(\array_filter(\array_keys($array), 'is_string'));
}
/**
* @psalm-assert-if-true list<TraceFrame> $trace
*/
public static function isTrace(array $trace): bool
{
if (!self::isSequential($trace)) {
return false;
}
$file_found = false;
foreach ($trace as $frame) {
if (!\is_array($frame) || !isset($frame['function'])) {
return false;
}
if (isset($frame['class']) && !\class_exists($frame['class'], false)) {
return false;
}
foreach ($frame as $key => $val) {
if (!isset(self::BT_STRUCTURE[$key])) {
return false;
}
if (\gettype($val) !== self::BT_STRUCTURE[$key]) {
return false;
}
if ('file' === $key) {
$file_found = true;
}
}
}
return $file_found;
}
/**
* @psalm-param TraceFrame $frame
*
* @psalm-pure
*/
public static function traceFrameIsListed(array $frame, array $matches): bool
{
if (isset($frame['class'])) {
$called = [\strtolower($frame['class']), \strtolower($frame['function'])];
} else {
$called = \strtolower($frame['function']);
}
return \in_array($called, $matches, true);
}
/** @psalm-pure */
public static function normalizeAliases(array $aliases): array
{
foreach ($aliases as $index => $alias) {
if (\is_array($alias) && 2 === \count($alias)) {
$alias = \array_values(\array_filter($alias, 'is_string'));
if (2 === \count($alias) && self::isValidPhpName($alias[1]) && self::isValidPhpNamespace($alias[0])) {
$aliases[$index] = [
\strtolower(\ltrim($alias[0], '\\')),
\strtolower($alias[1]),
];
} else {
unset($aliases[$index]);
continue;
}
} elseif (\is_string($alias)) {
if (self::isValidPhpNamespace($alias)) {
$alias = \explode('\\', \strtolower($alias));
$aliases[$index] = \end($alias);
} else {
unset($aliases[$index]);
continue;
}
} else {
unset($aliases[$index]);
}
}
return \array_values($aliases);
}
/** @psalm-pure */
public static function isValidPhpName(string $name): bool
{
return (bool) \preg_match('/^[a-zA-Z_\\x80-\\xff][a-zA-Z0-9_\\x80-\\xff]*$/', $name);
}
/** @psalm-pure */
public static function isValidPhpNamespace(string $ns): bool
{
$parts = \explode('\\', $ns);
if ('' === \reset($parts)) {
\array_shift($parts);
}
if (!\count($parts)) {
return false;
}
foreach ($parts as $part) {
if (!self::isValidPhpName($part)) {
return false;
}
}
return true;
}
/**
* trigger_error before PHP 8.1 truncates the error message at nul
* so we have to sanitize variable strings before using them.
*
* @psalm-pure
*/
public static function errorSanitizeString(string $input): string
{
if (KINT_PHP82) {
return $input;
}
return \strtok($input, "\0"); // @codeCoverageIgnore
}
/** @psalm-pure */
public static function getTypeString(ReflectionType $type): string
{
// @codeCoverageIgnoreStart
// ReflectionType::__toString was deprecated in 7.4 and undeprecated in 8
// and toString doesn't correctly show the nullable ? in the type before 8
if (!KINT_PHP80) {
if (!$type instanceof ReflectionNamedType) {
throw new UnexpectedValueException('ReflectionType on PHP 7 must be ReflectionNamedType');
}
$name = $type->getName();
if ($type->allowsNull() && 'mixed' !== $name && false === \strpos($name, '|')) {
$name = '?'.$name;
}
return $name;
}
// @codeCoverageIgnoreEnd
return (string) $type;
}
/**
* @psalm-param Encoding $encoding
*/
public static function truncateString(string $input, int $length = PHP_INT_MAX, string $end = '...', $encoding = false): string
{
$endlength = self::strlen($end);
if ($endlength >= $length) {
$endlength = 0;
$end = '';
}
if (self::strlen($input, $encoding) > $length) {
return self::substr($input, 0, $length - $endlength, $encoding).$end;
}
return $input;
}
/**
* @psalm-return Encoding
*/
public static function detectEncoding(string $string)
{
if (\function_exists('mb_detect_encoding')) {
$ret = \mb_detect_encoding($string, self::$char_encodings, true);
if (false !== $ret) {
return $ret;
}
}
// Pretty much every character encoding uses first 32 bytes as control
// characters. If it's not a multi-byte format it's safe to say matching
// any control character besides tab, nl, and cr means it's binary.
if (\preg_match('/[\\x00-\\x08\\x0B\\x0C\\x0E-\\x1F]/', $string)) {
return false;
}
if (\function_exists('iconv')) {
foreach (self::$legacy_encodings as $encoding) {
// Iconv detection works by triggering
// "Detected an illegal character in input string" notices
// This notice does not become a TypeError with strict_types
// so we don't have to wrap this in a try catch
if (@\iconv($encoding, $encoding, $string) === $string) {
return $encoding;
}
}
} elseif (!\function_exists('mb_detect_encoding')) { // @codeCoverageIgnore
// If a user has neither mb_detect_encoding, nor iconv, nor the
// polyfills, there's not much we can do about it...
// Pretend it's ASCII and pray the browser renders it properly.
return 'ASCII'; // @codeCoverageIgnore
}
return false;
}
/**
* @psalm-param Encoding $encoding
*/
public static function strlen(string $string, $encoding = false): int
{
if (\function_exists('mb_strlen')) {
if (false === $encoding) {
$encoding = self::detectEncoding($string);
}
if (false !== $encoding && 'ASCII' !== $encoding) {
return \mb_strlen($string, $encoding);
}
}
return \strlen($string);
}
/**
* @psalm-param Encoding $encoding
*/
public static function substr(string $string, int $start, ?int $length = null, $encoding = false): string
{
if (\function_exists('mb_substr')) {
if (false === $encoding) {
$encoding = self::detectEncoding($string);
}
if (false !== $encoding && 'ASCII' !== $encoding) {
return \mb_substr($string, $start, $length, $encoding);
}
}
// Special case for substr/mb_substr discrepancy
if ('' === $string) {
return '';
}
return \substr($string, $start, $length ?? PHP_INT_MAX);
}
public static function shortenPath(string $file): string
{
$split = \explode('/', \str_replace('\\', '/', $file));
$longest_match = 0;
$match = '';
foreach (self::$path_aliases as $path => $alias) {
$path = \explode('/', \str_replace('\\', '/', $path));
if (\count($path) < 2) {
continue;
}
if (\array_slice($split, 0, \count($path)) === $path && \count($path) > $longest_match) {
$longest_match = \count($path);
$match = $alias;
}
}
if ($longest_match) {
$suffix = \implode('/', \array_slice($split, $longest_match));
if (\preg_match('%^/*$%', $suffix)) {
return $match;
}
return $match.'/'.$suffix;
}
// fallback to find common path with Kint dir
$kint = \explode('/', \str_replace('\\', '/', KINT_DIR));
$had_real_path_part = false;
foreach ($split as $i => $part) {
if (!isset($kint[$i]) || $kint[$i] !== $part) {
if (!$had_real_path_part) {
break;
}
$suffix = \implode('/', \array_slice($split, $i));
if (\preg_match('%^/*$%', $suffix)) {
break;
}
$prefix = $i > 1 ? '.../' : '/';
return $prefix.$suffix;
}
if ($i > 0 && \strlen($kint[$i])) {
$had_real_path_part = true;
}
}
return $file;
}
public static function composerGetExtras(string $key = 'kint'): array
{
if (0 === \strpos(KINT_DIR, 'phar://')) {
@ -121,7 +521,7 @@ final class Utils
// Composer 2.0 Compatibility: packages are now wrapped into a "packages" top level key instead of the whole file being the package array
// @see https://getcomposer.org/upgrade/UPGRADE-2.0.md
foreach ($packages['packages'] ?? $packages as $package) {
if (isset($package['extra'][$key]) && \is_array($package['extra'][$key])) {
if (\is_array($package['extra'][$key] ?? null)) {
$extras = \array_replace($extras, $package['extra'][$key]);
}
}
@ -131,7 +531,7 @@ final class Utils
if (\file_exists($folder.'/composer.json') && \is_readable($folder.'/composer.json')) {
$composer = \json_decode(\file_get_contents($folder.'/composer.json'), true);
if (isset($composer['extra'][$key]) && \is_array($composer['extra'][$key])) {
if (\is_array($composer['extra'][$key] ?? null)) {
$extras = \array_replace($extras, $composer['extra'][$key]);
}
}
@ -164,133 +564,4 @@ final class Utils
\define('KINT_SKIP_HELPERS', true);
}
}
public static function isTrace(array $trace): bool
{
if (!self::isSequential($trace)) {
return false;
}
static $bt_structure = [
'function' => 'string',
'line' => 'integer',
'file' => 'string',
'class' => 'string',
'object' => 'object',
'type' => 'string',
'args' => 'array',
];
$file_found = false;
foreach ($trace as $frame) {
if (!\is_array($frame) || !isset($frame['function'])) {
return false;
}
foreach ($frame as $key => $val) {
if (!isset($bt_structure[$key])) {
return false;
}
if (\gettype($val) !== $bt_structure[$key]) {
return false;
}
if ('file' === $key) {
$file_found = true;
}
}
}
return $file_found;
}
public static function traceFrameIsListed(array $frame, array $matches): bool
{
if (isset($frame['class'])) {
$called = [\strtolower($frame['class']), \strtolower($frame['function'])];
} else {
$called = \strtolower($frame['function']);
}
return \in_array($called, $matches, true);
}
public static function normalizeAliases(array &$aliases): void
{
static $name_regex = '[a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*';
foreach ($aliases as $index => &$alias) {
if (\is_array($alias) && 2 === \count($alias)) {
$alias = \array_values(\array_filter($alias, 'is_string'));
if (2 === \count($alias) &&
\preg_match('/^'.$name_regex.'$/', $alias[1]) &&
\preg_match('/^\\\\?('.$name_regex.'\\\\)*'.$name_regex.'$/', $alias[0])
) {
$alias = [
\strtolower(\ltrim($alias[0], '\\')),
\strtolower($alias[1]),
];
} else {
unset($aliases[$index]);
continue;
}
} elseif (\is_string($alias)) {
if (\preg_match('/^\\\\?('.$name_regex.'\\\\)*'.$name_regex.'$/', $alias)) {
$alias = \explode('\\', \strtolower($alias));
$alias = \end($alias);
} else {
unset($aliases[$index]);
continue;
}
} else {
unset($aliases[$index]);
}
}
$aliases = \array_values($aliases);
}
/**
* @psalm-param Encoding $encoding
*/
public static function truncateString(string $input, int $length = PHP_INT_MAX, string $end = '...', $encoding = false): string
{
$endlength = BlobValue::strlen($end);
if ($endlength >= $length) {
$endlength = 0;
$end = '';
}
if (BlobValue::strlen($input, $encoding) > $length) {
return BlobValue::substr($input, 0, $length - $endlength, $encoding).$end;
}
return $input;
}
public static function getTypeString(ReflectionType $type): string
{
// @codeCoverageIgnoreStart
// ReflectionType::__toString was deprecated in 7.4 and undeprecated in 8
// and toString doesn't correctly show the nullable ? in the type before 8
if (!KINT_PHP80) {
if (!$type instanceof ReflectionNamedType) {
throw new UnexpectedValueException('ReflectionType on PHP 7 must be ReflectionNamedType');
}
$name = $type->getName();
if ($type->allowsNull() && 'mixed' !== $name && false === \strpos($name, '|')) {
$name = '?'.$name;
}
return $name;
}
// @codeCoverageIgnoreEnd
return (string) $type;
}
}

View File

@ -0,0 +1,190 @@
<?php
declare(strict_types=1);
/*
* The MIT License (MIT)
*
* Copyright (c) 2013 Jonathan Vollebregt (jnvsor@gmail.com), Rokas Šleinius (raveren@gmail.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
namespace Kint\Value;
use Kint\Value\Context\ContextInterface;
use Kint\Value\Representation\RepresentationInterface;
use OutOfRangeException;
/**
* @psalm-import-type ValueName from ContextInterface
*
* @psalm-type ValueFlags int-mask-of<AbstractValue::FLAG_*>
*/
abstract class AbstractValue
{
public const FLAG_NONE = 0;
public const FLAG_GENERATED = 1 << 0;
public const FLAG_BLACKLIST = 1 << 1;
public const FLAG_RECURSION = 1 << 2;
public const FLAG_DEPTH_LIMIT = 1 << 3;
public const FLAG_ARRAY_LIMIT = 1 << 4;
/** @psalm-var ValueFlags */
public int $flags = self::FLAG_NONE;
/** @psalm-readonly */
protected ContextInterface $context;
/** @psalm-readonly string */
protected string $type;
/** @psalm-var RepresentationInterface[] */
protected array $representations = [];
public function __construct(ContextInterface $context, string $type)
{
$this->context = $context;
$this->type = $type;
}
public function __clone()
{
$this->context = clone $this->context;
}
public function getContext(): ContextInterface
{
return $this->context;
}
public function getHint(): ?string
{
if (self::FLAG_NONE === $this->flags) {
return null;
}
if ($this->flags & self::FLAG_BLACKLIST) {
return 'blacklist';
}
if ($this->flags & self::FLAG_RECURSION) {
return 'recursion';
}
if ($this->flags & self::FLAG_DEPTH_LIMIT) {
return 'depth_limit';
}
if ($this->flags & self::FLAG_ARRAY_LIMIT) {
return 'array_limit';
}
return null;
}
public function getType(): string
{
return $this->type;
}
public function addRepresentation(RepresentationInterface $rep, ?int $pos = null): void
{
if (isset($this->representations[$rep->getName()])) {
throw new OutOfRangeException('Representation already exists');
}
if (null === $pos) {
$this->representations[$rep->getName()] = $rep;
} else {
$this->representations = \array_merge(
\array_slice($this->representations, 0, $pos),
[$rep->getName() => $rep],
\array_slice($this->representations, $pos)
);
}
}
public function replaceRepresentation(RepresentationInterface $rep, ?int $pos = null): void
{
if (null === $pos) {
$this->representations[$rep->getName()] = $rep;
} else {
$this->removeRepresentation($rep);
$this->addRepresentation($rep, $pos);
}
}
/**
* @param RepresentationInterface|string $rep
*/
public function removeRepresentation($rep): void
{
if ($rep instanceof RepresentationInterface) {
unset($this->representations[$rep->getName()]);
} else { // String
unset($this->representations[$rep]);
}
}
public function getRepresentation(string $name): ?RepresentationInterface
{
return $this->representations[$name] ?? null;
}
/** @psalm-return RepresentationInterface[] */
public function getRepresentations(): array
{
return $this->representations;
}
/** @psalm-param RepresentationInterface[] $reps */
public function appendRepresentations(array $reps): void
{
foreach ($reps as $rep) {
$this->addRepresentation($rep);
}
}
/** @psalm-api */
public function clearRepresentations(): void
{
$this->representations = [];
}
public function getDisplayType(): string
{
return $this->type;
}
public function getDisplayName(): string
{
return (string) $this->context->getName();
}
public function getDisplaySize(): ?string
{
return null;
}
public function getDisplayValue(): ?string
{
return null;
}
/** @psalm-return AbstractValue[] */
public function getDisplayChildren(): array
{
return [];
}
}

View File

@ -0,0 +1,71 @@
<?php
declare(strict_types=1);
/*
* The MIT License (MIT)
*
* Copyright (c) 2013 Jonathan Vollebregt (jnvsor@gmail.com), Rokas Šleinius (raveren@gmail.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
namespace Kint\Value;
use Kint\Value\Context\ContextInterface;
class ArrayValue extends AbstractValue
{
/** @psalm-readonly */
protected int $size;
/**
* @psalm-readonly
*
* @psalm-var AbstractValue[]
*/
protected array $contents;
/** @psalm-param AbstractValue[] $contents */
public function __construct(ContextInterface $context, int $size, array $contents)
{
parent::__construct($context, 'array');
$this->size = $size;
$this->contents = $contents;
}
public function getSize(): int
{
return $this->size;
}
/** @psalm-return AbstractValue[] */
public function getContents()
{
return $this->contents;
}
public function getDisplaySize(): string
{
return (string) $this->size;
}
public function getDisplayChildren(): array
{
return $this->contents;
}
}

View File

@ -25,14 +25,19 @@ declare(strict_types=1);
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
namespace Kint\Renderer\Rich;
namespace Kint\Value;
use Kint\Zval\Value;
use Kint\Value\Context\ContextInterface;
class DepthLimitPlugin extends AbstractPlugin implements ValuePluginInterface
class ClosedResourceValue extends AbstractValue
{
public function renderValue(Value $o): string
public function __construct(ContextInterface $context)
{
return '<dl>'.$this->renderLockedHeader($o, '<var>Depth Limit</var>').'</dl>';
parent::__construct($context, 'resource (closed)');
}
public function getDisplayType(): string
{
return 'closed resource';
}
}

View File

@ -0,0 +1,120 @@
<?php
declare(strict_types=1);
/*
* The MIT License (MIT)
*
* Copyright (c) 2013 Jonathan Vollebregt (jnvsor@gmail.com), Rokas Šleinius (raveren@gmail.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
namespace Kint\Value;
use Closure;
use Kint\Utils;
use Kint\Value\Context\BaseContext;
use Kint\Value\Context\ContextInterface;
use ReflectionFunction;
class ClosureValue extends InstanceValue
{
use ParameterHoldingTrait;
/** @psalm-readonly */
protected ?string $filename;
/** @psalm-readonly */
protected ?int $startline;
public function __construct(ContextInterface $context, Closure $cl)
{
parent::__construct($context, \get_class($cl), \spl_object_hash($cl), \spl_object_id($cl));
/**
* @psalm-suppress UnnecessaryVarAnnotation
*
* @psalm-var ContextInterface $this->context
* Psalm bug #11113
*/
$closure = new ReflectionFunction($cl);
if ($closure->isUserDefined()) {
$this->filename = $closure->getFileName();
$this->startline = $closure->getStartLine();
} else {
$this->filename = null;
$this->startline = null;
}
$parameters = [];
foreach ($closure->getParameters() as $param) {
$parameters[] = new ParameterBag($param);
}
$this->parameters = $parameters;
if (!$this->context instanceof BaseContext) {
return;
}
if (0 !== $this->context->getDepth()) {
return;
}
$ap = $this->context->getAccessPath();
if (null === $ap) {
return;
}
if (\preg_match('/^\\((function|fn)\\s*\\(/i', $ap, $match)) {
$this->context->name = \strtolower($match[1]);
}
}
/** @psalm-api */
public function getFileName(): ?string
{
return $this->filename;
}
/** @psalm-api */
public function getStartLine(): ?int
{
return $this->startline;
}
public function getDisplaySize(): ?string
{
return null;
}
public function getDisplayName(): string
{
return $this->context->getName().'('.$this->getParams().')';
}
public function getDisplayValue(): ?string
{
if (null !== $this->filename && null !== $this->startline) {
return Utils::shortenPath($this->filename).':'.$this->startline;
}
return parent::getDisplayValue();
}
}

View File

@ -25,14 +25,12 @@ declare(strict_types=1);
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
namespace Kint\Renderer\Text;
namespace Kint\Value;
use Kint\Zval\Value;
class EnumPlugin extends AbstractPlugin
class ColorValue extends StringValue
{
public function render(Value $o): string
public function getHint(): string
{
return $this->renderLockedHeader($o);
return parent::getHint() ?? 'color';
}
}

View File

@ -25,14 +25,12 @@ declare(strict_types=1);
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
namespace Kint\Renderer\Text;
namespace Kint\Value\Context;
use Kint\Zval\Value;
class RecursionPlugin extends AbstractPlugin
class ArrayContext extends BaseContext
{
public function render(Value $o): string
public function getOperator(): ?string
{
return $this->renderLockedHeader($o, 'RECURSION');
return '=>';
}
}

View File

@ -0,0 +1,83 @@
<?php
declare(strict_types=1);
/*
* The MIT License (MIT)
*
* Copyright (c) 2013 Jonathan Vollebregt (jnvsor@gmail.com), Rokas Šleinius (raveren@gmail.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
namespace Kint\Value\Context;
use InvalidArgumentException;
/**
* @psalm-import-type ValueName from ContextInterface
*/
class BaseContext implements ContextInterface
{
/** @psalm-var ValueName */
public $name;
public int $depth = 0;
public bool $reference = false;
public ?string $access_path = null;
/** @psalm-param mixed $name */
public function __construct($name)
{
if (!\is_string($name) && !\is_int($name)) {
throw new InvalidArgumentException('Context names must be string|int');
}
$this->name = $name;
}
public function getName()
{
return $this->name;
}
public function getDepth(): int
{
return $this->depth;
}
public function getOperator(): ?string
{
return null;
}
public function isRef(): bool
{
return $this->reference;
}
/** @psalm-param ?class-string $scope */
public function isAccessible(?string $scope): bool
{
return true;
}
public function getAccessPath(): ?string
{
return $this->access_path;
}
}

View File

@ -25,14 +25,21 @@ declare(strict_types=1);
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
namespace Kint\Renderer\Rich;
namespace Kint\Value\Context;
use Kint\Zval\Value;
class RecursionPlugin extends AbstractPlugin implements ValuePluginInterface
class ClassConstContext extends ClassDeclaredContext
{
public function renderValue(Value $o): string
public bool $final = false;
public function getOperator(): string
{
return '<dl>'.$this->renderLockedHeader($o, '<var>Recursion</var>').'</dl>';
return '::';
}
public function getModifiers(): string
{
$final = $this->final ? 'final ' : '';
return $final.$this->getAccess().' const';
}
}

View File

@ -0,0 +1,94 @@
<?php
declare(strict_types=1);
/*
* The MIT License (MIT)
*
* Copyright (c) 2013 Jonathan Vollebregt (jnvsor@gmail.com), Rokas Šleinius (raveren@gmail.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
namespace Kint\Value\Context;
use __PHP_Incomplete_Class;
abstract class ClassDeclaredContext extends ClassOwnedContext
{
public const ACCESS_PUBLIC = 1;
public const ACCESS_PROTECTED = 2;
public const ACCESS_PRIVATE = 3;
/** @psalm-var self::ACCESS_* */
public int $access;
/**
* @psalm-param class-string $owner_class
* @psalm-param self::ACCESS_* $access
*/
public function __construct(string $name, string $owner_class, int $access)
{
parent::__construct($name, $owner_class);
$this->access = $access;
}
abstract public function getModifiers(): string;
/** @psalm-param ?class-string $scope */
public function isAccessible(?string $scope): bool
{
if (__PHP_Incomplete_Class::class === $this->owner_class) {
return false;
}
if (self::ACCESS_PUBLIC === $this->access) {
return true;
}
if (null === $scope) {
return false;
}
if (self::ACCESS_PRIVATE === $this->access) {
return $scope === $this->owner_class;
}
if (\is_a($scope, $this->owner_class, true)) {
return true;
}
if (\is_a($this->owner_class, $scope, true)) {
return true;
}
return false;
}
protected function getAccess(): string
{
switch ($this->access) {
case self::ACCESS_PUBLIC:
return 'public';
case self::ACCESS_PROTECTED:
return 'protected';
case self::ACCESS_PRIVATE:
return 'private';
}
}
}

View File

@ -25,30 +25,35 @@ declare(strict_types=1);
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
namespace Kint\Zval;
namespace Kint\Value\Context;
class SimpleXMLElementValue extends InstanceValue
use __PHP_Incomplete_Class;
class ClassOwnedContext extends BaseContext
{
public $hints = ['object', 'simplexml_element'];
/** @psalm-var class-string */
public string $owner_class;
protected $is_string_value = false;
public function isStringValue(): bool
/** @psalm-param class-string $owner_class */
public function __construct(string $name, string $owner_class)
{
return $this->is_string_value;
parent::__construct($name);
$this->owner_class = $owner_class;
}
public function setIsStringValue(bool $is_string_value): void
public function getName(): string
{
$this->is_string_value = $is_string_value;
return (string) $this->name;
}
public function getValueShort(): ?string
public function getOperator(): string
{
if ($this->is_string_value && ($rep = $this->value) && 'contents' === $rep->getName() && 'string' === \gettype($rep->contents)) {
return '"'.$rep->contents.'"';
}
return '->';
}
return null;
/** @psalm-param ?class-string $scope */
public function isAccessible(?string $scope): bool
{
return __PHP_Incomplete_Class::class !== $this->owner_class;
}
}

View File

@ -25,32 +25,29 @@ declare(strict_types=1);
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
namespace Kint\Zval;
namespace Kint\Value\Context;
use Kint\Kint;
class StreamValue extends ResourceValue
/**
* Contexts represent the data that has to be found out about a zval _before_
* passing it into the next parse depth. This includes the access path, whether
* it's a reference or not, and OOP related stuff like visibility.
*
* @psalm-type ValueName = string|int
*/
interface ContextInterface
{
public $stream_meta;
/** @psalm-return ValueName */
public function getName();
public function __construct(array $meta = null)
{
parent::__construct();
$this->stream_meta = $meta;
}
public function getDepth(): int;
public function getValueShort(): ?string
{
if (empty($this->stream_meta['uri'])) {
return null;
}
public function isRef(): bool;
$uri = $this->stream_meta['uri'];
/** @psalm-param ?class-string $scope */
public function isAccessible(?string $scope): bool;
if (\stream_is_local($uri)) {
return Kint::shortenPath($uri);
}
public function getAccessPath(): ?string;
return $uri;
}
/** @psalm-return ?non-empty-string */
public function getOperator(): ?string;
}

View File

@ -25,39 +25,35 @@ declare(strict_types=1);
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
namespace Kint\Zval;
namespace Kint\Value\Context;
trait ParameterHoldingTrait
abstract class DoubleAccessMemberContext extends ClassDeclaredContext
{
/** @var ParameterValue[] */
public $parameters = [];
/** @psalm-var ?self::ACCESS_* */
public ?int $access_set = null;
private $paramcache;
public function getParams(): string
protected function getAccess(): string
{
if (null !== $this->paramcache) {
return $this->paramcache;
switch ($this->access) {
case self::ACCESS_PUBLIC:
if (self::ACCESS_PRIVATE === $this->access_set) {
return 'private(set)';
}
if (self::ACCESS_PROTECTED === $this->access_set) {
return 'protected(set)';
}
return 'public';
case self::ACCESS_PROTECTED:
if (self::ACCESS_PRIVATE === $this->access_set) {
return 'protected private(set)';
}
return 'protected';
case self::ACCESS_PRIVATE:
return 'private';
}
$out = [];
foreach ($this->parameters as $p) {
$type = $p->getType();
if ($type) {
$type .= ' ';
}
$default = $p->getDefault();
if ($default) {
$default = ' = '.$default;
}
$ref = $p->reference ? '&' : '';
$out[] = $type.$ref.$p->getName().$default;
}
return $this->paramcache = \implode(', ', $out);
}
}

View File

@ -0,0 +1,140 @@
<?php
declare(strict_types=1);
/*
* The MIT License (MIT)
*
* Copyright (c) 2013 Jonathan Vollebregt (jnvsor@gmail.com), Rokas Šleinius (raveren@gmail.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
namespace Kint\Value\Context;
use Kint\Value\InstanceValue;
use ReflectionMethod;
class MethodContext extends ClassDeclaredContext
{
public const MAGIC_NAMES = [
'__construct' => true,
'__destruct' => true,
'__call' => true,
'__callstatic' => true,
'__get' => true,
'__set' => true,
'__isset' => true,
'__unset' => true,
'__sleep' => true,
'__wakeup' => true,
'__serialize' => true,
'__unserialize' => true,
'__tostring' => true,
'__invoke' => true,
'__set_state' => true,
'__clone' => true,
'__debuginfo' => true,
];
public bool $final = false;
public bool $abstract = false;
public bool $static = false;
/**
* Whether the method was inherited from a parent class or interface.
*
* It's important to note that we never show static methods as
* "inherited" except when abstract via an interface.
*/
public bool $inherited = false;
public function __construct(ReflectionMethod $method)
{
parent::__construct(
$method->getName(),
$method->getDeclaringClass()->name,
ClassDeclaredContext::ACCESS_PUBLIC
);
$this->depth = 1;
$this->static = $method->isStatic();
$this->abstract = $method->isAbstract();
$this->final = $method->isFinal();
if ($method->isProtected()) {
$this->access = ClassDeclaredContext::ACCESS_PROTECTED;
} elseif ($method->isPrivate()) {
$this->access = ClassDeclaredContext::ACCESS_PRIVATE;
}
}
public function getOperator(): string
{
if ($this->static) {
return '::';
}
return '->';
}
public function getModifiers(): string
{
if ($this->abstract) {
$out = 'abstract ';
} elseif ($this->final) {
$out = 'final ';
} else {
$out = '';
}
$out .= $this->getAccess();
if ($this->static) {
$out .= ' static';
}
return $out;
}
public function setAccessPathFromParent(?InstanceValue $parent): void
{
$name = \strtolower($this->getName());
if ($this->static && !isset(self::MAGIC_NAMES[$name])) {
$this->access_path = '\\'.$this->owner_class.'::'.$this->name.'()';
} elseif (null === $parent) {
$this->access_path = null;
} else {
$c = $parent->getContext();
if ('__construct' === $name) {
$this->access_path = 'new \\'.$parent->getClassName().'()';
} elseif (null === $c->getAccessPath()) {
$this->access_path = null;
} elseif ('__invoke' === $name) {
$this->access_path = $c->getAccessPath().'()';
} elseif ('__clone' === $name) {
$this->access_path = 'clone '.$c->getAccessPath();
} elseif ('__tostring' === $name) {
$this->access_path = '(string) '.$c->getAccessPath();
} elseif (isset(self::MAGIC_NAMES[$name])) {
$this->access_path = null;
} else {
$this->access_path = $c->getAccessPath().'->'.$this->name.'()';
}
}
}
}

View File

@ -25,52 +25,54 @@ declare(strict_types=1);
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
namespace Kint\Renderer\Rich;
namespace Kint\Value\Context;
use Kint\Kint;
use Kint\Zval\Representation\MethodDefinitionRepresentation;
use Kint\Zval\Representation\Representation;
class MethodDefinitionPlugin extends AbstractPlugin implements TabPluginInterface
class PropertyContext extends DoubleAccessMemberContext
{
public function renderTab(Representation $r): ?string
public const HOOK_NONE = 0;
public const HOOK_GET = 1 << 0;
public const HOOK_GET_REF = 1 << 1;
public const HOOK_SET = 1 << 2;
public const HOOK_SET_TYPE = 1 << 3;
public bool $readonly = false;
/** @psalm-var int-mask-of<self::HOOK_*> */
public int $hooks = self::HOOK_NONE;
public ?string $hook_set_type = null;
public function getModifiers(): string
{
if (!$r instanceof MethodDefinitionRepresentation) {
$out = $this->getAccess();
if ($this->readonly) {
$out .= ' readonly';
}
return $out;
}
public function getHooks(): ?string
{
if (self::HOOK_NONE === $this->hooks) {
return null;
}
if (isset($r->contents)) {
$docstring = [];
foreach (\explode("\n", $r->contents) as $line) {
$docstring[] = \trim($line);
$out = '{ ';
if ($this->hooks & self::HOOK_GET) {
if ($this->hooks & self::HOOK_GET_REF) {
$out .= '&';
}
$docstring = $this->renderer->escape(\implode("\n", $docstring));
$out .= 'get; ';
}
$addendum = [];
if (isset($r->class) && $r->inherited) {
$addendum[] = 'Inherited from '.$this->renderer->escape($r->class);
}
if (isset($r->file, $r->line)) {
$addendum[] = 'Defined in '.$this->renderer->escape(Kint::shortenPath($r->file)).':'.((int) $r->line);
}
if ($addendum) {
$addendum = '<small>'.\implode("\n", $addendum).'</small>';
if (isset($docstring)) {
$docstring .= "\n\n".$addendum;
if ($this->hooks & self::HOOK_SET) {
if ($this->hooks & self::HOOK_SET_TYPE && '' !== ($this->hook_set_type ?? '')) {
$out .= 'set('.$this->hook_set_type.'); ';
} else {
$docstring = $addendum;
$out .= 'set; ';
}
}
$out .= '}';
if (!isset($docstring)) {
return null;
}
return '<pre>'.$docstring.'</pre>';
return $out;
}
}

View File

@ -25,14 +25,21 @@ declare(strict_types=1);
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
namespace Kint\Renderer\Rich;
namespace Kint\Value\Context;
use Kint\Zval\Value;
class ArrayLimitPlugin extends AbstractPlugin implements ValuePluginInterface
class StaticPropertyContext extends DoubleAccessMemberContext
{
public function renderValue(Value $o): string
public bool $final = false;
public function getOperator(): string
{
return '<dl>'.$this->renderLockedHeader($o, '<var>Array Limit</var>').'</dl>';
return '::';
}
public function getModifiers(): string
{
$final = $this->final ? 'final ' : '';
return $final.$this->getAccess().' static';
}
}

View File

@ -25,30 +25,40 @@ declare(strict_types=1);
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
namespace Kint\Zval;
namespace Kint\Value;
use DateTime;
use DateTimeInterface;
use Kint\Value\Context\ContextInterface;
class DateTimeValue extends InstanceValue
{
public $dt;
/** @psalm-readonly */
protected DateTimeInterface $dt;
public $hints = ['object', 'datetime'];
public function __construct(DateTime $dt)
public function __construct(ContextInterface $context, DateTimeInterface $dt)
{
parent::__construct();
parent::__construct($context, \get_class($dt), \spl_object_hash($dt), \spl_object_id($dt));
$this->dt = clone $dt;
}
public function getValueShort(): string
public function getHint(): string
{
return parent::getHint() ?? 'datetime';
}
public function getDisplayValue(): string
{
$stamp = $this->dt->format('Y-m-d H:i:s');
if ((int) ($micro = $this->dt->format('u'))) {
$stamp .= '.'.$micro;
}
$stamp .= $this->dt->format('P T');
$stamp .= $this->dt->format(' P');
$tzn = $this->dt->getTimezone()->getName();
if ('+' !== $tzn[0] && '-' !== $tzn[0]) {
$stamp .= $this->dt->format(' T');
}
return $stamp;
}

View File

@ -0,0 +1,81 @@
<?php
declare(strict_types=1);
/*
* The MIT License (MIT)
*
* Copyright (c) 2013 Jonathan Vollebregt (jnvsor@gmail.com), Rokas Šleinius (raveren@gmail.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
namespace Kint\Value;
use Kint\Utils;
use ReflectionFunctionAbstract;
/** @psalm-api */
final class DeclaredCallableBag
{
use ParameterHoldingTrait;
/** @psalm-readonly */
public bool $internal;
/** @psalm-readonly */
public ?string $filename;
/** @psalm-readonly */
public ?int $startline;
/** @psalm-readonly */
public ?int $endline;
/**
* @psalm-readonly
*
* @psalm-var ?non-empty-string
*/
public ?string $docstring;
/** @psalm-readonly */
public bool $return_reference;
/** @psalm-readonly */
public ?string $returntype = null;
public function __construct(ReflectionFunctionAbstract $callable)
{
$this->internal = $callable->isInternal();
$t = $callable->getFileName();
$this->filename = false === $t ? null : $t;
$t = $callable->getStartLine();
$this->startline = false === $t ? null : $t;
$t = $callable->getEndLine();
$this->endline = false === $t ? null : $t;
$t = $callable->getDocComment();
$this->docstring = false === $t ? null : $t;
$this->return_reference = $callable->returnsReference();
$rt = $callable->getReturnType();
if ($rt) {
$this->returntype = Utils::getTypeString($rt);
}
$parameters = [];
foreach ($callable->getParameters() as $param) {
$parameters[] = new ParameterBag($param);
}
$this->parameters = $parameters;
}
}

View File

@ -25,32 +25,33 @@ declare(strict_types=1);
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
namespace Kint\Parser;
namespace Kint\Value;
use Kint\Zval\Value;
use SplObjectStorage;
use Dom\NodeList;
use DOMNodeList;
use Kint\Value\Context\ContextInterface;
class SplObjectStoragePlugin extends AbstractPlugin
class DomNodeListValue extends InstanceValue
{
public function getTypes(): array
protected int $length;
/**
* @psalm-param DOMNodeList|NodeList $node
*/
public function __construct(ContextInterface $context, object $node)
{
return ['object'];
parent::__construct($context, \get_class($node), \spl_object_hash($node), \spl_object_id($node));
$this->length = $node->length;
}
public function getTriggers(): int
public function getLength(): int
{
return Parser::TRIGGER_COMPLETE;
return $this->length;
}
public function parse(&$var, Value &$o, int $trigger): void
public function getDisplaySize(): string
{
if (!$var instanceof SplObjectStorage || !($r = $o->getRepresentation('iterator'))) {
return;
}
$r = $o->getRepresentation('iterator');
if ($r) {
$o->size = !\is_array($r->contents) ? null : \count($r->contents);
}
return (string) $this->length;
}
}

View File

@ -25,20 +25,24 @@ declare(strict_types=1);
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
namespace Kint\Renderer\Rich;
namespace Kint\Value;
use DateTime;
use DateTimeZone;
use Kint\Zval\Representation\Representation;
use Dom\Node;
use DOMNode;
use Kint\Value\Context\ContextInterface;
class TimestampPlugin extends AbstractPlugin implements TabPluginInterface
class DomNodeValue extends InstanceValue
{
public function renderTab(Representation $r): ?string
/**
* @psalm-param DOMNode|Node $node
*/
public function __construct(ContextInterface $context, object $node)
{
if ($dt = DateTime::createFromFormat('U', (string) $r->contents)) {
return '<pre>'.$dt->setTimeZone(new DateTimeZone('UTC'))->format('Y-m-d H:i:s T').'</pre>';
}
parent::__construct($context, \get_class($node), \spl_object_hash($node), \spl_object_id($node));
}
public function getDisplaySize(): ?string
{
return null;
}
}

View File

@ -25,50 +25,44 @@ declare(strict_types=1);
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
namespace Kint\Zval;
namespace Kint\Value;
use BackedEnum;
use Kint\Value\Context\ContextInterface;
use UnitEnum;
class EnumValue extends InstanceValue
{
public $enumval;
/** @psalm-readonly */
protected UnitEnum $enumval;
public $hints = ['object', 'enum'];
public function __construct(UnitEnum $enumval)
public function __construct(ContextInterface $context, UnitEnum $enumval)
{
parent::__construct($context, \get_class($enumval), \spl_object_hash($enumval), \spl_object_id($enumval));
$this->enumval = $enumval;
}
public function getValueShort(): ?string
public function getHint(): string
{
return parent::getHint() ?? 'enum';
}
public function getDisplayType(): string
{
return $this->classname.'::'.$this->enumval->name;
}
public function getDisplayValue(): ?string
{
if ($this->enumval instanceof BackedEnum) {
if (\is_string($this->enumval->value)) {
return '"'.$this->enumval->value.'"';
} // Int
}
return (string) $this->enumval->value;
}
return null;
}
public function getType(): ?string
{
if (isset($this->classname)) {
if (isset($this->enumval->name)) {
return $this->classname.'::'.$this->enumval->name;
}
return $this->classname;
}
return null;
}
public function getSize(): ?string
{
return null;
}
}

View File

@ -0,0 +1,85 @@
<?php
declare(strict_types=1);
/*
* The MIT License (MIT)
*
* Copyright (c) 2013 Jonathan Vollebregt (jnvsor@gmail.com), Rokas Šleinius (raveren@gmail.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
namespace Kint\Value;
use InvalidArgumentException;
use Kint\Value\Context\ContextInterface;
/**
* @psalm-type FixedWidthType = null|boolean|integer|double
*/
class FixedWidthValue extends AbstractValue
{
/**
* @psalm-readonly
*
* @psalm-var FixedWidthType
*/
protected $value;
/** @psalm-param FixedWidthType $value */
public function __construct(ContextInterface $context, $value)
{
$type = \strtolower(\gettype($value));
if ('null' === $type || 'boolean' === $type || 'integer' === $type || 'double' === $type) {
parent::__construct($context, $type);
$this->value = $value;
} else {
throw new InvalidArgumentException('FixedWidthValue can only contain fixed width types, got '.$type);
}
}
/**
* @psalm-api
*
* @psalm-return FixedWidthType
*/
public function getValue()
{
return $this->value;
}
public function getDisplaySize(): ?string
{
return null;
}
public function getDisplayValue(): ?string
{
if ('boolean' === $this->type) {
return ((bool) $this->value) ? 'true' : 'false';
}
if ('integer' === $this->type || 'double' === $this->type) {
return (string) $this->value;
}
return null;
}
}

View File

@ -0,0 +1,98 @@
<?php
declare(strict_types=1);
/*
* The MIT License (MIT)
*
* Copyright (c) 2013 Jonathan Vollebregt (jnvsor@gmail.com), Rokas Šleinius (raveren@gmail.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
namespace Kint\Value;
use Kint\Value\Context\ContextInterface;
use Kint\Value\Representation\CallableDefinitionRepresentation;
class FunctionValue extends AbstractValue
{
/** @psalm-readonly */
protected DeclaredCallableBag $callable_bag;
/** @psalm-readonly */
protected ?CallableDefinitionRepresentation $definition_rep;
public function __construct(ContextInterface $c, DeclaredCallableBag $bag)
{
parent::__construct($c, 'function');
$this->callable_bag = $bag;
if ($this->callable_bag->internal) {
$this->definition_rep = null;
return;
}
/**
* @psalm-var string $this->callable_bag->filename
* @psalm-var int $this->callable_bag->startline
* Psalm issue #11121
*/
$this->definition_rep = new CallableDefinitionRepresentation(
$this->callable_bag->filename,
$this->callable_bag->startline,
$this->callable_bag->docstring
);
$this->addRepresentation($this->definition_rep);
}
public function getCallableBag(): DeclaredCallableBag
{
return $this->callable_bag;
}
/** @psalm-api */
public function getDefinitionRepresentation(): ?CallableDefinitionRepresentation
{
return $this->definition_rep;
}
public function getDisplayName(): string
{
return $this->context->getName().'('.$this->callable_bag->getParams().')';
}
public function getDisplayValue(): ?string
{
if ($this->definition_rep instanceof CallableDefinitionRepresentation) {
return $this->definition_rep->getDocstringFirstLine();
}
return parent::getDisplayValue();
}
public function getPhpDocUrl(): ?string
{
if (!$this->callable_bag->internal) {
return null;
}
return 'https://www.php.net/function.'.\str_replace('_', '-', \strtolower((string) $this->context->getName()));
}
}

View File

@ -0,0 +1,111 @@
<?php
declare(strict_types=1);
/*
* The MIT License (MIT)
*
* Copyright (c) 2013 Jonathan Vollebregt (jnvsor@gmail.com), Rokas Šleinius (raveren@gmail.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
namespace Kint\Value;
use Kint\Value\Context\ContextInterface;
class InstanceValue extends AbstractValue
{
/**
* @psalm-readonly
*
* @psalm-var class-string
*/
protected string $classname;
/** @psalm-readonly */
protected string $spl_object_hash;
/** @psalm-readonly */
protected int $spl_object_id;
/**
* The canonical children of this value, for text renderers.
*
* @psalm-var null|list<AbstractValue>
*/
protected ?array $children = null;
/** @psalm-param class-string $classname */
public function __construct(
ContextInterface $context,
string $classname,
string $spl_object_hash,
int $spl_object_id
) {
parent::__construct($context, 'object');
$this->classname = $classname;
$this->spl_object_hash = $spl_object_hash;
$this->spl_object_id = $spl_object_id;
}
/** @psalm-return class-string */
public function getClassName(): string
{
return $this->classname;
}
public function getSplObjectHash(): string
{
return $this->spl_object_hash;
}
public function getSplObjectId(): int
{
return $this->spl_object_id;
}
/** @psalm-param null|list<AbstractValue> $children */
public function setChildren(?array $children): void
{
$this->children = $children;
}
/** @psalm-return null|list<AbstractValue> */
public function getChildren(): ?array
{
return $this->children;
}
public function getDisplayType(): string
{
return $this->classname;
}
public function getDisplaySize(): ?string
{
if (null === $this->children) {
return null;
}
return (string) \count($this->children);
}
public function getDisplayChildren(): array
{
return $this->children ?? [];
}
}

View File

@ -0,0 +1,130 @@
<?php
declare(strict_types=1);
/*
* The MIT License (MIT)
*
* Copyright (c) 2013 Jonathan Vollebregt (jnvsor@gmail.com), Rokas Šleinius (raveren@gmail.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
namespace Kint\Value;
use Kint\Value\Context\ClassDeclaredContext;
use Kint\Value\Context\MethodContext;
use Kint\Value\Representation\CallableDefinitionRepresentation;
class MethodValue extends AbstractValue
{
/** @psalm-readonly */
protected DeclaredCallableBag $callable_bag;
/** @psalm-readonly */
protected ?CallableDefinitionRepresentation $definition_rep;
public function __construct(MethodContext $c, DeclaredCallableBag $bag)
{
parent::__construct($c, 'method');
$this->callable_bag = $bag;
if ($this->callable_bag->internal) {
$this->definition_rep = null;
return;
}
/**
* @psalm-var string $this->callable_bag->filename
* @psalm-var int $this->callable_bag->startline
* Psalm issue #11121
*/
$this->definition_rep = new CallableDefinitionRepresentation(
$this->callable_bag->filename,
$this->callable_bag->startline,
$this->callable_bag->docstring
);
$this->addRepresentation($this->definition_rep);
}
public function getHint(): string
{
return parent::getHint() ?? 'callable';
}
public function getContext(): MethodContext
{
/**
* @psalm-var MethodContext $this->context
* Psalm discuss #11116
*/
return $this->context;
}
public function getCallableBag(): DeclaredCallableBag
{
return $this->callable_bag;
}
/** @psalm-api */
public function getDefinitionRepresentation(): ?CallableDefinitionRepresentation
{
return $this->definition_rep;
}
public function getDisplayName(): string
{
$c = $this->getContext();
$name = $c->getName();
if ($c->static || (ClassDeclaredContext::ACCESS_PRIVATE === $c->access && $c->inherited)) {
$name = $c->owner_class.'::'.$name;
}
return $name.'('.$this->callable_bag->getParams().')';
}
public function getDisplayValue(): ?string
{
if ($this->definition_rep instanceof CallableDefinitionRepresentation) {
return $this->definition_rep->getDocstringFirstLine();
}
return parent::getDisplayValue();
}
public function getPhpDocUrl(): ?string
{
if (!$this->callable_bag->internal) {
return null;
}
$c = $this->getContext();
$class = \str_replace('\\', '-', \strtolower($c->owner_class));
$funcname = \str_replace('_', '-', \strtolower($c->getName()));
if (0 === \strpos($funcname, '--') && 0 !== \strpos($funcname, '-', 2)) {
$funcname = \substr($funcname, 2);
}
return 'https://www.php.net/'.$class.'.'.$funcname;
}
}

View File

@ -0,0 +1,63 @@
<?php
declare(strict_types=1);
/*
* The MIT License (MIT)
*
* Copyright (c) 2013 Jonathan Vollebregt (jnvsor@gmail.com), Rokas Šleinius (raveren@gmail.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
namespace Kint\Value;
class MicrotimeValue extends AbstractValue
{
/** @psalm-readonly */
protected AbstractValue $wrapped;
public function __construct(AbstractValue $old)
{
$this->context = $old->context;
$this->type = $old->type;
$this->flags = $old->flags;
$this->representations = $old->representations;
$this->wrapped = $old;
}
public function getHint(): string
{
return parent::getHint() ?? 'microtime';
}
public function getDisplaySize(): ?string
{
return $this->wrapped->getDisplaySize();
}
public function getDisplayValue(): ?string
{
return $this->wrapped->getDisplayValue();
}
public function getDisplayType(): string
{
return $this->wrapped->getDisplayType();
}
}

Some files were not shown because too many files have changed in this diff Show More