From b5841ee834121b5f3399184466b8ac43956885fa Mon Sep 17 00:00:00 2001 From: Jonathan Vollebregt Date: Thu, 10 Nov 2022 12:41:54 +0100 Subject: [PATCH] Add spl_object_id Refactors: * Renderer\Rich\SimpleXMLElementPlugin did nothing special * CallablePlugin's closure has been delegated to ClosurePlugin, and the non-method callable behavior has been removed --- build/kint.phar | Bin 434693 -> 431578 bytes src/Parser/Parser.php | 4 + src/Renderer/Rich/AbstractPlugin.php | 21 ++++- src/Renderer/Rich/CallablePlugin.php | 55 +++---------- src/Renderer/Rich/ClosurePlugin.php | 29 ++++--- src/Renderer/Rich/SimpleXMLElementPlugin.php | 79 ------------------- src/Renderer/RichRenderer.php | 9 ++- src/Renderer/TextRenderer.php | 8 +- src/Zval/InstanceValue.php | 2 + tests/Parser/ParserTest.php | 10 +++ 10 files changed, 73 insertions(+), 144 deletions(-) delete mode 100644 src/Renderer/Rich/SimpleXMLElementPlugin.php diff --git a/build/kint.phar b/build/kint.phar index fd8b89e6772fd7d0b372516ce43dcba192594b31..ad12ad5fcb7e3789d804193c3764c4ce48f0e5ce 100644 GIT binary patch delta 1438 zcma)6ZA@EL81B92oR)q-?=6&mKHLfNZDyH6}P;@0r@@1?KoH`J9=+v0W&P-cnJfVB~ zVC;<153(@bmwyN=0Fr;RcOE`4}4#+q&r;)AR}#T&AZPz+m_ zc9HS)*N@i5GmSfFW|J#zmGMwV<(_!dR6sH6m2fha9WNZ+lQ2gUW=c$$X?1_>nP1f~ z_&LqO;xMg-;4rN<`@GynZd)YeZwzkV!M6yFKCjF9d~+D;?X)$T9HzzOFk&*s=DPLOP^ zyzo@Bzn$OG77PpAigyIccGET(a$u$S-6*O>;4x7cn$9CVO!VOrNL@o3n3xOdsA1T`8mjW*(`b&xLg`JB@O|ZXq_4FB0Jemor#ewW=)G^*5ah+$E&RD=I8< zyIj72P|V%#22DxI4xK5KA;-q>-cLve-wzT!biJrlgL?{P!Y6$=@9r6T251c|?xZoe zW$wCHu^pIWNGm=rB4eI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UZ0$sU9~x$m!)*Ct*LurZSQvuS~%=EAX1gj^-T) zw_+U=emSFFhTT)J@$Zf9IM&6JaP~ip#MKi!riVd~eNj?*hoz8N%KmS%?nd`8IPfdQ zki8RNkYAaE1M?#nqbCJRWp6S0xylg7@=Fan5UD&@hv4*er v84J>Hp8MtO_^t7-Q^z*XG!EqV?CRfkt<=BGuZ~@SO~Ru@rBbb{-cbD)SmdrC diff --git a/src/Parser/Parser.php b/src/Parser/Parser.php index 2d971bc37..5c7a40a3a 100644 --- a/src/Parser/Parser.php +++ b/src/Parser/Parser.php @@ -407,6 +407,10 @@ private function parseObject(&$var, Value $o): Value $object->spl_object_hash = $hash; $object->size = \count($values); + if (KINT_PHP72) { + $object->spl_object_id = \spl_object_id($var); + } + if (isset($this->object_hashes[$hash])) { $object->hints[] = 'recursion'; diff --git a/src/Renderer/Rich/AbstractPlugin.php b/src/Renderer/Rich/AbstractPlugin.php index e8b9fe51a..4d9ec7067 100644 --- a/src/Renderer/Rich/AbstractPlugin.php +++ b/src/Renderer/Rich/AbstractPlugin.php @@ -28,6 +28,7 @@ namespace Kint\Renderer\Rich; use Kint\Renderer\RichRenderer; +use Kint\Zval\InstanceValue; use Kint\Zval\Value; /** @@ -42,6 +43,9 @@ public function __construct(RichRenderer $r) $this->renderer = $r; } + /** + * @param string $content The replacement for the getValueShort contents + */ public function renderLockedHeader(Value $o, string $content): string { $header = '
'; @@ -65,17 +69,28 @@ public function renderLockedHeader(Value $o, string $content): string } if (null !== ($s = $o->getType())) { - $s = $this->renderer->escape($s); + if (RichRenderer::$escape_types) { + $s = $this->renderer->escape($s); + } if ($o->reference) { $s = '&'.$s; } - $header .= ''.$s.' '; + $header .= ''.$s.''; + + if ($o instanceof InstanceValue && isset($o->spl_object_id)) { + $header .= '#'.((int) $o->spl_object_id); + } + + $header .= ' '; } if (null !== ($s = $o->getSize())) { - $header .= '('.$this->renderer->escape($s).') '; + if (RichRenderer::$escape_types) { + $s = $this->renderer->escape($s); + } + $header .= '('.$s.') '; } $header .= $content; diff --git a/src/Renderer/Rich/CallablePlugin.php b/src/Renderer/Rich/CallablePlugin.php index 4caee4d6a..043c2110d 100644 --- a/src/Renderer/Rich/CallablePlugin.php +++ b/src/Renderer/Rich/CallablePlugin.php @@ -37,65 +37,28 @@ class CallablePlugin extends AbstractPlugin implements ValuePluginInterface { protected static $method_cache = []; - public function renderValue(Value $o): string + protected $closure_plugin = null; + + public function renderValue(Value $o): ?string { if ($o instanceof MethodValue) { return $this->renderMethod($o); } if ($o instanceof ClosureValue) { - return $this->renderClosure($o); - } - - return $this->renderCallable($o); - } - - protected function renderClosure(ClosureValue $o): string - { - $children = $this->renderer->renderChildren($o); - - $header = ''; - - if (null !== ($s = $o->getModifiers())) { - $header .= ''.$s.' '; - } - - if (null !== ($s = $o->getName())) { - $header .= ''.$this->renderer->escape($s).'('.$this->renderer->escape($o->getParams()).')'; - } - - if (null !== ($s = $o->getValueShort())) { - if (RichRenderer::$strlen_max) { - $s = Utils::truncateString($s, RichRenderer::$strlen_max); - } - $header .= ' '.$this->renderer->escape($s); + return $this->getClosurePlugin()->renderValue($o); } - return '
'.$this->renderer->renderHeaderWrapper($o, (bool) \strlen($children), $header).$children.'
'; + return null; } - protected function renderCallable(Value $o): string + protected function getClosurePlugin(): ClosurePlugin { - $children = $this->renderer->renderChildren($o); - - $header = ''; - - if (null !== ($s = $o->getModifiers())) { - $header .= ''.$s.' '; - } - - if (null !== ($s = $o->getName())) { - $header .= ''.$this->renderer->escape($s).''; - } - - if (null !== ($s = $o->getValueShort())) { - if (RichRenderer::$strlen_max) { - $s = Utils::truncateString($s, RichRenderer::$strlen_max); - } - $header .= ' '.$this->renderer->escape($s); + if (null === $this->closure_plugin) { + $this->closure_plugin = new ClosurePlugin($this->renderer); } - return '
'.$this->renderer->renderHeaderWrapper($o, (bool) \strlen($children), $header).$children.'
'; + return $this->closure_plugin; } protected function renderMethod(MethodValue $o): string diff --git a/src/Renderer/Rich/ClosurePlugin.php b/src/Renderer/Rich/ClosurePlugin.php index cea043743..a2b5a7e06 100644 --- a/src/Renderer/Rich/ClosurePlugin.php +++ b/src/Renderer/Rich/ClosurePlugin.php @@ -33,26 +33,29 @@ class ClosurePlugin extends AbstractPlugin implements ValuePluginInterface { - public function renderValue(Value $o): string + public function renderValue(Value $o): ?string { + if (!$o instanceof ClosureValue) { + return null; + } + $children = $this->renderer->renderChildren($o); - if (!($o instanceof ClosureValue)) { - $header = $this->renderer->renderHeader($o); - } else { - $header = ''; + $header = ''; - if (null !== ($s = $o->getModifiers())) { - $header .= ''.$s.' '; - } + if (null !== ($s = $o->getModifiers())) { + $header .= ''.$s.' '; + } - if (null !== ($s = $o->getName())) { - $header .= ''.$this->renderer->escape($s).'('.$this->renderer->escape($o->getParams()).') '; - } + if (null !== ($s = $o->getName())) { + $header .= ''.$this->renderer->escape($s).'('.$this->renderer->escape($o->getParams()).') '; + } - $header .= 'Closure '; - $header .= $this->renderer->escape(Kint::shortenPath($o->filename)).':'.(int) $o->startline; + $header .= 'Closure'; + 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); diff --git a/src/Renderer/Rich/SimpleXMLElementPlugin.php b/src/Renderer/Rich/SimpleXMLElementPlugin.php deleted file mode 100644 index ae29999aa..000000000 --- a/src/Renderer/Rich/SimpleXMLElementPlugin.php +++ /dev/null @@ -1,79 +0,0 @@ -renderer->renderChildren($o); - - $header = ''; - - if (null !== ($s = $o->getModifiers())) { - $header .= ''.$s.' '; - } - - if (null !== ($s = $o->getName())) { - $header .= ''.$this->renderer->escape($s).' '; - - if ($s = $o->getOperator()) { - $header .= $this->renderer->escape($s, 'ASCII').' '; - } - } - - if (null !== ($s = $o->getType())) { - $s = $this->renderer->escape($s); - - if ($o->reference) { - $s = '&'.$s; - } - - $header .= ''.$this->renderer->escape($s).' '; - } - - if (null !== ($s = $o->getSize())) { - $header .= '('.$this->renderer->escape($s).') '; - } - - if (null !== ($s = $o->getValueShort())) { - if (RichRenderer::$strlen_max) { - $s = Utils::truncateString($s, RichRenderer::$strlen_max); - } - $header .= $this->renderer->escape($s); - } - - $header = $this->renderer->renderHeaderWrapper($o, (bool) \strlen($children), $header); - - return '
'.$header.$children.'
'; - } -} diff --git a/src/Renderer/RichRenderer.php b/src/Renderer/RichRenderer.php index fb29e276e..4ef045f46 100644 --- a/src/Renderer/RichRenderer.php +++ b/src/Renderer/RichRenderer.php @@ -56,7 +56,6 @@ class RichRenderer extends AbstractRenderer 'color' => Rich\ColorPlugin::class, 'depth_limit' => Rich\DepthLimitPlugin::class, 'recursion' => Rich\RecursionPlugin::class, - 'simplexml_element' => Rich\SimpleXMLElementPlugin::class, 'trace_frame' => Rich\TraceFramePlugin::class, ]; @@ -312,7 +311,13 @@ public function renderHeader(Value $o): string $s = '&'.$s; } - $output .= ''.$s.' '; + $output .= ''.$s.''; + + if ($o instanceof InstanceValue && isset($o->spl_object_id)) { + $output .= '#'.((int) $o->spl_object_id); + } + + $output .= ' '; } if (null !== ($s = $o->getSize())) { diff --git a/src/Renderer/TextRenderer.php b/src/Renderer/TextRenderer.php index 58f327ca1..833320a6c 100644 --- a/src/Renderer/TextRenderer.php +++ b/src/Renderer/TextRenderer.php @@ -201,7 +201,13 @@ public function renderHeader(Value $o): string $s = '&'.$s; } - $output[] = $this->colorType($this->escape($s)); + $s = $this->colorType($this->escape($s)); + + if ($o instanceof InstanceValue && isset($o->spl_object_id)) { + $s .= '#'.((int) $o->spl_object_id); + } + + $output[] = $s; } if (null !== ($s = $o->getSize())) { diff --git a/src/Zval/InstanceValue.php b/src/Zval/InstanceValue.php index a65b33b54..323733474 100644 --- a/src/Zval/InstanceValue.php +++ b/src/Zval/InstanceValue.php @@ -32,6 +32,7 @@ class InstanceValue extends Value public $type = 'object'; public $classname; public $spl_object_hash; + public $spl_object_id = null; public $filename; public $startline; public $hints = ['object']; @@ -48,6 +49,7 @@ public function transplant(Value $old): void 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; } diff --git a/tests/Parser/ParserTest.php b/tests/Parser/ParserTest.php index f2c06d883..4970ff4dc 100644 --- a/tests/Parser/ParserTest.php +++ b/tests/Parser/ParserTest.php @@ -354,6 +354,11 @@ public function testParseObject() $this->assertSame(ChildTestClass::class, $o->classname); $this->assertSame(\spl_object_hash($v), $o->spl_object_hash); $this->assertContains('object', $o->hints); + if (KINT_PHP72) { + $this->assertSame(\spl_object_id($v), $o->spl_object_id); + } else { + $this->assertNull($o->spl_object_id); + } $val = \array_values($o->value->contents); @@ -475,6 +480,11 @@ public function testParseObjectIncomplete() $this->assertSame(\spl_object_hash($v), $o->spl_object_hash); $this->assertContains('object', $o->hints); $this->assertNotNull($o->access_path); + if (KINT_PHP72) { + $this->assertSame(\spl_object_id($v), $o->spl_object_id); + } else { + $this->assertNull($o->spl_object_id); + } $val = \array_values($o->value->contents);