recurly/recurly-client-php — Issue #23: Infinite Loop created in pager.php
Author: beaudesigns
Created: 2011-12-06 14:48:16+00:00
State: open

I've run into a really weird issue, I spent some time working on it but was unable to track it down completely.

The basic issue is this: When you try to add a **per_page** parameter to the request, in my case, for invoices, and the **per_page** number is a squared number of the total number of records available, it creates an infinite loop inside of the **Recurly_Pager->current()** method.

Example: I have 6 invoices for a particular user. I create this request:

```php
$this->invoices = Recurly_InvoiceList::getForAccount(self::$user->storage->id, array('per_page' => 3));
```
It creates an infinite loop.

If I set the page size to 4, or anything other than a number that can be squared to reach the total invoices, it works fine.

Here's the current code:

```php
public function current()
{
while ($this->_position >= sizeof($this->_objects)) {
if (isset($this->_links['next'])) {
$num_objects = sizeof($this->_objects);
$this->_loadFrom($this->_links['next']);
$this->_position -= $num_objects;
}
}

return $this->_objects[$this->_position];
}
```

The problem happens inside of the ```while()``` loop. If we add ``` && sizeof($this->_objects) > 0``` to the ```while()```, it partially fixes the problem, however, it creates an extra ```valid()``` record that is blank, meaning if we use a ```foreach()```, we'll have a blank record.

I've spent too much time trying to track it down already, if it becomes a big enough issue, I'll have to try to fix it, but hopefully y'all will figure it out before I do.

One thought, is that part of the problem might be that the API returns a ```rel="next";``` link in the header, even if there is no next page. Should that happen? If there's not another page, shouldn't it just leave that out? I think that would simplify the issue considerably.