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
-------
24dcb5202c
Add a method to check if any results were found
This commit is contained in:
commit
eb1183111a
@ -5,6 +5,7 @@ CHANGELOG
|
||||
-----
|
||||
|
||||
* deprecated `Symfony\Component\Finder\Iterator\FilterIterator`
|
||||
* added Finder::hasResults() method to check if any results were found
|
||||
|
||||
3.3.0
|
||||
-----
|
||||
|
@ -613,6 +613,20 @@ class Finder implements \IteratorAggregate, \Countable
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the any results were found.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasResults()
|
||||
{
|
||||
foreach ($this->getIterator() as $_) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Counts all the results collected by the iterators.
|
||||
*
|
||||
|
@ -424,6 +424,20 @@ class FinderTest extends Iterator\RealIteratorTestCase
|
||||
count($finder);
|
||||
}
|
||||
|
||||
public function testHasResults()
|
||||
{
|
||||
$finder = $this->buildFinder();
|
||||
$finder->in(__DIR__);
|
||||
$this->assertTrue($finder->hasResults());
|
||||
}
|
||||
|
||||
public function testNoResults()
|
||||
{
|
||||
$finder = $this->buildFinder();
|
||||
$finder->in(__DIR__)->name('DoesNotExist');
|
||||
$this->assertFalse($finder->hasResults());
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getContainsTestData
|
||||
*/
|
||||
|
Reference in New Issue
Block a user