Skip to content

Commit 07083b2

Browse files
committed
refactor(support): Improve exception handling with line number
- Modify `getMarked` and `get` methods to accept lineNumber parameter. - Update `getFile` method to slice code based on specified line number for more flexible debugging. - Remove unnecessary comments and code to enhance clarity. - These changes improve the readability and maintainability of exception context management.
1 parent 75aee9d commit 07083b2

File tree

6 files changed

+18
-27
lines changed

6 files changed

+18
-27
lines changed

src/Collectors/ExceptionContextCollector.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,17 @@
1717

1818
class ExceptionContextCollector extends AbstractExceptionCollector
1919
{
20+
public function __construct(
21+
private string $mark = '',
22+
private int $lineNumber = 5
23+
) {}
24+
2025
public function collect(): array
2126
{
22-
return ExceptionContext::getMarked($this->exception);
27+
return ExceptionContext::getMarked(
28+
$this->exception,
29+
$this->mark,
30+
$this->lineNumber
31+
);
2332
}
2433
}

src/Pipes/AddChorePipe.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ class AddChorePipe
2525
use WithPipeArgs;
2626

2727
/**
28-
* @noinspection MissingParameterTypeDeclarationInspection
29-
*
3028
* @param array-key $key
3129
*/
3230
public function handle(Collection $collectors, \Closure $next, mixed $value, mixed $key): Stringable

src/Pipes/FixPrettyJsonPipe.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,12 @@ public function __construct(private JsonFixer $jsonFixer) {}
2727

2828
public function handle(Collection $collectors, \Closure $next, string $missingValue = '"..."'): Stringable
2929
{
30-
$content = $next($collectors);
31-
3230
try {
3331
$fixedReport = $this
3432
->jsonFixer
3533
->silent(false)
3634
->missingValue($missingValue)
37-
->fix((string) $content);
35+
->fix((string) $content = $next($collectors));
3836

3937
return str(json_pretty_encode(json_decode($fixedReport, true, 512, \JSON_THROW_ON_ERROR)));
4038
} catch (\Throwable) {

src/Support/ExceptionContext.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
*/
2222
class ExceptionContext
2323
{
24-
public static function getMarked(\Throwable $throwable, string $mark = ''): array
24+
public static function getMarked(\Throwable $throwable, string $mark = '', int $lineNumber = 5): array
2525
{
26-
return collect(static::get($throwable))
26+
return collect(self::get($throwable, $lineNumber))
2727
->tap(static function (Collection $collection) use (
2828
&$exceptionLine,
2929
$throwable,
@@ -52,9 +52,9 @@ public static function getMarked(\Throwable $throwable, string $mark = '➤'): a
5252
->all();
5353
}
5454

55-
public static function get(\Throwable $throwable): array
55+
public static function get(\Throwable $throwable, int $lineNumber = 5): array
5656
{
57-
return self::getEval($throwable) ?: self::getFile($throwable);
57+
return self::getEval($throwable) ?: self::getFile($throwable, $lineNumber);
5858
}
5959

6060
/**
@@ -69,11 +69,10 @@ private static function getEval(\Throwable $throwable): ?array
6969
return null;
7070
}
7171

72-
private static function getFile(\Throwable $throwable): array
72+
private static function getFile(\Throwable $throwable, int $lineNumber = 5): array
7373
{
74-
// file($throwable->getFile());
75-
return collect(explode(\PHP_EOL, file_get_contents($throwable->getFile())))
76-
->slice($throwable->getLine() - 10, 20)
74+
return collect(file($throwable->getFile()))
75+
->slice($throwable->getLine() - $lineNumber, 2 * $lineNumber - 1)
7776
->mapWithKeys(static fn (string $code, int $line): array => [$line + 1 => $code])
7877
->all();
7978
}

src/Support/JsonFixer.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,6 @@ private function lastToken(): mixed
202202
return end($this->stack);
203203
}
204204

205-
/**
206-
* @noinspection PhpInconsistentReturnPointsInspection
207-
*/
208205
private function popToken(?string $token = null): mixed
209206
{
210207
// Last one

src/Support/helpers.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,16 +108,6 @@ function json_pretty_encode(mixed $value, int $options = 0, int $depth = 512): s
108108
}
109109
}
110110

111-
if (!\function_exists('Guanguans\LaravelExceptionNotify\Support\hydrate_pipe')) {
112-
/**
113-
* @param class-string $pipe
114-
*/
115-
function hydrate_pipe(string $pipe, mixed ...$parameters): string
116-
{
117-
return [] === $parameters ? $pipe : \sprintf('%s:%s', $pipe, implode(',', $parameters));
118-
}
119-
}
120-
121111
if (!\function_exists('Guanguans\LaravelExceptionNotify\Support\human_bytes')) {
122112
/**
123113
* Convert bytes to human readable format.

0 commit comments

Comments
 (0)