Issues (16)

src/Search/Sort/DocSort.php (1 issue)

1
<?php namespace Nord\Lumen\Elasticsearch\Search\Sort;
2
3
/**
4
 * Allows to add one or more sort on specific fields. Each sort can be reversed as well. The sort is defined on a per
5
 * field level, with special field name for _score to sort by score, and _doc to sort by index order.
6
 *
7
 * The order defaults to desc when sorting on the _score, and defaults to asc when sorting on anything else.
8
 *
9
 * _doc has no real use-case besides being the most efficient sort order. So if you don’t care about the order in which
10
 * documents are returned, then you should sort by _doc. This especially helps when scrolling.
11
 *
12
 * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html
13
 */
14
class DocSort extends AbstractSort
15
{
16
    /**
17
     * @inheritdoc
18
     */
19
    public function toArray()
20
    {
21
        $options = $this->applyOptions([]);
22
23
        if (empty($options)) {
24
            return '_doc';
0 ignored issues
show
Bug Best Practice introduced by
The expression return '_doc' returns the type string which is incompatible with the return type mandated by Illuminate\Contracts\Support\Arrayable::toArray() of array.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
25
        } else {
26
            return ['_doc' => $options];
27
        }
28
    }
29
}
30