Skip to content

Commit da1c521

Browse files
harm-lessharm-less
harm-less
authored and
harm-less
committed
Bugfix
- Load method in FilesQuery has a max files load - Bugfix: adding a dir to the DirCollection caused it to have wrong indexes because a index of -1 could occur
1 parent 82716ae commit da1c521

File tree

6 files changed

+33
-23
lines changed

6 files changed

+33
-23
lines changed

src/FQ/collections/DirCollection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function addDir(Dir $dir, $index = null) {
2323
if (is_int($index) && $totalDirs < $index) {
2424
throw new DirCollectionException(sprintf('Trying to add dir, but the provided index of "%s" is to high. There are currently %s dirs.', $index, $totalDirs));
2525
}
26-
array_splice($this->_dirs, $index === null ? $totalDirs - 1 : $index, 0, array($dir));
26+
array_splice($this->_dirs, $index === null ? $totalDirs : $index, 0, array($dir));
2727
return $dir;
2828
}
2929

src/FQ/query/FilesQuery.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,13 +325,23 @@ protected function _prepareQueryChild(ChildDir $childDir, $rootDirs) {
325325
return $queryChild;
326326
}
327327

328-
public function load() {
328+
/**
329+
* @param int $maxFiles
330+
* @return bool
331+
*/
332+
public function load($maxFiles = 0) {
329333
$this->_hasRunCheck();
330334

331335
if ($this->queryHasFilter(FilesQuery::FILTER_EXISTING)) {
336+
$filesLoaded = 0;
332337
foreach ($this->listPathsSimple() as $path) {
333338
/** @noinspection PhpIncludeInspection */
334339
require_once $path;
340+
341+
$filesLoaded++;
342+
if ($maxFiles > 0 && $filesLoaded >= $maxFiles) {
343+
break;
344+
}
335345
}
336346
return true;
337347
}

src/FQ/query/FilesQueryRequirements.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ protected function requirementLast(FilesQuery $query) {
204204
$lastRootDirId = null;
205205
$rootSelection = $query->getCurrentRootDirSelection();
206206
if (count($rootSelection) >= 1) {
207-
$lastRootDirId = $rootSelection[0]->id();
207+
$lastRootDirId = $rootSelection[count($rootSelection) - 1]->id();
208208

209209
foreach ($query->queryChildDirs(true) as $child) {
210210
$pathExist = $child->pathsExist();

tests/FQ/Tests/FilesTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,15 +224,15 @@ public function testQueryPathWithTwoRootDirs() {
224224
$this->_addRootDir();
225225
$this->_addRootDir(null, $this->_newActualRootDirSecond());
226226
$this->_addChildDir();
227-
$this->assertEquals(self::ROOT_DIR_SECOND_ABSOLUTE_PATH . '/child1/File1.php', $files->queryPath('File1'));
227+
$this->assertEquals(self::ROOT_DIR_DEFAULT_ABSOLUTE_PATH . '/child1/File1.php', $files->queryPath('File1'));
228228
$this->assertFalse($files->queryPath('does-not-exist'));
229229
}
230230
public function testQueryPathWithTwoRootDirsWhenReversed() {
231231
$files = $this->files();
232232
$this->_addRootDir();
233233
$this->_addRootDir(null, $this->_newActualRootDirSecond());
234234
$this->_addChildDir();
235-
$this->assertEquals(self::ROOT_DIR_DEFAULT_ABSOLUTE_PATH . '/child1/File1.php', $files->queryPath('File1', null, null, true));
235+
$this->assertEquals(self::ROOT_DIR_SECOND_ABSOLUTE_PATH . '/child1/File1.php', $files->queryPath('File1', null, null, true));
236236
$this->assertFalse($files->queryPath('does-not-exist'));
237237
}
238238

tests/FQ/Tests/Query/FilesQueryTest.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -250,27 +250,27 @@ public function testListBasePathsWithReversedSetToTrue() {
250250
$index = 0;
251251
foreach ($paths as $rootDirId => $path) {
252252
if ($index === 0) {
253-
$this->assertEquals(self::ROOT_DIR_DEFAULT_ID, $rootDirId);
254-
$this->assertEquals(self::ROOT_DIR_DEFAULT_BASE_PATH . '/child1/File1.php', $path);
255-
}
256-
else if ($index === 1) {
257253
$this->assertEquals(self::ROOT_DIR_SECOND_ID, $rootDirId);
258254
$this->assertEquals(self::ROOT_DIR_SECOND_BASE_PATH . '/child1/File1.php', $path);
259255
}
256+
else if ($index === 1) {
257+
$this->assertEquals(self::ROOT_DIR_DEFAULT_ID, $rootDirId);
258+
$this->assertEquals(self::ROOT_DIR_DEFAULT_BASE_PATH . '/child1/File1.php', $path);
259+
}
260260
$index++;
261261
}
262262

263263
$rawPaths = $query->listRawPaths();
264264
$index = 0;
265265
foreach ($paths as $rootDirId => $path) {
266266
if ($index === 0) {
267-
$this->assertEquals(self::ROOT_DIR_DEFAULT_ID, $rootDirId);
268-
$this->assertEquals(self::ROOT_DIR_DEFAULT_BASE_PATH . '/child1/File1.php', $path);
269-
}
270-
else if ($index === 1) {
271267
$this->assertEquals(self::ROOT_DIR_SECOND_ID, $rootDirId);
272268
$this->assertEquals(self::ROOT_DIR_SECOND_BASE_PATH . '/child1/File1.php', $path);
273269
}
270+
else if ($index === 1) {
271+
$this->assertEquals(self::ROOT_DIR_DEFAULT_ID, $rootDirId);
272+
$this->assertEquals(self::ROOT_DIR_DEFAULT_BASE_PATH . '/child1/File1.php', $path);
273+
}
274274
$index++;
275275
}
276276
}
@@ -284,13 +284,13 @@ public function testListRawPathsWithReversedSetToTrue() {
284284
$index = 0;
285285
foreach ($rawPaths as $rootDirId => $path) {
286286
if ($index === 0) {
287-
$this->assertEquals(self::ROOT_DIR_DEFAULT_ID, $rootDirId);
288-
$this->assertEquals(self::ROOT_DIR_DEFAULT_ABSOLUTE_PATH . '/child1/File1.php', $path);
289-
}
290-
else if ($index === 1) {
291287
$this->assertEquals(self::ROOT_DIR_SECOND_ID, $rootDirId);
292288
$this->assertEquals(self::ROOT_DIR_SECOND_ABSOLUTE_PATH . '/child1/File1.php', $path);
293289
}
290+
else if ($index === 1) {
291+
$this->assertEquals(self::ROOT_DIR_DEFAULT_ID, $rootDirId);
292+
$this->assertEquals(self::ROOT_DIR_DEFAULT_ABSOLUTE_PATH . '/child1/File1.php', $path);
293+
}
294294
$index++;
295295
}
296296
}

tests/FQ/Tests/Samples/SimpleSampleTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ public function testQueryFile1InReverse(){
3333
$index = 0;
3434
foreach ($paths as $rootDirId => $childPaths) {
3535
if ($index === 0) {
36-
$this->assertEquals(self::ROOT_DIR_DEFAULT_ID, $rootDirId);
37-
$this->assertEquals($this->sample()->root() . 'root1/child2/File1.php', $childPaths[0]);
38-
$this->assertEquals($this->sample()->root() . 'root1/child1/File1.php', $childPaths[1]);
36+
$this->assertEquals(self::ROOT_DIR_SECOND_ID, $rootDirId);
37+
$this->assertEquals($this->sample()->root() . 'root2/child1/File1.php', $childPaths[0]);
38+
$this->assertEquals($this->sample()->root() . 'root2/child2/File1.php', $childPaths[1]);
3939
}
4040
else if ($index === 1) {
41-
$this->assertEquals(self::ROOT_DIR_SECOND_ID, $rootDirId);
42-
$this->assertEquals($this->sample()->root() . 'root2/child2/File1.php', $childPaths[0]);
43-
$this->assertEquals($this->sample()->root() . 'root2/child1/File1.php', $childPaths[1]);
41+
$this->assertEquals(self::ROOT_DIR_DEFAULT_ID, $rootDirId);
42+
$this->assertEquals($this->sample()->root() . 'root1/child1/File1.php', $childPaths[0]);
43+
$this->assertEquals($this->sample()->root() . 'root1/child2/File1.php', $childPaths[1]);
4444
}
4545
$index++;
4646
}

0 commit comments

Comments
 (0)