Skip to content

Commit

Permalink
feature #23471 [Finder] Add a method to check if any results were fou…
Browse files Browse the repository at this point in the history
…nd (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
  • Loading branch information
fabpot committed Sep 26, 2017
2 parents 08825f8 + 24dcb52 commit eb11831
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Symfony/Component/Finder/CHANGELOG.md
Expand Up @@ -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
-----
Expand Down
14 changes: 14 additions & 0 deletions src/Symfony/Component/Finder/Finder.php
Expand Up @@ -613,6 +613,20 @@ public function append($iterator)
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.
*
Expand Down
14 changes: 14 additions & 0 deletions src/Symfony/Component/Finder/Tests/FinderTest.php
Expand Up @@ -424,6 +424,20 @@ public function testCountWithoutIn()
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
*/
Expand Down

0 comments on commit eb11831

Please sign in to comment.