Skip to content

Commit

Permalink
Merge pull request #26 from jackbayliss/feature/trim-paths
Browse files Browse the repository at this point in the history
trimFilePaths feature
  • Loading branch information
freekmurze authored Nov 18, 2024
2 parents 8c5981b + 8fe7951 commit c5ca87a
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ frame is an application frame, or a vendor frame. Here's an example using a Lara
```php
$backtrace = Spatie\Backtrace\Backtrace::create()->applicationPath(base_path());
```
### Removing the application path from the file name

You can use `trimFilePaths` to remove the base path of your app from the file. This will only work if you use it in conjunction the `applicationPath` method re above. Here's an example using a Laravel specific function. This will ensure the Frame has the trimmedFilePath property set.

```php
$backtrace = Backtrace::create()->applicationPath(base_path())->trimFilePaths());
```

### Getting a certain part of a trace

Expand Down
16 changes: 15 additions & 1 deletion src/Backtrace.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ class Backtrace
/** @var bool */
protected $withObject = false;

/** @var bool */
protected $trimFilePaths = false;

/** @var string|null */
protected $applicationPath;

Expand Down Expand Up @@ -91,6 +94,13 @@ public function applicationPath(string $applicationPath): self
return $this;
}

public function trimFilePaths(): self
{
$this->trimFilePaths = true;

return $this;
}

public function offset(int $offset): self
{
$this->offset = $offset;
Expand Down Expand Up @@ -183,14 +193,18 @@ class_exists(ClosureStream::class)
$currentLine -= 1;
}

if ($this->trimFilePaths && $this->applicationPath) {
$trimmedFilePath = str_replace($this->applicationPath, '', $currentFile);
}
$frame = new Frame(
$currentFile,
$currentLine,
$arguments,
$rawFrame['function'] ?? null,
$rawFrame['class'] ?? null,
$this->isApplicationFrame($currentFile),
$textSnippet
$textSnippet,
$trimmedFilePath ?? null,
);

$frames[] = $frame;
Expand Down
8 changes: 7 additions & 1 deletion src/Frame.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ class Frame
/** @var string */
public $file;

/** @var string|null */
public $trimmedFilePath;

/** @var int */
public $lineNumber;

Expand All @@ -38,10 +41,13 @@ public function __construct(
string $method = null,
string $class = null,
bool $isApplicationFrame = false,
?string $textSnippet = null
?string $textSnippet = null,
?string $trimmedFilePath = null
) {
$this->file = $file;

$this->trimmedFilePath = $trimmedFilePath;

$this->lineNumber = $lineNumber;

$this->arguments = $arguments;
Expand Down
16 changes: 16 additions & 0 deletions tests/BacktraceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use DateTime;
use PHPUnit\Framework\TestSuite;
use ReflectionClass;
use Spatie\Backtrace\Arguments\ArgumentReducers;
use Spatie\Backtrace\Backtrace;
use Spatie\Backtrace\Frame;
Expand Down Expand Up @@ -300,4 +301,19 @@ public function it_can_get_the_index_of_the_first_application_frame()
{
$this->assertEquals(0, Backtrace::create()->firstApplicationFrameIndex());
}

/** @test */
public function it_trims_file_path_if_application_path_set()
{
$className = (new ReflectionClass(self::class))->getShortName();

$this->assertEquals(DIRECTORY_SEPARATOR.$className.'.php', Backtrace::create()->applicationPath(__DIR__)->trimFilePaths()->frames()[0]->trimmedFilePath);
}

/** @test */
public function it_does_not_trim_file_path_if_no_application_path_set()
{
$this->assertNull(Backtrace::create()->trimFilePaths()->frames()[0]->trimmedFilePath);
}

}

0 comments on commit c5ca87a

Please sign in to comment.