Skip to content

Commit

Permalink
Parser::noRecurseCall: Simplify logic
Browse files Browse the repository at this point in the history
  • Loading branch information
jnvsor committed Dec 30, 2024
1 parent 9fa1184 commit 9d3a86b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
Binary file modified build/kint.phar
Binary file not shown.
26 changes: 17 additions & 9 deletions src/Parser/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,12 @@ public function parse(&$var, ContextInterface $c): AbstractValue

public function addPlugin(PluginInterface $p): void
{
try {
$this->noRecurseCall();
} catch (DomainException $e) { // @codeCoverageIgnore
\trigger_error('Calling Kint\\Parser::addPlugin from inside a parse is deprecated', E_USER_DEPRECATED); // @codeCoverageIgnore
}

if (!$types = $p->getTypes()) {
return;
}
Expand Down Expand Up @@ -209,24 +215,26 @@ public function addPlugin(PluginInterface $p): void

public function clearPlugins(): void
{
try {
$this->noRecurseCall();
} catch (DomainException $e) { // @codeCoverageIgnore
\trigger_error('Calling Kint\\Parser::clearPlugins from inside a parse is deprecated', E_USER_DEPRECATED); // @codeCoverageIgnore
}

$this->plugins = [];
}

protected function noRecurseCall(): void
{
$bt = \debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT | DEBUG_BACKTRACE_IGNORE_ARGS);

$caller_frame = [
'function' => __FUNCTION__,
];

while (isset($bt[0]['object']) && $bt[0]['object'] === $this) {
$caller_frame = \array_shift($bt);
}
\reset($bt);
/** @psalm-var class-string $caller_frame['class'] */
$caller_frame = \next($bt);

foreach ($bt as $frame) {
if (isset($frame['object']) && $frame['object'] === $this) {
throw new DomainException(__CLASS__.'::'.$caller_frame['function'].' cannot be called from inside a parse');
if (isset($frame['object']) && $frame['object'] === $this && 'parse' === $frame['function']) {
throw new DomainException($caller_frame['class'].'::'.$caller_frame['function'].' cannot be called from inside a parse');
}
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/Parser/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ function (&$var, $v) use ($p, &$success) {
try {
$p->setDepthLimit(43);
} catch (DomainException $e) {
$this->assertStringStartsWith(Parser::class.'::setDepthLimit ', $e->getMessage());
$success = true;
}

Expand Down

0 comments on commit 9d3a86b

Please sign in to comment.