Completed
Push — master ( f4b83a...43b3ad )
by
unknown
01:16
created

Search::search()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Spatie\Searchable;
4
5
use Illuminate\Foundation\Auth\User;
6
7
class Search
8
{
9
    protected $aspects = [];
10
11
    /**
12
     * @param string|\Spatie\Searchable\SearchAspect $searchAspect
13
     *
14
     * @return \Spatie\Searchable\Search
15
     */
16
    public function registerAspect($searchAspect): self
17
    {
18
        if (is_string($searchAspect)) {
19
            $searchAspect = app($searchAspect);
20
        }
21
22
        $this->aspects[$searchAspect->getType()] = $searchAspect;
23
24
        return $this;
25
    }
26
27
    public function registerModel(string $modelClass, ...$attributes): self
28
    {
29
        if (isset($attributes[0]) && is_callable($attributes[0])) {
30
            $attributes = $attributes[0];
31
        }
32
33
        if (is_array(array_get($attributes, 0))) {
0 ignored issues
show
Documentation introduced by
$attributes is of type callable, but the function expects a object<ArrayAccess>|array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
34
            $attributes = $attributes[0];
35
        }
36
37
        $searchAspect = new ModelSearchAspect($modelClass, $attributes);
38
39
        $this->registerAspect($searchAspect);
40
41
        return $this;
42
    }
43
44
    public function getSearchAspects(): array
45
    {
46
        return $this->aspects;
47
    }
48
49
    public function search(string $query, ?User $user = null): SearchResultCollection
50
    {
51
        return $this->perform($query, $user);
52
    }
53
54
    public function perform(string $query, ?User $user = null): SearchResultCollection
55
    {
56
        $searchResults = new SearchResultCollection();
57
58
        collect($this->getSearchAspects())
59
            ->each(function (SearchAspect $aspect) use ($query, $user, $searchResults) {
60
                $searchResults->addResults($aspect->getType(), $aspect->getResults($query, $user));
0 ignored issues
show
Unused Code introduced by
The call to SearchAspect::getResults() has too many arguments starting with $user.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
61
            });
62
63
        return $searchResults;
64
    }
65
}
66