Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit eb11831

Browse filesBrowse files
committed
feature #23471 [Finder] Add a method to check if any results were found (duncan3dc)
This PR was merged into the 3.4 branch. Discussion ---------- [Finder] Add a method to check if any results were found | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | License | MIT If I want to know if any results were found, but I don't want to start trawling through them, I have to do the rather ugly: ```php $found = false; foreach ($finder as $thing) { $found = true; break; } if ($found) { ``` This PR enables the much more readable: ```php if ($finder->found()) { ``` This seemed like an obvious thing to me, so I suspect there might be a reason this doesn't exist already, but I couldn't find any previous discussion. If it'll be accepted then I'll glady create a docs PR Commits ------- 24dcb52 Add a method to check if any results were found
2 parents 08825f8 + 24dcb52 commit eb11831
Copy full SHA for eb11831

File tree

3 files changed

+29
-0
lines changed
Filter options

3 files changed

+29
-0
lines changed

‎src/Symfony/Component/Finder/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Finder/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
-----
66

77
* deprecated `Symfony\Component\Finder\Iterator\FilterIterator`
8+
* added Finder::hasResults() method to check if any results were found
89

910
3.3.0
1011
-----

‎src/Symfony/Component/Finder/Finder.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Finder/Finder.php
+14Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,20 @@ public function append($iterator)
613613
return $this;
614614
}
615615

616+
/**
617+
* Check if the any results were found.
618+
*
619+
* @return bool
620+
*/
621+
public function hasResults()
622+
{
623+
foreach ($this->getIterator() as $_) {
624+
return true;
625+
}
626+
627+
return false;
628+
}
629+
616630
/**
617631
* Counts all the results collected by the iterators.
618632
*

‎src/Symfony/Component/Finder/Tests/FinderTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Finder/Tests/FinderTest.php
+14Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,20 @@ public function testCountWithoutIn()
424424
count($finder);
425425
}
426426

427+
public function testHasResults()
428+
{
429+
$finder = $this->buildFinder();
430+
$finder->in(__DIR__);
431+
$this->assertTrue($finder->hasResults());
432+
}
433+
434+
public function testNoResults()
435+
{
436+
$finder = $this->buildFinder();
437+
$finder->in(__DIR__)->name('DoesNotExist');
438+
$this->assertFalse($finder->hasResults());
439+
}
440+
427441
/**
428442
* @dataProvider getContainsTestData
429443
*/

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.