Skip to content

Commit

Permalink
Add trap()->context() method with context mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed May 4, 2024
1 parent b6ad654 commit 5e78bbf
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 4 deletions.
29 changes: 29 additions & 0 deletions src/Client/TrapHandle.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,35 @@ public function return(int|string $key = 0): mixed
return $this->values[$k];
}

/**
* Add dynamic context to the dumped data.
* The method merges new values with the existing ones using the {@see \array_merge()} function.
*
* There are two ways to add context:
*
* 1. Use named arguments:
* ```php
* trap($phpCode)->context(language: 'php');
* ```
*
* 2. Use array:
* ```php
* trap()->context(['foo bar', => 42, 'baz' => 69]);
* ```
*
* @param mixed ...$values
*/
public function context(mixed ...$values): self
{
if (\array_keys($values) === [0] && \is_array($values[0])) {
$this->staticState->dataContext = \array_merge($this->staticState->dataContext, $values[0]);
return $this;
}

$this->staticState->dataContext = \array_merge($this->staticState->dataContext, $values);
return $this;
}

public function __destruct()
{
$this->haveToSend() and $this->sendDump();
Expand Down
6 changes: 4 additions & 2 deletions src/Client/TrapHandle/Dumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Debug\FileLinkFormatter;
use Symfony\Component\VarDumper\Caster\ReflectionCaster;
use Symfony\Component\VarDumper\Cloner\DumperInterface;
use Symfony\Component\VarDumper\Cloner\VarCloner;
use Symfony\Component\VarDumper\Dumper\CliDumper;
use Symfony\Component\VarDumper\Dumper\ContextProvider\CliContextProvider;
Expand Down Expand Up @@ -62,7 +61,10 @@ public static function setDumper(?DataDumperInterface $dumper = null): Closure
use ($cloner, $dumper): ?string {
$var = $cloner->cloneVar($var);

$label === null or $var = $var->withContext(['label' => $label]);
$context = StaticState::getValue()->dataContext;

Check failure on line 64 in src/Client/TrapHandle/Dumper.php

View workflow job for this annotation

GitHub Actions / Psalm Validation (PHP 8.2, OS ubuntu-latest)

PossiblyNullPropertyFetch

src/Client/TrapHandle/Dumper.php:64:28: PossiblyNullPropertyFetch: Cannot get property on possibly null variable of type Buggregator\Trap\Client\TrapHandle\StaticState|null (see https://psalm.dev/082)

$label === null or $context['label'] ??= $label;

Check failure on line 66 in src/Client/TrapHandle/Dumper.php

View workflow job for this annotation

GitHub Actions / Psalm Validation (PHP 8.2, OS ubuntu-latest)

MixedAssignment

src/Client/TrapHandle/Dumper.php:66:36: MixedAssignment: Unable to determine the type of this assignment (see https://psalm.dev/032)
$label === [] or $var = $var->withContext($context);

Check failure on line 67 in src/Client/TrapHandle/Dumper.php

View workflow job for this annotation

GitHub Actions / Psalm Validation (PHP 8.2, OS ubuntu-latest)

TypeDoesNotContainType

src/Client/TrapHandle/Dumper.php:67:17: TypeDoesNotContainType: null|string does not contain empty array (see https://psalm.dev/056)

Check failure on line 67 in src/Client/TrapHandle/Dumper.php

View workflow job for this annotation

GitHub Actions / Psalm Validation (PHP 8.2, OS ubuntu-latest)

PossiblyNullArgument

src/Client/TrapHandle/Dumper.php:67:59: PossiblyNullArgument: Argument 1 of Symfony\Component\VarDumper\Cloner\Data::withContext cannot be null, possibly null value provided (see https://psalm.dev/078)
$depth > 0 and $var = $var->withMaxDepth($depth);

return $dumper->dump($var);
Expand Down
2 changes: 2 additions & 0 deletions src/Client/TrapHandle/StaticState.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*/
final class StaticState
{
public array $dataContext = [];

/**
* @param SimpleStackTrace $stackTrace Simple stack trace without arguments and objects.
* @param StackTraceWithObjects $stackTraceWithObjects Stack trace without arguments but with objects.
Expand Down
26 changes: 24 additions & 2 deletions tests/Unit/Client/TrapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

namespace Buggregator\Trap\Tests\Unit\Client;

use Buggregator\Trap\Client\TrapHandle\Counter;
use Buggregator\Trap\Client\TrapHandle\Dumper;
use Symfony\Component\VarDumper\Cloner\Data;
use Symfony\Component\VarDumper\Dumper\DataDumperInterface;

final class TrapTest extends Base
Expand All @@ -17,6 +15,30 @@ public function testLabel(): void
$this->assertSame('FooName', static::$lastData->getContext()['label']);
}

public function testSimpleContext(): void
{
trap('test-value')->context(foo: 'test-context');

self::assertSame(['foo' => 'test-context'], static::$lastData->getContext());
}

public function testArrayContext(): void
{
trap('test-value')->context(['foo' => 'test-context']);

self::assertSame(['foo' => 'test-context'], static::$lastData->getContext());
}

public function testContextMultiple(): void
{
trap('test-value')
->context(['foo' => 'test-context'])
->context(['bar' => 'bar-context'])
->context(foo: 'new');

self::assertSame(['foo' => 'new', 'bar' => 'bar-context'], static::$lastData->getContext());
}

/**
* Check the first line of dumped stacktrace string contains right file and line.
*/
Expand Down

0 comments on commit 5e78bbf

Please sign in to comment.