Skip to content
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

Calling Sequence->get() with an index of -1 doesn't work in 4.8+ #85

Closed
Jamesking56 opened this issue Jul 11, 2019 · 10 comments
Closed
Assignees

Comments

@Jamesking56
Copy link
Contributor

Jamesking56 commented Jul 11, 2019

Bug Report

Information Description
Version 4.8.0
PHP version 7.2.19
OS Platform Linux (Ubuntu 18.04)

Summary

According to the documentation, the get() method supports passing in a negative index from version 4.8 and above. When I do this however, I get InvalidIndex exception thrown with the message:

-1 is an invalid index in the current sequence

Standalone code, or other way to reproduce the problem

Create any Sequence (empty or not) and call ->get(-1).

Expected result

I'm expecting that the return value is the latest Period item in the Sequence as shown in the documentation:

https://period.thephpleague.com/4.0/sequence/collection/#sequenceget

Actual result

InvalidIndex exception is thrown citing that -1 is an invalid index.

@Jamesking56 Jamesking56 changed the title Calling ->get() with -1 doesn't work in 4.8+ Calling Sequence->get() with an index of -1 doesn't work in 4.8+ Jul 11, 2019
@nyamsprod
Copy link
Member

@Jamesking56 thanks for using the library. Could you provide a snippet please as this feature is covered by tests I would need to see a concrete example in order to understand the origin of the issue if there's one.

thanks in advance.

@Jamesking56
Copy link
Contributor Author

Well, its the smallest concrete example known to man really, I don't think your tests actually cover negative offsets:

$sequence = new Sequence(
    new Period(
        new \DateTimeImmutable('yesterday'),
        new \DateTimeImmutable('now')
    )
);

$sequence->get(-1); // Exception instead of expected period.

@Jamesking56
Copy link
Contributor Author

Same with an empty Sequence, I think it should return null in this instance:

$sequence = new Sequence();

$sequence->get(-1); // Exception instead of expected period.

@nyamsprod
Copy link
Member

$sequence = new Sequence();

$sequence->get(-1); // Exception instead of expected period.

This is correct with an empty sequence get should definitely throw ...

The first example is debatable but I can see where your reasoning comes from ... I'll see if I'll fix it or not it needs thinking first.

@Jamesking56
Copy link
Contributor Author

Looks like its just the Sequence with a single item that needs fixing then, as this correct logic works when there is 2 or more items.

@Jamesking56
Copy link
Contributor Author

Thinking further about this, I think it would be a lot nicer if there was a latest method on Sequence that returned the latest Period or null if there isn't one. No exceptions thrown.

@nyamsprod
Copy link
Member

Thinking further about this, I think it would be a lot nicer if there was a latest method on Sequence that returned the latest Period or null if there isn't one. No exceptions thrown.

Just use the Sequence::isEmpty method for that

nyamsprod added a commit that referenced this issue Jul 12, 2019
@nyamsprod
Copy link
Member

the issue is fixed ... it will be released in a patch version in the upcoming days

@nyamsprod
Copy link
Member

the 4.8.1 release it out with the fix.

@Jamesking56
Copy link
Contributor Author

Thanks @nyamsprod

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants