Skip to content

Commit df885ba

Browse files
committed
implement Bounds class
1 parent 47fe726 commit df885ba

File tree

1 file changed

+35
-52
lines changed

1 file changed

+35
-52
lines changed

src/CodeSnippet.php

Lines changed: 35 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,12 @@ public function fromFile($file): self
101101
}
102102

103103
try {
104-
[$startLineNumber, $endLineNumber] = $this->getBoundsMulti($file->numberOfLines());
105-
106104
$code = [];
105+
$bounds = $this->getBoundsMulti($file->numberOfLines());
106+
$line = $file->getLine($bounds->start);
107+
$currentLineNumber = $bounds->start;
107108

108-
$line = $file->getLine($startLineNumber);
109-
110-
$currentLineNumber = $startLineNumber;
111-
112-
while ($currentLineNumber <= $endLineNumber) {
109+
while ($currentLineNumber <= $bounds->end) {
113110
$value = rtrim(substr($line, 0, 250));
114111
$isSelected = $this->isSurroundedLineNumber($currentLineNumber);
115112

@@ -150,7 +147,7 @@ protected function isSurroundedLineNumber(int $lineNumber): bool
150147
return in_array($lineNumber, $this->surroundingLines, true);
151148
}
152149

153-
protected function getBounds(int $surroundingLine, int $totalNumberOfLineInFile): array
150+
protected function getBounds(int $surroundingLine, int $totalNumberOfLineInFile): Bounds
154151
{
155152
$startLine = max($surroundingLine - floor($this->snippetLineCount / 2), 1);
156153

@@ -161,87 +158,73 @@ protected function getBounds(int $surroundingLine, int $totalNumberOfLineInFile)
161158
$startLine = max($endLine - ($this->snippetLineCount - 1), 1);
162159
}
163160

164-
return [$startLine, $endLine];
161+
return Bounds::createFromArray([$startLine, $endLine]);
165162
}
166163

167-
protected function getBoundsMulti(int $totalNumberOfLineInFile): array
164+
protected function getBoundsMulti(int $totalNumberOfLineInFile): Bounds
168165
{
169-
$startLine = $this->surroundingLines[0];
170-
$endLine = $this->surroundingLines[count($this->surroundingLines) - 1];
166+
$bounds = Bounds::createFromArray($this->surroundingLines);
171167

172168
// snippetLineCount() was used
173169
if (! is_int($this->linesAfter) || ! is_int($this->linesBefore)) {
174-
[$startLine, $endLine] = $this->getBoundsMultiForSnippetLineCount(
175-
$startLine, $endLine, $totalNumberOfLineInFile
176-
);
170+
$this->getBoundsMultiForSnippetLineCount($bounds, $totalNumberOfLineInFile);
177171
}
178172

179173
// linesBefore() and linesAfter() were used
180174
if (is_int($this->linesAfter) && is_int($this->linesBefore)) {
181-
$startLine -= $this->linesBefore;
182-
$endLine += $this->linesAfter;
175+
$bounds->start -= $this->linesBefore;
176+
$bounds->end += $this->linesAfter;
183177

184-
$this->updateSnippetLineCount($startLine, $endLine);
178+
$this->updateSnippetLineCount($bounds);
185179
}
186180

187-
[$startLine, $endLine] = $this->ensureBoundsAreWithinLimits($startLine, $endLine, $totalNumberOfLineInFile);
188-
[$startLine, $endLine] = $this->trimSnippetSize($startLine, $endLine);
181+
$this->ensureBoundsAreWithinLimits($bounds, $totalNumberOfLineInFile);
182+
$this->trimSnippetSize($bounds);
183+
$this->updateSnippetLineCount($bounds);
189184

190-
$this->updateSnippetLineCount($startLine, $endLine);
191-
192-
return [$startLine, $endLine];
185+
return $bounds;
193186
}
194187

195-
protected function getBoundsMultiForSnippetLineCount(int $firstLineNum, int $lastLineNum, int $totalNumberOfLineInFile): array
188+
protected function getBoundsMultiForSnippetLineCount(Bounds $bounds, int $totalNumberOfLineInFile): void
196189
{
197-
$startBounds = $this->getBounds($firstLineNum, $totalNumberOfLineInFile);
198-
$endBounds = $this->getBounds($lastLineNum, $totalNumberOfLineInFile);
199-
200-
$bounds = array_merge($startBounds, $endBounds);
201-
sort($bounds, SORT_NUMERIC);
190+
$startBounds = $this->getBounds($bounds->start, $totalNumberOfLineInFile);
191+
$endBounds = $this->getBounds($bounds->end, $totalNumberOfLineInFile);
202192

203-
$startLine = $bounds[0];
204-
$endLine = $bounds[count($bounds) - 1];
205-
206-
return [$startLine, $endLine];
193+
$bounds->copy($startBounds->mergeWith($endBounds));
207194
}
208195

209-
protected function updateSnippetLineCount(int $startLine, int $endLine): void
196+
protected function updateSnippetLineCount(Bounds $bounds): void
210197
{
211-
$this->snippetLineCount = ($endLine - $startLine) + 1;
198+
$this->snippetLineCount = ($bounds->end - $bounds->start) + 1;
212199
}
213200

214-
protected function trimSnippetSize(int $startLine, int $endLine): array
201+
protected function trimSnippetSize(Bounds $bounds): void
215202
{
216-
if (count(range($startLine, $endLine)) > $this->snippetLineCount) {
217-
if (! in_array($endLine, $this->surroundingLines, true)) {
218-
$endLine--;
203+
if (count(range($bounds->start, $bounds->end)) > $this->snippetLineCount) {
204+
if (! in_array($bounds->end, $this->surroundingLines, true)) {
205+
$bounds->end--;
219206
}
220207
}
221208

222-
if (count(range($startLine, $endLine)) > $this->snippetLineCount) {
223-
if (! in_array($startLine, $this->surroundingLines, true)) {
224-
$startLine++;
209+
if (count(range($bounds->start, $bounds->end)) > $this->snippetLineCount) {
210+
if (! in_array($bounds->start, $this->surroundingLines, true)) {
211+
$bounds->start++;
225212
}
226213
}
227-
228-
return [$startLine, $endLine];
229214
}
230215

231-
protected function ensureBoundsAreWithinLimits(int $startLine, int $endLine, int $totalNumberOfLineInFile): array
216+
protected function ensureBoundsAreWithinLimits(Bounds $bounds, int $totalNumberOfLineInFile): void
232217
{
233-
if ($startLine <= 0) {
234-
$startLine = 1;
218+
if ($bounds->start <= 0) {
219+
$bounds->start = 1;
235220
}
236221

237-
if ($endLine > $totalNumberOfLineInFile) {
238-
$endLine = $totalNumberOfLineInFile;
222+
if ($bounds->end > $totalNumberOfLineInFile) {
223+
$bounds->end = $totalNumberOfLineInFile;
239224

240225
if (count($this->surroundingLines) === 1) {
241-
$startLine = max($endLine - ($this->snippetLineCount - 1), 1);
226+
$bounds->start = max($bounds->end - ($this->snippetLineCount - 1), 1);
242227
}
243228
}
244-
245-
return [$startLine, $endLine];
246229
}
247230
}

0 commit comments

Comments
 (0)