Skip to content

Commit

Permalink
Add trap()->stackTrace() method. Now trap() doesn't print stack t…
Browse files Browse the repository at this point in the history
…race
  • Loading branch information
roxblnfk committed Jan 12, 2024
1 parent 7f05055 commit c7a7dee
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# BSD-3 License

Copyright (c) 2023, Buggregator. All rights reserved.
Copyright (c) 2023–2024, Buggregator. All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
following conditions are met:
Expand Down
26 changes: 15 additions & 11 deletions src/Client/TrapHandle.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@ public function if(bool|callable $condition): self
return $this;
}

/**
* Add stack trace to the dump.
*/
public function stackTrace(): self
{
$cwd = \getcwd();
$this->values['Stack trace'] = [
'cwd' => $cwd,
'trace' => new TraceStub($this->staticState->stackTrace),
];

return $this;
}

/**
* Set max depth for the dump.
*
Expand Down Expand Up @@ -102,16 +116,6 @@ private function sendDump(): void
$_SERVER['VAR_DUMPER_SERVER'] = '127.0.0.1:9912';
}

// If there are no values - stack trace
if ($this->values === []) {
VarDumper::dump([
'cwd' => \getcwd(),
// todo StackTrace::stackTrace(\getcwd()) - add CWD
'trace' => new TraceStub($this->staticState->stackTrace),
], depth: $this->depth);
return;
}

// Dump single value
if (\array_keys($this->values) === [0]) {
VarDumper::dump($this->values[0], depth: $this->depth);
Expand Down Expand Up @@ -140,7 +144,7 @@ private function __construct(

private function haveToSend(): bool
{
if (!$this->haveToSend) {
if (!$this->haveToSend || $this->values === []) {
return false;
}

Expand Down
12 changes: 11 additions & 1 deletion tests/Unit/Client/TrapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function testLabel(): void
*/
public function testStackTrace(): void
{
$line = __FILE__ . ':' . __LINE__ and trap();
$line = __FILE__ . ':' . __LINE__ and trap()->stackTrace();

$this->assertArrayHasKey('trace', static::$lastData->getValue());

Expand All @@ -26,6 +26,16 @@ public function testStackTrace(): void
$this->assertStringContainsString($line, $neededLine);
}

/**
* Nothing is dumped if no arguments are passed to {@see trap()}.
*/
public function testEmptyTrapCall(): void
{
trap();

self::assertNull(self::$lastData);
}

/**
* After calling {@see trap()} the dumped data isn't stored in the memory.
*/
Expand Down

0 comments on commit c7a7dee

Please sign in to comment.