Skip to content

Commit

Permalink
Add spl_object_id
Browse files Browse the repository at this point in the history
Refactors:
* Renderer\Rich\SimpleXMLElementPlugin did nothing special
* CallablePlugin's closure has been delegated to ClosurePlugin,
  and the non-method callable behavior has been removed
  • Loading branch information
jnvsor committed Nov 10, 2022
1 parent 71a0f64 commit b5841ee
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 144 deletions.
Binary file modified build/kint.phar
Binary file not shown.
4 changes: 4 additions & 0 deletions src/Parser/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
21 changes: 18 additions & 3 deletions src/Renderer/Rich/AbstractPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
namespace Kint\Renderer\Rich;

use Kint\Renderer\RichRenderer;
use Kint\Zval\InstanceValue;
use Kint\Zval\Value;

/**
Expand All @@ -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 = '<dt class="kint-parent kint-locked">';
Expand All @@ -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 = '&amp;'.$s;
}

$header .= '<var>'.$s.'</var> ';
$header .= '<var>'.$s.'</var>';

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;
Expand Down
55 changes: 9 additions & 46 deletions src/Renderer/Rich/CallablePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 .= '<var>'.$s.'</var> ';
}

if (null !== ($s = $o->getName())) {
$header .= '<dfn>'.$this->renderer->escape($s).'('.$this->renderer->escape($o->getParams()).')</dfn>';
}

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 '<dl>'.$this->renderer->renderHeaderWrapper($o, (bool) \strlen($children), $header).$children.'</dl>';
return null;
}

protected function renderCallable(Value $o): string
protected function getClosurePlugin(): ClosurePlugin
{
$children = $this->renderer->renderChildren($o);

$header = '';

if (null !== ($s = $o->getModifiers())) {
$header .= '<var>'.$s.'</var> ';
}

if (null !== ($s = $o->getName())) {
$header .= '<dfn>'.$this->renderer->escape($s).'</dfn>';
}

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 '<dl>'.$this->renderer->renderHeaderWrapper($o, (bool) \strlen($children), $header).$children.'</dl>';
return $this->closure_plugin;
}

protected function renderMethod(MethodValue $o): string
Expand Down
29 changes: 16 additions & 13 deletions src/Renderer/Rich/ClosurePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 .= '<var>'.$s.'</var> ';
}
if (null !== ($s = $o->getModifiers())) {
$header .= '<var>'.$s.'</var> ';
}

if (null !== ($s = $o->getName())) {
$header .= '<dfn>'.$this->renderer->escape($s).'('.$this->renderer->escape($o->getParams()).')</dfn> ';
}
if (null !== ($s = $o->getName())) {
$header .= '<dfn>'.$this->renderer->escape($s).'('.$this->renderer->escape($o->getParams()).')</dfn> ';
}

$header .= '<var>Closure</var> ';
$header .= $this->renderer->escape(Kint::shortenPath($o->filename)).':'.(int) $o->startline;
$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);

Expand Down
79 changes: 0 additions & 79 deletions src/Renderer/Rich/SimpleXMLElementPlugin.php

This file was deleted.

9 changes: 7 additions & 2 deletions src/Renderer/RichRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];

Expand Down Expand Up @@ -312,7 +311,13 @@ public function renderHeader(Value $o): string
$s = '&amp;'.$s;
}

$output .= '<var>'.$s.'</var> ';
$output .= '<var>'.$s.'</var>';

if ($o instanceof InstanceValue && isset($o->spl_object_id)) {
$output .= '#'.((int) $o->spl_object_id);
}

$output .= ' ';
}

if (null !== ($s = $o->getSize())) {
Expand Down
8 changes: 7 additions & 1 deletion src/Renderer/TextRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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())) {
Expand Down
2 changes: 2 additions & 0 deletions src/Zval/InstanceValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand All @@ -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;
}
Expand Down
10 changes: 10 additions & 0 deletions tests/Parser/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit b5841ee

Please sign in to comment.