Skip to content

Commit

Permalink
ProfilePlugin: Handle class recursion more gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
jnvsor committed Sep 15, 2024
1 parent 1bfeb7d commit 095ce54
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
Binary file modified build/kint.phar
Binary file not shown.
33 changes: 31 additions & 2 deletions src/Parser/ProfilePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class ProfilePlugin extends AbstractPlugin
protected array $instance_complexity = [];
protected array $instance_count_stack = [];
protected array $class_complexity = [];
protected array $class_count_stack = [];

public function getTypes(): array
{
Expand All @@ -59,13 +60,27 @@ public function parse(&$var, Value &$o, int $trigger): void
$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];
}

Expand All @@ -74,6 +89,16 @@ public function parse(&$var, Value &$o, int $trigger): void

if ($o instanceof InstanceValue) {
--$this->instance_count_stack[$o->spl_object_hash];

if (0 === $this->instance_count_stack[$o->spl_object_hash]) {
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
Expand Down Expand Up @@ -122,12 +147,16 @@ public function parse(&$var, Value &$o, int $trigger): void

foreach (\class_parents($var) as $class) {
$this->class_complexity[$class] ??= 0;
$this->class_complexity[$class] += $sub_complexity;
if (0 === $this->class_count_stack[$class]) {
$this->class_complexity[$class] += $sub_complexity;
}
}

foreach (\class_implements($var) as $iface) {
$this->class_complexity[$iface] ??= 0;
$this->class_complexity[$iface] += $sub_complexity;
if (0 === $this->class_count_stack[$iface]) {
$this->class_complexity[$iface] += $sub_complexity;
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Renderer/Rich/ProfilePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function renderTab(Representation $r): ?string

$out .= 'Complexity: '.$r->complexity.PHP_EOL;
if (isset($r->instance_counts)) {
$out .= 'Instances: '.\var_export($r->instance_counts, true).PHP_EOL;
$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;
Expand Down

0 comments on commit 095ce54

Please sign in to comment.