Skip to content

Commit 7aa3b5d

Browse files
Refactor
1 parent 16d1b86 commit 7aa3b5d

File tree

6 files changed

+33
-26
lines changed

6 files changed

+33
-26
lines changed

src/Event/Value/ThrowableBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static function from(\Throwable $t): Throwable
3737
$t::class,
3838
$t->getMessage(),
3939
ThrowableToStringMapper::map($t),
40-
Filter::getFilteredStacktrace($t, false),
40+
Filter::stackTraceFromThrowableAsString($t, false),
4141
$previous,
4242
);
4343
}

src/Framework/Constraint/Exception/Exception.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ protected function failureDescription(mixed $other): string
6868

6969
if ($other instanceof Throwable) {
7070
$message = '. Message was: "' . $other->getMessage() . '" at'
71-
. "\n" . Filter::getFilteredStacktrace($other);
71+
. "\n" . Filter::stackTraceFromThrowableAsString($other);
7272
}
7373

7474
return sprintf(

src/Framework/TestSuite.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -589,15 +589,15 @@ private function throwableToString(Throwable $t): string
589589
return sprintf(
590590
"%s\n%s",
591591
$message,
592-
Filter::getFilteredStacktrace($t),
592+
Filter::stackTraceFromThrowableAsString($t),
593593
);
594594
}
595595

596596
return sprintf(
597597
"%s: %s\n%s",
598598
$t::class,
599599
$message,
600-
Filter::getFilteredStacktrace($t),
600+
Filter::stackTraceFromThrowableAsString($t),
601601
);
602602
}
603603

src/Util/Filter.php

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,49 +30,56 @@
3030
/**
3131
* @throws Exception
3232
*/
33-
public static function getFilteredStacktrace(Throwable $t, bool $unwrap = true): string
33+
public static function stackTraceFromThrowableAsString(Throwable $t, bool $unwrap = true): string
3434
{
35-
$filteredStacktrace = '';
36-
3735
if ($t instanceof PhptAssertionFailedError) {
38-
$eTrace = $t->syntheticTrace();
39-
$eFile = $t->syntheticFile();
40-
$eLine = $t->syntheticLine();
36+
$stackTrace = $t->syntheticTrace();
37+
$file = $t->syntheticFile();
38+
$line = $t->syntheticLine();
4139
} elseif ($t instanceof Exception) {
42-
$eTrace = $t->getSerializableTrace();
43-
$eFile = $t->getFile();
44-
$eLine = $t->getLine();
40+
$stackTrace = $t->getSerializableTrace();
41+
$file = $t->getFile();
42+
$line = $t->getLine();
4543
} else {
4644
if ($unwrap && $t->getPrevious()) {
4745
$t = $t->getPrevious();
4846
}
4947

50-
$eTrace = $t->getTrace();
51-
$eFile = $t->getFile();
52-
$eLine = $t->getLine();
48+
$stackTrace = $t->getTrace();
49+
$file = $t->getFile();
50+
$line = $t->getLine();
5351
}
5452

55-
if (!self::frameExists($eTrace, $eFile, $eLine)) {
53+
if (!self::frameExists($stackTrace, $file, $line)) {
5654
array_unshift(
57-
$eTrace,
58-
['file' => $eFile, 'line' => $eLine],
55+
$stackTrace,
56+
['file' => $file, 'line' => $line],
5957
);
6058
}
6159

60+
return self::stackTraceAsString($stackTrace);
61+
}
62+
63+
/**
64+
* @param list<array{file: string, line: ?int, class?: class-string, function?: string, type: string}> $frames
65+
*/
66+
public static function stackTraceAsString(array $frames): string
67+
{
68+
$buffer = '';
6269
$prefix = defined('__PHPUNIT_PHAR_ROOT__') ? __PHPUNIT_PHAR_ROOT__ : false;
6370
$excludeList = new ExcludeList;
6471

65-
foreach ($eTrace as $frame) {
72+
foreach ($frames as $frame) {
6673
if (self::shouldPrintFrame($frame, $prefix, $excludeList)) {
67-
$filteredStacktrace .= sprintf(
74+
$buffer .= sprintf(
6875
"%s:%s\n",
6976
$frame['file'],
7077
$frame['line'] ?? '?',
7178
);
7279
}
7380
}
7481

75-
return $filteredStacktrace;
82+
return $buffer;
7683
}
7784

7885
/**

tests/unit/Event/Value/ThrowableTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function testCanBeCreatedForThrowableWithoutPrevious(): void
2929
$this->assertSame(Exception::class, $t->className());
3030
$this->assertSame('message', $t->message());
3131
$this->assertSame("Exception: message\n", $t->description());
32-
$this->assertSame(Filter::getFilteredStacktrace($e), $t->stackTrace());
32+
$this->assertSame(Filter::stackTraceFromThrowableAsString($e), $t->stackTrace());
3333
$this->assertFalse($t->hasPrevious());
3434

3535
$this->expectException(NoPreviousThrowableException::class);
@@ -46,15 +46,15 @@ public function testCanBeCreatedForThrowableWithPrevious(): void
4646
$this->assertSame(Exception::class, $t->className());
4747
$this->assertSame('second message', $t->message());
4848
$this->assertSame("Exception: second message\n", $t->description());
49-
$this->assertSame(Filter::getFilteredStacktrace($second, false), $t->stackTrace());
49+
$this->assertSame(Filter::stackTraceFromThrowableAsString($second, false), $t->stackTrace());
5050
$this->assertTrue($t->hasPrevious());
5151

5252
$previous = $t->previous();
5353

5454
$this->assertSame(Exception::class, $previous->className());
5555
$this->assertSame('first message', $previous->message());
5656
$this->assertSame("Exception: first message\n", $previous->description());
57-
$this->assertSame(Filter::getFilteredStacktrace($first), $previous->stackTrace());
57+
$this->assertSame(Filter::stackTraceFromThrowableAsString($first), $previous->stackTrace());
5858

5959
$this->assertStringMatchesFormat(
6060
<<<'EOD'

tests/unit/Util/FilterTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ public function testUnwrapThrowableUsesPreviousValues(): void
2323
$first = new Exception('first', 123, null);
2424
$second = new Exception('second', 345, $first);
2525

26-
$this->assertSame(Filter::getFilteredStacktrace($second), Filter::getFilteredStacktrace($first));
26+
$this->assertSame(Filter::stackTraceFromThrowableAsString($second), Filter::stackTraceFromThrowableAsString($first));
2727
}
2828
}

0 commit comments

Comments
 (0)