@@ -101,15 +101,12 @@ public function fromFile($file): self
101
101
}
102
102
103
103
try {
104
- [$ startLineNumber , $ endLineNumber ] = $ this ->getBoundsMulti ($ file ->numberOfLines ());
105
-
106
104
$ code = [];
105
+ $ bounds = $ this ->getBoundsMulti ($ file ->numberOfLines ());
106
+ $ line = $ file ->getLine ($ bounds ->start );
107
+ $ currentLineNumber = $ bounds ->start ;
107
108
108
- $ line = $ file ->getLine ($ startLineNumber );
109
-
110
- $ currentLineNumber = $ startLineNumber ;
111
-
112
- while ($ currentLineNumber <= $ endLineNumber ) {
109
+ while ($ currentLineNumber <= $ bounds ->end ) {
113
110
$ value = rtrim (substr ($ line , 0 , 250 ));
114
111
$ isSelected = $ this ->isSurroundedLineNumber ($ currentLineNumber );
115
112
@@ -150,7 +147,7 @@ protected function isSurroundedLineNumber(int $lineNumber): bool
150
147
return in_array ($ lineNumber , $ this ->surroundingLines , true );
151
148
}
152
149
153
- protected function getBounds (int $ surroundingLine , int $ totalNumberOfLineInFile ): array
150
+ protected function getBounds (int $ surroundingLine , int $ totalNumberOfLineInFile ): Bounds
154
151
{
155
152
$ startLine = max ($ surroundingLine - floor ($ this ->snippetLineCount / 2 ), 1 );
156
153
@@ -161,87 +158,73 @@ protected function getBounds(int $surroundingLine, int $totalNumberOfLineInFile)
161
158
$ startLine = max ($ endLine - ($ this ->snippetLineCount - 1 ), 1 );
162
159
}
163
160
164
- return [$ startLine , $ endLine ];
161
+ return Bounds:: createFromArray ( [$ startLine , $ endLine ]) ;
165
162
}
166
163
167
- protected function getBoundsMulti (int $ totalNumberOfLineInFile ): array
164
+ protected function getBoundsMulti (int $ totalNumberOfLineInFile ): Bounds
168
165
{
169
- $ startLine = $ this ->surroundingLines [0 ];
170
- $ endLine = $ this ->surroundingLines [count ($ this ->surroundingLines ) - 1 ];
166
+ $ bounds = Bounds::createFromArray ($ this ->surroundingLines );
171
167
172
168
// snippetLineCount() was used
173
169
if (! is_int ($ this ->linesAfter ) || ! is_int ($ this ->linesBefore )) {
174
- [$ startLine , $ endLine ] = $ this ->getBoundsMultiForSnippetLineCount (
175
- $ startLine , $ endLine , $ totalNumberOfLineInFile
176
- );
170
+ $ this ->getBoundsMultiForSnippetLineCount ($ bounds , $ totalNumberOfLineInFile );
177
171
}
178
172
179
173
// linesBefore() and linesAfter() were used
180
174
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 ;
183
177
184
- $ this ->updateSnippetLineCount ($ startLine , $ endLine );
178
+ $ this ->updateSnippetLineCount ($ bounds );
185
179
}
186
180
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 );
189
184
190
- $ this ->updateSnippetLineCount ($ startLine , $ endLine );
191
-
192
- return [$ startLine , $ endLine ];
185
+ return $ bounds ;
193
186
}
194
187
195
- protected function getBoundsMultiForSnippetLineCount (int $ firstLineNum , int $ lastLineNum , int $ totalNumberOfLineInFile ): array
188
+ protected function getBoundsMultiForSnippetLineCount (Bounds $ bounds , int $ totalNumberOfLineInFile ): void
196
189
{
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 );
202
192
203
- $ startLine = $ bounds [0 ];
204
- $ endLine = $ bounds [count ($ bounds ) - 1 ];
205
-
206
- return [$ startLine , $ endLine ];
193
+ $ bounds ->copy ($ startBounds ->mergeWith ($ endBounds ));
207
194
}
208
195
209
- protected function updateSnippetLineCount (int $ startLine , int $ endLine ): void
196
+ protected function updateSnippetLineCount (Bounds $ bounds ): void
210
197
{
211
- $ this ->snippetLineCount = ($ endLine - $ startLine ) + 1 ;
198
+ $ this ->snippetLineCount = ($ bounds -> end - $ bounds -> start ) + 1 ;
212
199
}
213
200
214
- protected function trimSnippetSize (int $ startLine , int $ endLine ): array
201
+ protected function trimSnippetSize (Bounds $ bounds ): void
215
202
{
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 --;
219
206
}
220
207
}
221
208
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 ++;
225
212
}
226
213
}
227
-
228
- return [$ startLine , $ endLine ];
229
214
}
230
215
231
- protected function ensureBoundsAreWithinLimits (int $ startLine , int $ endLine , int $ totalNumberOfLineInFile ): array
216
+ protected function ensureBoundsAreWithinLimits (Bounds $ bounds , int $ totalNumberOfLineInFile ): void
232
217
{
233
- if ($ startLine <= 0 ) {
234
- $ startLine = 1 ;
218
+ if ($ bounds -> start <= 0 ) {
219
+ $ bounds -> start = 1 ;
235
220
}
236
221
237
- if ($ endLine > $ totalNumberOfLineInFile ) {
238
- $ endLine = $ totalNumberOfLineInFile ;
222
+ if ($ bounds -> end > $ totalNumberOfLineInFile ) {
223
+ $ bounds -> end = $ totalNumberOfLineInFile ;
239
224
240
225
if (count ($ this ->surroundingLines ) === 1 ) {
241
- $ startLine = max ($ endLine - ($ this ->snippetLineCount - 1 ), 1 );
226
+ $ bounds -> start = max ($ bounds -> end - ($ this ->snippetLineCount - 1 ), 1 );
242
227
}
243
228
}
244
-
245
- return [$ startLine , $ endLine ];
246
229
}
247
230
}
0 commit comments