Failed Conditions
Push — refactor/improve-static-analys... ( bdf823...46faab )
by Bas
10:08
created

BuildsSearches::orderByFrequency()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 1
nc 1
nop 1
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
12
trait BuildsSearches
13
{
14
    /**
15
     * Search an ArangoSearch view.
16
     */
17
    public function searchView(
18
        array|Expression|string $fields,
19
        string $searchText,
20
        string $analyzer = null
21
    ): IlluminateQueryBuilder {
22
        if(!is_array($fields)) {
0 ignored issues
show
introduced by
The condition is_array($fields) is always true.
Loading history...
23
            $fields = Arr::wrap($fields);
24
        }
25
        $fields = $this->grammar->convertJsonFields($fields);
26
        $fields = $this->convertIdToKey($fields);
0 ignored issues
show
Bug introduced by
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

26
        /** @scrutinizer ignore-call */ 
27
        $fields = $this->convertIdToKey($fields);
Loading history...
27
28
        $searchText = $this->bindValue($searchText, 'search');
0 ignored issues
show
Bug introduced by
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

28
        /** @scrutinizer ignore-call */ 
29
        $searchText = $this->bindValue($searchText, 'search');
Loading history...
29
30
        $locale = App::getLocale();
31
        $analyzerLocales = ['de','en','es','fi','fr','it','nl','no','pt','ru','sv','zh'];
32
        if ($analyzer === null && in_array($locale, $analyzerLocales)) {
33
            $analyzer = 'text_' . $locale;
34
        } else {
35
            $analyzer = 'text_en';
36
        }
37
38
        $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...
39
            'fields' => $fields,
40
            'searchText' => $searchText,
41
            'analyzer' => $analyzer
42
        ];
43
44
        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...
45
    }
46
47
48
    /**
49
     * Add an "order by best matching" clause to the query.
50
     * To be used with the searchView function
51
     *
52
     * @param string $direction
53
     *
54
     * @return IlluminateQueryBuilder
55
     */
56
    public function orderByBestMatching($direction = 'DESC'): IlluminateQueryBuilder
57
    {
58
        $alias = $this->getTableAlias($this->from);
0 ignored issues
show
Bug introduced by
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

58
        /** @scrutinizer ignore-call */ 
59
        $alias = $this->getTableAlias($this->from);
Loading history...
59
        $column = new Expression('BM25(' . $alias . ')');
60
61
        $this->orderBy($column, $direction);
0 ignored issues
show
Bug introduced by
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

61
        $this->/** @scrutinizer ignore-call */ 
62
               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...
62
63
        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...
64
    }
65
66
    /**
67
     * Add an "order by best matching" clause to the query.
68
     * To be used with the searchView function
69
     *
70
     * @param string $direction
71
     *
72
     * @return IlluminateQueryBuilder
73
     */
74
    public function orderByFrequency($direction = 'DESC'): IlluminateQueryBuilder
75
    {
76
        $alias = $this->getTableAlias($this->from);
77
        $column = new Expression('TFIDF(' . $alias . ')');
78
79
        $this->orderBy($column, $direction);
80
81
        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...
82
    }
83
}
84