Passed
Push — next ( d224bd...9e651f )
by Bas
15:29 queued 11:57
created

BuildsSearches::searchView()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 30
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 16
c 1
b 0
f 0
nc 4
nop 3
dl 0
loc 30
ccs 17
cts 17
cp 1
crap 3
rs 9.7333
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>|Expression|string $fields
18
     * @param string $searchText
19
     * @param string|null $analyzer
20
     * @return IlluminateQueryBuilder
21
     */
22 4
    public function searchView(
23
        array|Expression|string $fields,
24
        string $searchText,
25
        string $analyzer = null
0 ignored issues
show
Unused Code introduced by
The parameter $analyzer is not used and could be removed. ( Ignorable by Annotation )

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

25
        /** @scrutinizer ignore-unused */ string $analyzer = null

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
26
    ): IlluminateQueryBuilder {
27
        assert($this->grammar instanceof Grammar);
28
29 4
        if(!is_array($fields)) {
0 ignored issues
show
introduced by
The condition is_array($fields) is always true.
Loading history...
30 1
            $fields = Arr::wrap($fields);
31
        }
32 4
        $fields = $this->grammar->convertJsonFields($fields);
33 4
        $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

33
        /** @scrutinizer ignore-call */ 
34
        $fields = $this->convertIdToKey($fields);
Loading history...
34
35 4
        $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

35
        /** @scrutinizer ignore-call */ 
36
        $searchText = $this->bindValue($searchText, 'search');
Loading history...
36
37 4
        $locale = App::getLocale();
38 4
        $analyzerLocales = ['de','en','es','fi','fr','it','nl','no','pt','ru','sv','zh'];
39
40 4
        $analyzer = 'text_en';
41 4
        if (in_array($locale, $analyzerLocales)) {
42 4
            $analyzer = 'text_' . $locale;
43
        }
44
45 4
        $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...
46 4
            'fields' => $fields,
47 4
            'searchText' => $searchText,
48 4
            'analyzer' => $analyzer
49 4
        ];
50
51 4
        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...
52
    }
53
54
55
    /**
56
     * Add an "order by best matching" clause to the query.
57
     * To be used with the searchView function
58
     *
59
     * @param string $direction
60
     *
61
     * @return IlluminateQueryBuilder
62
     */
63 1
    public function orderByBestMatching($direction = 'DESC'): IlluminateQueryBuilder
64
    {
65 1
        $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

65
        /** @scrutinizer ignore-call */ 
66
        $alias = $this->getTableAlias($this->from);
Loading history...
66 1
        $column = new Expression('BM25(' . $alias . ')');
67
68 1
        $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

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