Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Finder] Add a method to check if any results were found #23471

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 26, 2017

Conversation

duncan3dc
Copy link
Contributor

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:

$found = false;
foreach ($finder as $thing) {
    $found = true;
    break;
}
if ($found) {

This PR enables the much more readable:

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

@duncan3dc duncan3dc changed the base branch from master to 3.4 July 10, 2017 21:40
@duncan3dc duncan3dc force-pushed the finder-found branch 2 times, most recently from f76cec1 to d30a06a Compare July 10, 2017 21:46
@Koc
Copy link
Contributor

Koc commented Jul 10, 2017

Why not to use count($finder)?

@duncan3dc
Copy link
Contributor Author

Because that will iterate through every single file that matches. If I only want to know if some exist or not, that's an overly expensive check

@nicolas-grekas
Copy link
Member

nicolas-grekas commented Jul 11, 2017

I remember a similar proposal that wasn't accepted, because the use case is not common enough.
You can achieve it in just one extra line btw:

foreach ($finder as $thing) {
    // do things here
    break;
}

@nicolas-grekas nicolas-grekas added this to the 3.4 milestone Jul 11, 2017
@duncan3dc
Copy link
Contributor Author

It may be only one extra line but a lot of the clarity is lost. You see a foreach and you think we're doing something with the results, it's misleading

@voronkovich
Copy link
Contributor

@duncan3dc, I'm not sure but I think you could try to use the Iterator::valid method instead of foreach loop:

if ($finder->getIterator->valid()) {
}

@duncan3dc
Copy link
Contributor Author

@voronkovich Not if the iterator is an IteratorAggregate you can't

@javiereguiluz
Copy link
Member

I like this feature proposal ... and I recently needed it 😄

However, I don't like the proposed found() method name, so I checked the method names of PHP SPL iterators. They don't have a method to check if the iterator has elements, but somewhat related it's the valid() method (e.g. in ArrayIterator, valid() returns true if the array contains any more entries).

I'd prefer a more descriptive method name: hasResults(), hasItems(), hasEntries(), hasElements(), etc.

@Simperfit
Copy link
Contributor

I like the proposal too, and it can be useful, hasResults() is quite nice

@voronkovich
Copy link
Contributor

👍 for hasResults

*
* @return bool
*/
public function found()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about matches() maybe?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've changed it to hasResults() now

*/
public function found()
{
foreach ($this->getIterator() as $null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might as well update getIterator() to @return \Traversable|SplFileInfo[] then, to indicate why we add this method. Currently we could rely on getIterator()->valid() i believe :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that as outside the scope of this change, I suggest opening a separate PR if you believe that should be changed

@nicolas-grekas
Copy link
Member

@symfony/deciders we need to make a decision here. Personally I'm "-0" :)

@javiereguiluz
Copy link
Member

I'm 👍

*/
public function hasResults()
{
foreach ($this->getIterator() as $null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor suggestion: $null may be confusing and in the future, it may be a reserved word in PHP. Could we use $_ as the name of this unused variable as we do in other parts of Symfony? Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I wasn't aware of the convention, I've updated it to use $_ now

@@ -5,6 +5,7 @@ CHANGELOG
-----

* deprecated `Symfony\Component\Finder\Iterator\FilterIterator`
* added Finder::hasResults() method to check if any results were found
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there should be backticks around the method:

 * added `Finder::hasResults()` method to check if any results were found

@fabpot
Copy link
Member

fabpot commented Sep 26, 2017

Thank you @duncan3dc.

@fabpot fabpot merged commit 24dcb52 into symfony:3.4 Sep 26, 2017
fabpot added a commit that referenced this pull request Sep 26, 2017
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants