Skip to content

Commit 131ea65

Browse files
committed
refactor getSourceAt() in AbstractSourceCodeFormatter to use a linesAround setting instead of a total number of lines.
1 parent 23b870b commit 131ea65

File tree

4 files changed

+41
-35
lines changed

4 files changed

+41
-35
lines changed

config/query-tracer.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,19 @@
234234
|
235235
*/
236236

237-
'includeSource' => true,
238-
'includeSourceLines' => 8,
237+
'includeSource' => true,
238+
239+
/*
240+
|--------------------------------------------------------------------------
241+
| Number of Source Code Lines Around Target Line
242+
|--------------------------------------------------------------------------
243+
|
244+
| Here you can specify how many lines of source code should be displayed
245+
| before and after the target line of the stack frame.
246+
|
247+
*/
248+
249+
'sourceLinesAround' => 4,
239250

240251
/*
241252
|--------------------------------------------------------------------------

src/Classes/AbstractSourceCodeFormatter.php

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,34 +32,37 @@ public function __construct(Config $config)
3232
* Retrieves the formatted source code from the specified file around the given line.
3333
*
3434
* @param string $fileName
35-
* @param int $line
35+
* @param int $line
3636
*
3737
* @return string|null
3838
*/
3939
public function getSourceAt(string $fileName, int $line): ?string
4040
{
41-
if ($this->config->shouldIncludeSource()) {
41+
if ($this->config->shouldIncludeSource() && $line > 0) {
4242
if ($cached = $this->sourceCache[$fileName][$line] ?? false) {
4343
return $cached;
4444
}
4545

4646
$sourceCode = file($fileName);
47-
$numSourceLines = $this->config->getSourceLineCount();
48-
$startingLine = $line - round($numSourceLines / 2);
4947

50-
while ($startingLine > 1 && trim($sourceCode[$startingLine - 1] ?? '') === '') {
51-
$startingLine--;
52-
$numSourceLines++;
53-
}
48+
$sourceLinesAround = $this->config->getSourceLinesAround();
49+
$totalLines = count($sourceCode);
50+
$startingLine = max(1, $line - $sourceLinesAround);
51+
$endingLine = min($totalLines, $line + $sourceLinesAround);
5452

55-
$lineCounter = $startingLine;
56-
$codeSlice = collect(array_slice($sourceCode, $startingLine, $numSourceLines))->mapWithKeys(
57-
function ($line) use (&$lineCounter) {
58-
return [++$lineCounter => $line];
59-
}
60-
);
53+
if ($startingLine <= $endingLine) {
54+
$numSourceLines = $endingLine - $startingLine + 1;
55+
$lineCounter = $startingLine;
6156

62-
return $this->sourceCache[$fileName][$line] = $this->getFormattedCode($codeSlice, $line);
57+
$codeSlice = collect(array_slice($sourceCode, $startingLine - 1, $numSourceLines))
58+
->mapWithKeys(
59+
function ($line) use (&$lineCounter) {
60+
return [$lineCounter++ => $line];
61+
}
62+
);
63+
64+
return $this->sourceCache[$fileName][$line] = $this->getFormattedCode($codeSlice, $line);
65+
}
6366
}
6467

6568
return null;
@@ -70,7 +73,7 @@ function ($line) use (&$lineCounter) {
7073
* Formats the source code listing to include in the trace.
7174
*
7275
* @param Collection $code
73-
* @param int $highlightedLine
76+
* @param int $highlightedLine
7477
*
7578
* @return string
7679
*/
@@ -123,8 +126,8 @@ protected function removeExpendableIndentation(Collection $code): Collection
123126
* Formats the given line according to the maximum line length.
124127
*
125128
* @param string $line
126-
* @param bool $trim
127-
* @param bool $highlightSpaces
129+
* @param bool $trim
130+
* @param bool $highlightSpaces
128131
*
129132
* @return string
130133
*/
@@ -145,7 +148,7 @@ protected function formatLine(string $line, bool $trim = true, bool $highlightSp
145148
* Adds line numbers to the given code lines.
146149
*
147150
* @param Collection $code
148-
* @param int $highlightedLine
151+
* @param int $highlightedLine
149152
*
150153
* @return mixed
151154
*/

src/Classes/Config.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function getLogArrayValues(): array
1616

1717
public function shouldIncludeSource(): bool
1818
{
19-
return config('query-tracer.trace.includeSource') === true && $this->getSourceLineCount() > 0;
19+
return config('query-tracer.trace.includeSource') === true;
2020
}
2121

2222
public function getCommentTag(): string
@@ -34,9 +34,9 @@ public function getLineLength(): int
3434
return config('query-tracer.trace.sqlComment.lineLength', 80);
3535
}
3636

37-
public function getSourceLineCount(): int
37+
public function getSourceLinesAround(): int
3838
{
39-
return config('query-tracer.trace.includeSourceLines');
39+
return config('query-tracer.trace.sourceLinesAround');
4040
}
4141

4242
public function getHighlightLineDecoration(): string

tests/Feature/DefaultModeTest.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,16 @@
3636

3737
it('includes the source in the query log trace', function () {
3838
config()->set('query-tracer.trace.includeSource', true);
39-
config()->set('query-tracer.trace.includeSourceLines', 8);
39+
config()->set('query-tracer.trace.sourceLinesAround', 4);
4040
config()->set('query-tracer.trace.logArray.values', ['source']);
4141

4242
expectTraceArray()->toHaveKey('source')->containingPerformQuerySourceCode();
4343
});
4444

4545

46-
it('includes the calling line only if includeSourceLines is set to 1', function () {
46+
it('includes the calling line only if sourceLinesAround is set to 0', function () {
4747
config()->set('query-tracer.trace.includeSource', true);
48-
config()->set('query-tracer.trace.includeSourceLines', 1);
48+
config()->set('query-tracer.trace.sourceLinesAround', 0);
4949
config()->set('query-tracer.trace.logArray.values', ['source']);
5050

5151
expectTraceArray()->toHaveKey('source')->containingPerformQuerySourceCode(true);
@@ -62,14 +62,6 @@
6262
});
6363

6464

65-
it('does not include source if includeSourceLines is set to 0', function () {
66-
config()->set('query-tracer.trace.includeSource', true);
67-
config()->set('query-tracer.trace.includeSourceLines', 0);
68-
69-
expectTraceArray()->not->toHaveKey('source');
70-
});
71-
72-
7365
it('only contains the configured log array values', function () {
7466
$availableValues = getAvailableLogArrayValues();
7567

0 commit comments

Comments
 (0)