Issues (364)

src/Query/Concerns/BuildsSearches.php (14 issues)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace LaravelFreelancerNL\Aranguent\Query\Concerns;
6
7
use Illuminate\Database\Query\Builder as IlluminateQueryBuilder;
8
use Illuminate\Database\Query\Expression;
9
use Illuminate\Support\Arr;
10
use Illuminate\Support\Facades\App;
11
use LaravelFreelancerNL\Aranguent\Query\Grammar;
12
13
trait BuildsSearches
14
{
15
    /**
16
     * Search an ArangoSearch view.
17
     * @param array<mixed>|string $fields
18
     * @param string $searchText
19
     * @param string|null $analyzer
20
     * @return IlluminateQueryBuilder
21
     */
22 6
    public function searchView(
23
        $fields,
24
        $searchText,
25
        $analyzer = null,
26
    ): IlluminateQueryBuilder {
27
        assert($this->grammar instanceof Grammar);
28
29 6
        if (!is_array($fields)) {
30 2
            $fields = Arr::wrap($fields);
31
        }
32 6
        $fields = $this->grammar->convertJsonFields($fields);
33 6
        $fields = $this->convertIdToKey($fields);
0 ignored issues
show
It seems like convertIdToKey() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

33
        /** @scrutinizer ignore-call */ 
34
        $fields = $this->convertIdToKey($fields);
Loading history...
34
35 6
        $searchText = $this->bindValue($searchText, 'search');
0 ignored issues
show
It seems like bindValue() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

35
        /** @scrutinizer ignore-call */ 
36
        $searchText = $this->bindValue($searchText, 'search');
Loading history...
36
37 6
        $locale = App::getLocale();
38 6
        $analyzerLocales = ['de','en','es','fi','fr','it','nl','no','pt','ru','sv','zh'];
39
40
41 6
        if (!$analyzer && in_array($locale, $analyzerLocales)) {
42 5
            $analyzer = 'text_' . $locale;
43
        }
44 6
        if (!$analyzer) {
45
            $analyzer = 'text_en';
46
        }
47
48 6
        $this->search = [
0 ignored issues
show
Bug Best Practice introduced by
The property search does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
49 6
            'fields' => $fields,
50 6
            'searchText' => $searchText,
51 6
            'analyzer' => $analyzer,
52 6
        ];
53
54 6
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type LaravelFreelancerNL\Aran...Concerns\BuildsSearches which is incompatible with the type-hinted return Illuminate\Database\Query\Builder.
Loading history...
55
    }
56
57
    /**
58
     * Search an ArangoSearch view ith a raw search expression.
59
     *
60
     * @param Expression|string $rawSearch
61
     * @param array<mixed> $bindings
62
     * @return IlluminateQueryBuilder
63
     */
64 1
    public function rawSearchView(
65
        $rawSearch,
66
        $bindings = [],
67
    ): IlluminateQueryBuilder {
68
        assert($this->grammar instanceof Grammar);
69
70 1
        if (is_string($rawSearch)) {
71 1
            $rawSearch = new Expression($rawSearch);
0 ignored issues
show
$rawSearch of type string is incompatible with the type Illuminate\Database\Query\TValue expected by parameter $value of Illuminate\Database\Quer...pression::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

71
            $rawSearch = new Expression(/** @scrutinizer ignore-type */ $rawSearch);
Loading history...
72
        }
73
74 1
        $this->search = [
0 ignored issues
show
Bug Best Practice introduced by
The property search does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
75 1
            'expression' => $rawSearch,
76 1
        ];
77
78 1
        $this->bindings['search'] = $bindings;
0 ignored issues
show
Bug Best Practice introduced by
The property bindings does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
79
80 1
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type LaravelFreelancerNL\Aran...Concerns\BuildsSearches which is incompatible with the type-hinted return Illuminate\Database\Query\Builder.
Loading history...
81
    }
82
83
84
    /**
85
     * Add an "order by best matching" clause to the query.
86
     * To be used with the searchView function
87
     *
88
     * @param string $direction
89
     *
90
     * @return IlluminateQueryBuilder
91
     */
92 3
    public function orderByBestMatching($direction = 'DESC'): IlluminateQueryBuilder
93
    {
94 3
        $alias = $this->getTableAlias($this->from);
0 ignored issues
show
It seems like getTableAlias() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

94
        /** @scrutinizer ignore-call */ 
95
        $alias = $this->getTableAlias($this->from);
Loading history...
95 3
        $column = new Expression('BM25(' . $alias . ')');
0 ignored issues
show
'BM25(' . $alias . ')' of type string is incompatible with the type Illuminate\Database\Query\TValue expected by parameter $value of Illuminate\Database\Quer...pression::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

95
        $column = new Expression(/** @scrutinizer ignore-type */ 'BM25(' . $alias . ')');
Loading history...
96
97 3
        $this->orderBy($column, $direction);
0 ignored issues
show
The method orderBy() does not exist on LaravelFreelancerNL\Aran...Concerns\BuildsSearches. Did you maybe mean orderByBestMatching()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

97
        $this->/** @scrutinizer ignore-call */ 
98
               orderBy($column, $direction);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
98
99 3
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type LaravelFreelancerNL\Aran...Concerns\BuildsSearches which is incompatible with the type-hinted return Illuminate\Database\Query\Builder.
Loading history...
100
    }
101
102
    /**
103
     * Add an "order by best matching" clause to the query.
104
     * To be used with the searchView function
105
     *
106
     * @param string $direction
107
     *
108
     * @return IlluminateQueryBuilder
109
     */
110 1
    public function orderByFrequency($direction = 'DESC'): IlluminateQueryBuilder
111
    {
112 1
        $alias = $this->getTableAlias($this->from);
113 1
        $column = new Expression('TFIDF(' . $alias . ')');
0 ignored issues
show
'TFIDF(' . $alias . ')' of type string is incompatible with the type Illuminate\Database\Query\TValue expected by parameter $value of Illuminate\Database\Quer...pression::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

113
        $column = new Expression(/** @scrutinizer ignore-type */ 'TFIDF(' . $alias . ')');
Loading history...
114
115 1
        $this->orderBy($column, $direction);
116
117 1
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type LaravelFreelancerNL\Aran...Concerns\BuildsSearches which is incompatible with the type-hinted return Illuminate\Database\Query\Builder.
Loading history...
118
    }
119
}
120