Skip to content

Commit

Permalink
Representation revamp
Browse files Browse the repository at this point in the history
  • Loading branch information
jnvsor committed Oct 24, 2024
1 parent 521a000 commit 39ff4c3
Show file tree
Hide file tree
Showing 87 changed files with 2,089 additions and 1,713 deletions.
Binary file modified build/kint.phar
Binary file not shown.
49 changes: 27 additions & 22 deletions src/Parser/ArrayLimitPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@
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\Representation;
use Kint\Value\Representation\ValueRepresentation;

class ArrayLimitPlugin extends AbstractPlugin implements PluginBeginInterface
{
Expand All @@ -53,6 +54,19 @@ class ArrayLimitPlugin extends AbstractPlugin implements PluginBeginInterface
*/
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
{
return ['array'];
Expand All @@ -65,10 +79,6 @@ public function getTriggers(): int

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();

Expand Down Expand Up @@ -116,21 +126,16 @@ public function parseBegin(&$var, ContextInterface $c): ?AbstractValue
$out->appendHints($array->getHints());

// Explicitly copy over profile plugin
$profile = $array->getRepresentation('profiling');
if ($profile instanceof ProfileRepresentation) {
$slicep = $slice->getRepresentation('profiling');
if ($slicep instanceof ProfileRepresentation) {
$profile->complexity += $slicep->complexity;
}

$out->addRepresentation($profile);
$arrayp = $array->getRepresentation('profiling');
$slicep = $slice->getRepresentation('profiling');
if ($arrayp instanceof ProfileRepresentation && $slicep instanceof ProfileRepresentation) {
$out->addRepresentation(new ProfileRepresentation($arrayp->complexity + $slicep->complexity));
}

// Add contents
$rep = new Representation('Contents');
$rep->implicit_label = true;
$rep->contents = $out->getContents();
$out->addRepresentation($rep);
// 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;
}
Expand All @@ -153,12 +158,12 @@ protected function replaceDepthLimit(AbstractValue $v, int $depth): void
$reps = $v->getRepresentations();

foreach ($reps as $rep) {
if ($rep->contents instanceof AbstractValue) {
$this->replaceDepthLimit($rep->contents, $depth + 1);
} elseif (\is_array($rep->contents)) {
foreach ($rep->contents as $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);
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/Parser/Base64Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

use Kint\Value\AbstractValue;
use Kint\Value\Context\BaseContext;
use Kint\Value\Representation\Representation;
use Kint\Value\Representation\ValueRepresentation;
use Kint\Value\StringValue;

class Base64Plugin extends AbstractPlugin implements PluginCompleteInterface
Expand Down Expand Up @@ -83,13 +83,14 @@ public function parseComplete(&$var, AbstractValue $v, int $trigger): AbstractVa
$base->access_path = 'base64_decode('.$ap.')';
}

$r = new Representation('Base64');
$r->contents = $this->getParser()->parse($data, $base);
$data = $this->getParser()->parse($data, $base);

if ($r->contents instanceof StringValue && false === $r->contents->getEncoding()) {
if (!$data instanceof StringValue || false === $data->getEncoding()) {
return $v;
}

$r = new ValueRepresentation('Base64', $data);

if (\strlen($var) > self::$min_length_soft) {
$v->addRepresentation($r, 0);
} else {
Expand Down
12 changes: 2 additions & 10 deletions src/Parser/BinaryPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
namespace Kint\Parser;

use Kint\Value\AbstractValue;
use Kint\Value\Representation\Representation;
use Kint\Value\Representation\BinaryRepresentation;
use Kint\Value\StringValue;

class BinaryPlugin extends AbstractPlugin implements PluginCompleteInterface
Expand All @@ -46,15 +46,7 @@ public function getTriggers(): int
public function parseComplete(&$var, AbstractValue $v, int $trigger): AbstractValue
{
if ($v instanceof StringValue && false === $v->getEncoding()) {
$rep = $v->getRepresentation('contents');

if (!$rep) {
$rep = new Representation('Contents');
$rep->implicit_label = true;
$rep->contents = $v->getValue();
}

$rep->hints['binary'] = true;
$v->addRepresentation(new BinaryRepresentation($v->getValue(), true), 0);
}

return $v;
Expand Down
12 changes: 5 additions & 7 deletions src/Parser/ClassHooksPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
use Kint\Value\Context\PropertyContext;
use Kint\Value\InstanceValue;
use Kint\Value\MethodValue;
use Kint\Value\Representation\Representation;
use Kint\Value\Representation\ContainerRepresentation;
use ReflectionProperty;

class ClassHooksPlugin extends AbstractPlugin implements PluginCompleteInterface
Expand Down Expand Up @@ -62,13 +62,13 @@ public function parseComplete(&$var, AbstractValue $v, int $trigger): AbstractVa
return $v;
}

$props = $v->getRepresentation('properties')->contents ?? null;
$props = $v->getRepresentation('properties');

if (!\is_array($props) || !\count($props)) {
if (!$props instanceof ContainerRepresentation) {
return $v;
}

foreach ($props as $prop) {
foreach ($props->getContents() as $prop) {
$c = $prop->getContext();

if (!$c instanceof PropertyContext || PropertyContext::HOOK_NONE === $c->hooks) {
Expand Down Expand Up @@ -108,9 +108,7 @@ public function parseComplete(&$var, AbstractValue $v, int $trigger): AbstractVa
$cache = $cache[$cowner][$cname] ?? [];

if (\count($cache)) {
$r = new Representation('Hooks', 'propertyhooks');
$r->contents = $cache;
$prop->addRepresentation($r);
$prop->addRepresentation(new ContainerRepresentation('Hooks', $cache, 'propertyhooks'));
}
}

Expand Down
10 changes: 4 additions & 6 deletions src/Parser/ClassMethodsPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
use Kint\Value\InstanceValue;
use Kint\Value\MethodValue;
use Kint\Value\Representation\CallableDefinitionRepresentation;
use Kint\Value\Representation\Representation;
use Kint\Value\Representation\ContainerRepresentation;
use ReflectionClass;

/**
Expand Down Expand Up @@ -112,11 +112,9 @@ public function parseComplete(&$var, AbstractValue $v, int $trigger): AbstractVa
}

if (!empty(self::$cache[$class])) {
$rep = new Representation('Available methods', 'methods');
$rep->contents = [];

$cdepth = $v->getContext()->getDepth();
$parser = $this->getParser();
$contents = [];

// Can't cache access paths or depth
foreach (self::$cache[$class] as $key => $m) {
Expand All @@ -139,10 +137,10 @@ public function parseComplete(&$var, AbstractValue $v, int $trigger): AbstractVa
$mc->name = $mc->owner_class.'::'.$mc->name;
}

$rep->contents[] = $method;
$contents[] = $method;
}

$v->addRepresentation($rep);
$v->addRepresentation(new ContainerRepresentation('Available methods', $contents, 'methods'));
}

return $v;
Expand Down
26 changes: 11 additions & 15 deletions src/Parser/ClassStaticsPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
use Kint\Value\Context\ClassOwnedContext;
use Kint\Value\Context\StaticPropertyContext;
use Kint\Value\InstanceValue;
use Kint\Value\Representation\Representation;
use Kint\Value\Representation\ContainerRepresentation;
use Kint\Value\UninitializedValue;
use ReflectionClass;
use ReflectionClassConstant;
Expand Down Expand Up @@ -74,28 +74,24 @@ public function parseComplete(&$var, AbstractValue $v, int $trigger): AbstractVa
$pdepth = $parser->getDepthLimit();
$r = new ReflectionClass($class);

$crep = new Representation('Class constants', 'constants');
$crep->contents = $this->getCachedConstants($r);

$statics_full_name = false;
$statics = [];
$props = $r->getProperties(ReflectionProperty::IS_STATIC);
foreach ($props as $prop) {
$statics[$prop->name] = $prop;
}

while ($r = $r->getParentClass()) {
foreach ($r->getProperties(ReflectionProperty::IS_STATIC) as $static) {
$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;
}
$statics[] = $static;
}
}

$srep = new Representation('Static properties', 'statics');
$srep->contents = [];

$statics_parsed = [];
$found_statics = [];

$cdepth = $v->getContext()->getDepth();
Expand Down Expand Up @@ -140,19 +136,19 @@ public function parseComplete(&$var, AbstractValue $v, int $trigger): AbstractVa
* Appears to have been fixed in master
*/
if (!$static->isInitialized()) {
$srep->contents[] = new UninitializedValue($prop);
$statics_parsed[] = new UninitializedValue($prop);
} else {
$static = $static->getValue();
$srep->contents[] = $parser->parse($static, $prop);
$statics_parsed[] = $parser->parse($static, $prop);
}
}

if (\count($srep->contents)) {
$v->addRepresentation($srep);
if ($statics_parsed) {
$v->addRepresentation(new ContainerRepresentation('Static properties', $statics_parsed, 'statics'));
}

if (\count($crep->contents)) {
$v->addRepresentation($crep);
if ($consts = $this->getCachedConstants($r)) {
$v->addRepresentation(new ContainerRepresentation('Class constants', $consts, 'constants'));
}

return $v;
Expand Down
6 changes: 2 additions & 4 deletions src/Parser/ClosurePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
use Kint\Value\AbstractValue;
use Kint\Value\ClosureValue;
use Kint\Value\Context\BaseContext;
use Kint\Value\Representation\Representation;
use Kint\Value\Representation\ContainerRepresentation;
use ReflectionFunction;
use ReflectionReference;

Expand Down Expand Up @@ -85,9 +85,7 @@ public function parseComplete(&$var, AbstractValue $v, int $trigger): AbstractVa
$statics_parsed[$name] = $parser->parse($statics[$name], $base);
}

$r = new Representation('Uses');
$r->contents = $statics_parsed;
$object->addRepresentation($r, 0);
$object->addRepresentation(new ContainerRepresentation('Uses', $statics_parsed), 0);
}

return $object;
Expand Down
29 changes: 10 additions & 19 deletions src/Parser/DOMDocumentPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
use Kint\Value\DomNodeValue;
use Kint\Value\FixedWidthValue;
use Kint\Value\InstanceValue;
use Kint\Value\Representation\Representation;
use Kint\Value\Representation\ContainerRepresentation;
use Kint\Value\StringValue;
use LogicException;

Expand Down Expand Up @@ -273,9 +273,9 @@ private function parseList($var, ContextInterface $c): InstanceValue

$v->setChildren($contents);

$r = new Representation('Iterator');
$r->contents = $contents;
$v->addRepresentation($r, 0);
if ($contents) {
$v->addRepresentation(new ContainerRepresentation('Iterator', $contents), 0);
}

return $v;
}
Expand Down Expand Up @@ -326,6 +326,7 @@ private function parseNode(Node $var, ContextInterface $c): DomNodeValue
$children = [];
$attributes = [];

/** @psalm-var non-empty-array $known_properties */
foreach ($known_properties as $prop => $readonly) {
$prop_c = new PropertyContext($prop, $class, ClassDeclaredContext::ACCESS_PUBLIC);
$prop_c->depth = $cdepth + 1;
Expand All @@ -343,10 +344,8 @@ private function parseNode(Node $var, ContextInterface $c): DomNodeValue
}
$children = self::getChildren($prop_obj);
} elseif ('attributes' === $prop) {
$attributes = $prop_obj->getRepresentation('iterator')->contents ?? null;
if (!\is_array($attributes)) {
$attributes = [];
}
$attributes = $prop_obj->getRepresentation('iterator');
$attributes = $attributes instanceof ContainerRepresentation ? $attributes->getContents() : [];
}
}

Expand All @@ -355,23 +354,15 @@ private function parseNode(Node $var, ContextInterface $c): DomNodeValue
$v->setChildren($properties);

if ($children) {
$crep = new Representation('Children');
$crep->implicit_label = true;
$crep->contents = $children;

$v->addRepresentation($crep);
$v->addRepresentation(new ContainerRepresentation('Children', $children, null, true));
}

if ($attributes) {
$arep = new Representation('Attributes');
$arep->contents = $attributes;
$v->addRepresentation($arep);
$v->addRepresentation(new ContainerRepresentation('Attributes', $attributes));
}

if (self::$verbose) {
$props = new Representation('Properties');
$props->contents = $properties;
$v->addRepresentation($props);
$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);
Expand Down
Loading

0 comments on commit 39ff4c3

Please sign in to comment.