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

Error in ItemList->getFirstItem() #26

Open
pavel-mironchik opened this issue Oct 2, 2017 · 1 comment
Open

Error in ItemList->getFirstItem() #26

pavel-mironchik opened this issue Oct 2, 2017 · 1 comment
Assignees

Comments

@pavel-mironchik
Copy link

Method getFirstItem() uses results of method getItems()

$items = $this->getItems(...$types);

and if all ok, then get element with index 0

return $items[0];

Here is the link to this line in code

But method getItems() uses function array_filter(), which preserves the array's keys. So, for example, if we have

ItemList $items [
    0 => type 'Breadcrumb'
    1 => type 'Breadcrumb'
    2 => type 'Product'
]

then after $items->getItems('Product') it will become

ItemList $items [
   2 => type 'Product'
]

and $items[0] will return null.

I suggest to use

public function getFirstItem(...$types)
    {
        $items = array_values($this->getItems(...$types));

or

public function getItems(...$types)
    {
        ...

        return array_values($this->items);
@pavel-mironchik
Copy link
Author

It seems that getItems() used as well in method getItemByTypeAndIndex(), where index is valuable. So we can do like this:

public function getFirstItem(...$types)
    {
        $items = array_slice($this->getItems(...$types), 0, 1);

@jkphl jkphl self-assigned this Oct 2, 2017
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