Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

WIP: Fix 330, return NULL if current() is not countable #334

Closed
wants to merge 1 commit into from

Conversation

ezimuel
Copy link
Contributor

@ezimuel ezimuel commented Aug 9, 2018

This PR fixes #330 returning NULL if current() result is not countable. It removes and changes part of the Zend\Db\ResultSet\AbstractResultSet::current() implementation modified with 8276cf6.

@ezimuel
Copy link
Contributor Author

ezimuel commented Aug 9, 2018

Adding @bartmcleod to review this, since I changed the previous implementation 8276cf6

Copy link
Member

@Ocramius Ocramius left a comment

Choose a reason for hiding this comment

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

Requires test additions before being considered. Edge cases to be considered:

  • countable value
  • iterable value
  • non-countable non-iterable value
  • array
  • scalar
  • countable and iterable value

@@ -208,7 +203,7 @@ public function current()
if (is_array($this->buffer)) {
$this->buffer[$this->position] = $data;
}
return is_array($data) ? $data : null;
return (is_array($data) || $data instanceof Countable) ? $data : null;
Copy link
Member

Choose a reason for hiding this comment

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

Why is Countable a valid type, but (for example) Traversable not? What's the rationale behind the change?

Copy link
Contributor

@bartmcleod bartmcleod Aug 9, 2018

Choose a reason for hiding this comment

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

@ezimuel Since this apparently fixes #330 because of a bug I might have introduced earlier, it is not needed to cover every single interface that could be returned by ->current(). If it fixes #330 and does not break anything, then it is ok, but we should see a unit test that confirms #330 and thus explains the need for the fix.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@bartmcleod The code that you introduced here just returns the current value of dataSource that can be a different type, depending on the adapter (e.g. for Mysqli is false, for PDO is null, etc). Maybe, I should align all the default value of the adapter to be the same but I'm not sure this will introduce BC breaks in some existing project. That's why I focused my fix on the AbstractResultSet.

@Ocramius I'm trying to accomodate different return type for Resultset::current() coming from different adapter, it's seems there is inconsistency with different adapters (see above). Anyway, I'm still testing all the use cases, it's quite difficult without type hinting...

Copy link
Contributor

Choose a reason for hiding this comment

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

@ezimuel would it be okay to check with empty() and to always return null in that case? So that false and null are treated equally.

Copy link
Contributor

@bartmcleod bartmcleod left a comment

Choose a reason for hiding this comment

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

Please add a test to confirm #330 and its fix.

// datasource was an array when the resultset was initialized
return $this->dataSource->current();
}

Copy link
Contributor

Choose a reason for hiding this comment

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

I had my doubts about this part, but at the time, it was needed, so I am surprised the Travis build succeeds without these lines. If it does, it is probably ok.

@@ -208,7 +203,7 @@ public function current()
if (is_array($this->buffer)) {
$this->buffer[$this->position] = $data;
}
return is_array($data) ? $data : null;
return (is_array($data) || $data instanceof Countable) ? $data : null;
Copy link
Contributor

@bartmcleod bartmcleod Aug 9, 2018

Choose a reason for hiding this comment

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

@ezimuel Since this apparently fixes #330 because of a bug I might have introduced earlier, it is not needed to cover every single interface that could be returned by ->current(). If it fixes #330 and does not break anything, then it is ok, but we should see a unit test that confirms #330 and thus explains the need for the fix.

@ezimuel ezimuel changed the title Fix 330, return NULL if current() is not countable WIP: Fix 330, return NULL if current() is not countable Aug 10, 2018
@ezimuel
Copy link
Contributor Author

ezimuel commented Aug 17, 2018

Closing in favor of #337

@ezimuel ezimuel closed this Aug 17, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Zend\Db\ResultSet\ResultSet->current() returns false instead of NULL
3 participants