From 34baaa1bb39448c7a978eea8f44d2dda0a2889e2 Mon Sep 17 00:00:00 2001 From: Jonathan Vollebregt Date: Mon, 26 Aug 2024 18:44:42 +0200 Subject: [PATCH] Add 'omit_spl_id' hint to hide spl info of generated objects --- build/kint.phar | Bin 485749 -> 487307 bytes src/Parser/HtmlPlugin.php | 7 ++++++- src/Parser/SerializePlugin.php | 1 + src/Parser/SimpleXMLElementPlugin.php | 2 ++ src/Parser/XmlPlugin.php | 1 + src/Renderer/AbstractRenderer.php | 6 ++++++ src/Renderer/RendererInterface.php | 2 ++ src/Renderer/Rich/AbstractPlugin.php | 2 +- src/Renderer/Rich/ClosurePlugin.php | 6 +++++- src/Renderer/RichRenderer.php | 16 +++++++++++++++- src/Renderer/TextRenderer.php | 16 +++++++++++++++- tests/Parser/HtmlPluginTest.php | 4 ++++ tests/Parser/SerializePluginTest.php | 2 ++ tests/Parser/SimpleXMLElementPluginTest.php | 10 ++++++++++ tests/Parser/XmlPluginTest.php | 9 +++++++++ 15 files changed, 79 insertions(+), 5 deletions(-) diff --git a/build/kint.phar b/build/kint.phar index 4430b2e323fac492d410977cb244e69f24a3e85b..af20abb772cffdb638af0d8858a76df496b68b5c 100644 GIT binary patch delta 1624 zcmc&!ZERCj7~Z)(=eG1?yZh+c0&6a&u5+|oXA}ub#~9IV=v+7b;Re!mYhz1F+w|Vf zi7^{uoQ}p|Zo_MXLKl@p31p$c8{$VGeqi{4oj5|+G8Qv`AOkecj~^85-DPn_m+(X5 z`ST>tIq&m6=Xp=guisYey<9QIYMFD|f`XyXY^x>wWjt$3JG37n##Sh^5~_X+_=O*J zbxcdOOfH~sy*s_Oa10eOGuTf4gNrm+xjf)WUb+y0p86; zg6@FVS`TAM$^^9sDVpl{z|WJk0qV~v%Eg~{DJUYoG(oA~-JhgX@bR0J243H#&}f&I z1DZn};)MZ47YnJRf;KGE2G(ptrI2|Hso>B&Qj5-{qS>3Tt6QpD52K$`rNB;8jCeS! z*joaY8d6pk^@Y5?=pFA|?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ca32..996f94e09 100644 --- a/src/Parser/HtmlPlugin.php +++ b/src/Parser/HtmlPlugin.php @@ -98,7 +98,12 @@ public function parse(&$var, Value &$o, int $trigger): void } $r = new Representation('HTML'); - $r->contents = $iter->contents; + $r->contents = []; + + foreach ($iter->contents as $val) { + $val->hints[] = 'omit_spl_id'; + $r->contents[] = $val; + } $o->addRepresentation($r, 0); } diff --git a/src/Parser/SerializePlugin.php b/src/Parser/SerializePlugin.php index d284c4f9b..deceda52a 100644 --- a/src/Parser/SerializePlugin.php +++ b/src/Parser/SerializePlugin.php @@ -84,6 +84,7 @@ public function parse(&$var, Value &$o, int $trigger): void $base_obj = new Value('unserialize('.$o->name.')'); $base_obj->depth = $o->depth + 1; + $base_obj->hints[] = 'omit_spl_id'; if (null !== $o->access_path) { $base_obj->access_path = 'unserialize('.$o->access_path; diff --git a/src/Parser/SimpleXMLElementPlugin.php b/src/Parser/SimpleXMLElementPlugin.php index 07e2a9019..4c1e17a80 100644 --- a/src/Parser/SimpleXMLElementPlugin.php +++ b/src/Parser/SimpleXMLElementPlugin.php @@ -217,6 +217,8 @@ protected function getChildrenRepresentation(SimpleXMLElementValue $element, Sim $obj->name = $nsAlias.':'.$name; } + $obj->hints[] = 'omit_spl_id'; + if (null !== $element->access_path) { if ('' === $nsAlias) { $obj->access_path = $element->access_path.'->'; diff --git a/src/Parser/XmlPlugin.php b/src/Parser/XmlPlugin.php index 960bd7d06..6408c0c15 100644 --- a/src/Parser/XmlPlugin.php +++ b/src/Parser/XmlPlugin.php @@ -79,6 +79,7 @@ public function parse(&$var, Value &$o, int $trigger): void $base_obj = new Value($name); $base_obj->access_path = $access_path; $base_obj->depth = $o->depth + 1; + $base_obj->hints[] = 'omit_spl_id'; $r = new Representation('XML'); $r->contents = $this->getParser()->parse($xml, $base_obj); diff --git a/src/Renderer/AbstractRenderer.php b/src/Renderer/AbstractRenderer.php index 12452e40e..63f0ffcad 100644 --- a/src/Renderer/AbstractRenderer.php +++ b/src/Renderer/AbstractRenderer.php @@ -48,11 +48,17 @@ abstract class AbstractRenderer implements ConstructableRendererInterface protected array $call_info = []; protected array $statics = []; protected bool $show_trace = true; + 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'])) { diff --git a/src/Renderer/RendererInterface.php b/src/Renderer/RendererInterface.php index f9c30faa3..4bdebfd46 100644 --- a/src/Renderer/RendererInterface.php +++ b/src/Renderer/RendererInterface.php @@ -47,6 +47,8 @@ public function setShowTrace(bool $show_trace): void; public function getShowTrace(): bool; + public function shouldRenderObjectIds(): bool; + public function filterParserPlugins(array $plugins): array; public function preRender(): string; diff --git a/src/Renderer/Rich/AbstractPlugin.php b/src/Renderer/Rich/AbstractPlugin.php index bac4eb56f..032358d90 100644 --- a/src/Renderer/Rich/AbstractPlugin.php +++ b/src/Renderer/Rich/AbstractPlugin.php @@ -74,7 +74,7 @@ public function renderLockedHeader(Value $o, string $content): string $header .= ''.$s.''; - if ($o instanceof InstanceValue) { + if ($o instanceof InstanceValue && $this->renderer->shouldRenderObjectIds()) { $header .= '#'.$o->spl_object_id; } diff --git a/src/Renderer/Rich/ClosurePlugin.php b/src/Renderer/Rich/ClosurePlugin.php index 7223024ed..5a5663cae 100644 --- a/src/Renderer/Rich/ClosurePlugin.php +++ b/src/Renderer/Rich/ClosurePlugin.php @@ -47,7 +47,11 @@ public function renderValue(Value $o): ?string $header .= ''.$s.' '; } - $header .= ''.$this->renderer->escape($o->getName()).'('.$this->renderer->escape($o->getParams()).') Closure#'.$o->spl_object_id; + $header .= ''.$this->renderer->escape($o->getName()).'('.$this->renderer->escape($o->getParams()).') Closure'; + + if ($this->renderer->shouldRenderObjectIds()) { + $header .= '#'.$o->spl_object_id; + } if (null !== $o->filename) { $header .= ' '.$this->renderer->escape(Kint::shortenPath($o->filename)).':'.(int) $o->startline; diff --git a/src/Renderer/RichRenderer.php b/src/Renderer/RichRenderer.php index 9c8955415..e75c38cde 100644 --- a/src/Renderer/RichRenderer.php +++ b/src/Renderer/RichRenderer.php @@ -220,9 +220,19 @@ public function shouldFolderRender(): bool public function render(Value $o): string { + $render_spl_ids_stash = $this->render_spl_ids; + + if ($this->render_spl_ids && \in_array('omit_spl_id', $o->hints, true)) { + $this->render_spl_ids = false; + } + if (($plugin = $this->getPlugin(self::$value_plugins, $o->hints)) && $plugin instanceof ValuePluginInterface) { $output = $plugin->renderValue($o); if (null !== $output && \strlen($output)) { + if (!$this->render_spl_ids && $render_spl_ids_stash) { + $this->render_spl_ids = true; + } + return $output; } } @@ -230,6 +240,10 @@ public function render(Value $o): string $children = $this->renderChildren($o); $header = $this->renderHeaderWrapper($o, (bool) \strlen($children), $this->renderHeader($o)); + if (!$this->render_spl_ids && $render_spl_ids_stash) { + $this->render_spl_ids = true; + } + return '
'.$header.$children.'
'; } @@ -303,7 +317,7 @@ public function renderHeader(Value $o): string $output .= ''.$s.''; - if ($o instanceof InstanceValue) { + if ($o instanceof InstanceValue && $this->shouldRenderObjectIds()) { $output .= '#'.$o->spl_object_id; } diff --git a/src/Renderer/TextRenderer.php b/src/Renderer/TextRenderer.php index 71f99789c..9344b9d8d 100644 --- a/src/Renderer/TextRenderer.php +++ b/src/Renderer/TextRenderer.php @@ -124,9 +124,19 @@ public function __construct() public function render(Value $o): string { + $render_spl_ids_stash = $this->render_spl_ids; + + if ($this->render_spl_ids && \in_array('omit_spl_id', $o->hints, true)) { + $this->render_spl_ids = false; + } + if ($plugin = $this->getPlugin(self::$plugins, $o->hints)) { $output = $plugin->render($o); if (null !== $output && \strlen($output)) { + if (!$this->render_spl_ids && $render_spl_ids_stash) { + $this->render_spl_ids = true; + } + return $output; } } @@ -140,6 +150,10 @@ public function render(Value $o): string $out .= $this->renderHeader($o); $out .= $this->renderChildren($o).PHP_EOL; + if (!$this->render_spl_ids && $render_spl_ids_stash) { + $this->render_spl_ids = true; + } + return $out; } @@ -204,7 +218,7 @@ public function renderHeader(Value $o): string $s = $this->colorType($this->escape($s)); - if ($o instanceof InstanceValue) { + if ($o instanceof InstanceValue && $this->shouldRenderObjectIds()) { $s .= '#'.$o->spl_object_id; } diff --git a/tests/Parser/HtmlPluginTest.php b/tests/Parser/HtmlPluginTest.php index 2acc888a4..39c0156b4 100644 --- a/tests/Parser/HtmlPluginTest.php +++ b/tests/Parser/HtmlPluginTest.php @@ -66,18 +66,22 @@ public function testParse() $o = $p->parse($v, clone $b); + $this->assertNotContains('omit_spl_id', $o->hints); + $r = $o->getRepresentation('html'); $this->assertNotNull($r); $this->assertCount(2, $r->contents); $this->assertInstanceOf(InstanceValue::class, $r->contents[0]); + $this->assertContains('omit_spl_id', $r->contents[0]->hints); $this->assertSame(DocumentType::class, $r->contents[0]->classname); $this->assertSame('html', $r->contents[0]->name); $this->assertSame(0, $r->contents[0]->size); $this->assertSame('\\Dom\\HTMLDocument::createFromString($v)->childNodes->item(0)', $r->contents[0]->access_path); $this->assertInstanceOf(InstanceValue::class, $r->contents[1]); + $this->assertContains('omit_spl_id', $r->contents[1]->hints); $this->assertSame(HTMLElement::class, $r->contents[1]->classname); $this->assertSame('html', $r->contents[1]->name); $this->assertSame(0, $r->contents[1]->size); diff --git a/tests/Parser/SerializePluginTest.php b/tests/Parser/SerializePluginTest.php index 4f0d5cbab..eec9c9fda 100644 --- a/tests/Parser/SerializePluginTest.php +++ b/tests/Parser/SerializePluginTest.php @@ -88,6 +88,8 @@ public function testParse() 'unserialize($v, '.\var_export(['allowed_classes' => false], true).')[\'obj\']', $obj->getRepresentation('serialized')->contents->value->contents[0]->access_path ); + $this->assertNotContains('omit_spl_id', $obj->hints); + $this->assertNotContains('omit_spl_id', $obj->getRepresentation('serialized')->contents->value->hints); SerializePlugin::$allowed_classes = [self::class]; $obj = $p->parse($v, clone $b); diff --git a/tests/Parser/SimpleXMLElementPluginTest.php b/tests/Parser/SimpleXMLElementPluginTest.php index acb7ce5df..f383dd0d1 100644 --- a/tests/Parser/SimpleXMLElementPluginTest.php +++ b/tests/Parser/SimpleXMLElementPluginTest.php @@ -124,6 +124,7 @@ public function testParse() $this->assertSame('$v', $x->name); $this->assertSame('$v', $x->access_path); + $this->assertNotContains('omit_spl_id', $x->hints); $this->assertNull($x->getRepresentation('tostring')); $this->assertInstanceOf(Representation::class, $x->getRepresentation('children')); $this->assertInstanceOf(Representation::class, $x->getRepresentation('attributes')); @@ -144,6 +145,7 @@ public function testParse() $this->assertSame('g', $g1->name); $this->assertSame('$v->g', $g1->access_path); + $this->assertContains('omit_spl_id', $g1->hints); $this->assertNull($g1->getRepresentation('tostring')); $this->assertInstanceOf(Representation::class, $g1->getRepresentation('children')); $this->assertInstanceOf(Representation::class, $g1->getRepresentation('attributes')); @@ -168,6 +170,7 @@ public function testParse() $this->assertSame('inner', $inner->name); $this->assertSame('(string) $v->g->inner', $inner->access_path); + $this->assertContains('omit_spl_id', $inner->hints); $this->assertInstanceOf(Representation::class, $inner->getRepresentation('tostring')); $this->assertNull($inner->getRepresentation('children')); $this->assertNull($inner->getRepresentation('attributes')); @@ -184,6 +187,7 @@ public function testParse() $this->assertSame('g', $g2->name); $this->assertSame('$v->g[1]', $g2->access_path); + $this->assertContains('omit_spl_id', $g2->hints); $this->assertNull($g2->getRepresentation('tostring')); $this->assertNull($g2->getRepresentation('children')); $this->assertNull($g2->getRepresentation('attributes')); @@ -198,6 +202,7 @@ public function testParse() $this->assertSame('wrap', $wrap->name); $this->assertSame('$v->wrap', $wrap->access_path); + $this->assertContains('omit_spl_id', $wrap->hints); $this->assertNull($wrap->getRepresentation('tostring')); $this->assertInstanceOf(Representation::class, $wrap->getRepresentation('children')); $this->assertNull($wrap->getRepresentation('attributes')); @@ -213,6 +218,7 @@ public function testParse() $this->assertSame('wrap', $wrap2->name); $this->assertSame('$v->wrap->wrap', $wrap2->access_path); + $this->assertContains('omit_spl_id', $wrap2->hints); $this->assertNull($wrap2->getRepresentation('tostring')); $this->assertInstanceOf(Representation::class, $wrap2->getRepresentation('children')); $this->assertNull($wrap2->getRepresentation('attributes')); @@ -228,6 +234,7 @@ public function testParse() $this->assertSame('text', $text->name); $this->assertSame('(string) $v->wrap->wrap->text', $text->access_path); + $this->assertContains('omit_spl_id', $text->hints); $this->assertInstanceOf(Representation::class, $text->getRepresentation('tostring')); $this->assertNull($text->getRepresentation('children')); $this->assertNull($text->getRepresentation('attributes')); @@ -244,6 +251,7 @@ public function testParse() $this->assertSame('not-php-compatible', $incomp->name); $this->assertSame('$v->wrap->{\'not-php-compatible\'}', $incomp->access_path); + $this->assertContains('omit_spl_id', $incomp->hints); $this->assertNull($incomp->getRepresentation('tostring')); $this->assertNull($incomp->getRepresentation('children')); $this->assertInstanceOf(Representation::class, $incomp->getRepresentation('attributes')); @@ -263,6 +271,7 @@ public function testParse() $this->assertSame('value', $value->name); $this->assertSame('(string) $v->wrap->value', $value->access_path); + $this->assertContains('omit_spl_id', $value->hints); $this->assertInstanceOf(Representation::class, $value->getRepresentation('tostring')); $this->assertNull($value->getRepresentation('children')); $this->assertInstanceOf(Representation::class, $value->getRepresentation('attributes')); @@ -284,6 +293,7 @@ public function testParse() $this->assertSame('both', $both->name); $this->assertSame('(string) $v->both', $both->access_path); + $this->assertContains('omit_spl_id', $both->hints); $this->assertInstanceOf(Representation::class, $both->getRepresentation('tostring')); $this->assertNull($both->getRepresentation('children')); $this->assertInstanceOf(Representation::class, $both->getRepresentation('attributes')); diff --git a/tests/Parser/XmlPluginTest.php b/tests/Parser/XmlPluginTest.php index d90d8b5bc..f2c82c2ad 100644 --- a/tests/Parser/XmlPluginTest.php +++ b/tests/Parser/XmlPluginTest.php @@ -67,10 +67,13 @@ public function testParseSimpleXML() $o = $p->parse($v, clone $b); + $this->assertNotContains('omit_spl_id', $o->hints); + $r = $o->getRepresentation('xml'); $this->assertNotNull($r); $this->assertInstanceOf(InstanceValue::class, $r->contents); + $this->assertContains('omit_spl_id', $r->contents->hints); $this->assertSame('SimpleXMLElement', $r->contents->classname); $this->assertSame('x', $r->contents->name); $this->assertSame(2, $r->contents->size); @@ -108,10 +111,13 @@ public function testParseDOMDocument() $o = $p->parse($v, clone $b); + $this->assertNotContains('omit_spl_id', $o->hints); + $r = $o->getRepresentation('xml'); $this->assertNotNull($r); $this->assertInstanceOf(InstanceValue::class, $r->contents); + $this->assertContains('omit_spl_id', $r->contents->hints); $this->assertSame(DOMElement::class, $r->contents->classname); $this->assertSame('x', $r->contents->name); // Since PHP 8.1 there's a new schemaTypeInfo property that parses normally @@ -150,6 +156,8 @@ public function testParseXMLDocument() $o = $p->parse($v, clone $b); + $this->assertNotContains('omit_spl_id', $o->hints); + $r = $o->getRepresentation('xml'); if (!KINT_PHP84) { @@ -160,6 +168,7 @@ public function testParseXMLDocument() $this->assertNotNull($r); $this->assertInstanceOf(InstanceValue::class, $r->contents); + $this->assertContains('omit_spl_id', $r->contents->hints); $this->assertSame(Element::class, $r->contents->classname); $this->assertSame('x', $r->contents->name); $this->assertSame(0, $r->contents->size);