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

UnsavedRelationList::first() / last() return false instead of null when list is empty #11083

Closed
kinglozzer opened this issue Dec 1, 2023 · 5 comments

Comments

@kinglozzer
Copy link
Member

kinglozzer commented Dec 1, 2023

Pull request: #11085

Affected Version

5.x

Description

UnsavedRelationList::first() and UnsavedRelationList::last() return false instead of null when the list is empty. This is inconsistent with DataList and co, which return null. Cause is the use of reset() and end():

public function first()
{
$item = reset($this->items);
if (is_numeric($item)) {
$item = DataObject::get_by_id($this->dataClass, $item);
}
if (!empty($this->extraFields[key($this->items)])) {
$item->update($this->extraFields[key($this->items)]);
}
return $item;
}

public function last()
{
$item = end($this->items);
if (!empty($this->extraFields[key($this->items)])) {
$item->update($this->extraFields[key($this->items)]);
}
return $item;
}

I suppose there’s a minor BC risk to changing this, open to feedback on whether this is appropriate for a patch release.

@GuySartorelli
Copy link
Member

GuySartorelli commented Dec 3, 2023

I'm of the opinion that false is just outright wrong here and should have always been null, and that anyone relying on false will/should be doing so only as a falsy check where null will behave the same way (i.e. if (!$returnValue)).

If this is new as of CMS 5, then I doubt anyone else has even noticed the changed return type.

tl;dr: I'd support this being patched rather than waiting for a major release.

But we should probably get at least one more opinion. @silverstripe/core-team any other opinions?

@michalkleiner
Copy link
Contributor

patch 👍

@madmatt
Copy link
Member

madmatt commented Dec 4, 2023

+1 for patch

@kinglozzer
Copy link
Member Author

Thanks all, PR raised

GuySartorelli added a commit that referenced this issue Dec 4, 2023
FIX: UnsavedRelationList first/last to return null if list is empty (fixes #11083)
@GuySartorelli
Copy link
Member

Merged. Will be automatically tagged and merged up by GitHub actions

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

4 participants