Search::getSearchAspects()   A
last analyzed

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 0
1
<?php
2
3
namespace Spatie\Searchable;
4
5
use Illuminate\Foundation\Auth\User;
6
use Illuminate\Support\Arr;
7
8
class Search
9
{
10
    protected $aspects = [];
11
12
    /**
13
     * @param string|\Spatie\Searchable\SearchAspect $searchAspect
14
     *
15
     * @return \Spatie\Searchable\Search
16
     */
17
    public function registerAspect($searchAspect): self
18
    {
19
        if (is_string($searchAspect)) {
20
            $searchAspect = app($searchAspect);
21
        }
22
23
        $this->aspects[$searchAspect->getType()] = $searchAspect;
24
25
        return $this;
26
    }
27
28
    public function registerModel(string $modelClass, ...$attributes): self
29
    {
30
        if (isset($attributes[0]) && is_callable($attributes[0])) {
31
            $attributes = $attributes[0];
32
        }
33
34
        if (is_array(Arr::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...
35
            $attributes = $attributes[0];
36
        }
37
38
        $searchAspect = new ModelSearchAspect($modelClass, $attributes);
39
40
        $this->registerAspect($searchAspect);
41
42
        return $this;
43
    }
44
45
    public function getSearchAspects(): array
46
    {
47
        return $this->aspects;
48
    }
49
50
    public function search(string $query, ?User $user = null): SearchResultCollection
51
    {
52
        return $this->perform($query, $user);
53
    }
54
55
    public function perform(string $query, ?User $user = null): SearchResultCollection
56
    {
57
        $searchResults = new SearchResultCollection();
58
59
        collect($this->getSearchAspects())
60
            ->each(function (SearchAspect $aspect) use ($query, $user, $searchResults) {
61
                $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...
62
            });
63
64
        return $searchResults;
65
    }
66
}
67