GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — 2.9 ( 39ef0e...48eca6 )
by Thorsten
14:15
created

PMF_Search_Elasticsearch::search()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 66
Code Lines 40

Duplication

Lines 18
Ratio 27.27 %
Metric Value
dl 18
loc 66
rs 8.8291
cc 4
eloc 40
nc 4
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
use Elasticsearch\Client;
4
5
/**
6
 * phpMyFAQ Elasticsearch based search classes.
7
 *
8
 * PHP Version 5.5
9
 *
10
 * This Source Code Form is subject to the terms of the Mozilla Public License,
11
 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
12
 * obtain one at http://mozilla.org/MPL/2.0/.
13
 *
14
 * @category  phpMyFAQ
15
 * @author    Thorsten Rinne <[email protected]>
16
 * @copyright 2015-2016 phpMyFAQ Team
17
 * @license   http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
18
 * @link      http://www.phpmyfaq.de
19
 * @since     2015-12-25
20
 */
21
if (!defined('IS_VALID_PHPMYFAQ')) {
22
    exit();
23
}
24
25
/**
26
 * PMF_Search_Elasticsearch.
27
 *
28
 * @category  phpMyFAQ
29
 * @author    Thorsten Rinne <[email protected]>
30
 * @copyright 2015-2016 phpMyFAQ Team
31
 * @license   http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
32
 * @link      http://www.phpmyfaq.de
33
 * @since     2015-12-25
34
 */
35
class PMF_Search_Elasticsearch extends PMF_Search_Abstract implements PMF_Search_Interface
36
{
37
    /** @var Client */
38
    private $client = null;
39
40
    /** @var array */
41
    private $esConfig = [];
42
43
    /** @var string */
44
    private $language = '';
45
46
    /** @var array */
47
    private $categoryIds = [];
48
49
    /**
50
     * Constructor.
51
     *
52
     * @param PMF_Configuration
53
     */
54
    public function __construct(PMF_Configuration $config)
55
    {
56
        parent::__construct($config);
57
58
        $this->client = $this->_config->getElasticsearch();
59
        $this->esConfig = $this->_config->getElasticsearchConfig();
60
    }
61
62
    /**
63
     * Prepares the search and executes it.
64
     *
65
     * @param string $searchTerm Search term
66
     *
67
     * @throws PMF_Search_Exception
68
     *
69
     * @return array
70
     */
71
    public function search($searchTerm)
72
    {
73
        if ('' !== $this->getLanguage()) {
74
            $languageFilter = [
75
                'term' => [
76
                    'lang' => $this->getLanguage()
77
                ]
78
            ];
79
        } else {
80
            $languageFilter = '';
81
        }
82
83
        $searchParams = [
84
            'index' => $this->esConfig['index'],
85
            'type' => $this->esConfig['type'],
86
            'size' => 1000,
87
            'body' => [
88
                'query' => [
89
                    'filtered' => [
90
                        'filter' => [
91
                            'bool' => [
92
                                'must' => [
93
                                    [
94
                                        'terms' => [ 'category_id' => $this->getCategoryIds() ]
95
                                    ],
96
                                    $languageFilter
97
                                ]
98
                            ]
99
                        ],
100
                        'query' => [
101
                            'bool' => [
102
                                'should' => [
103
                                    [ 'match' => [ 'question' => $searchTerm ] ],
104
                                    [ 'match' => [ 'answer' => $searchTerm ] ],
105
                                    [ 'match' => [ 'keywords' => $searchTerm ] ]
106
                                ]
107
                            ]
108
                        ]
109
                    ]
110
                ]
111
            ]
112
        ];
113
114
        $result = $this->client->search($searchParams);
115
116 View Code Duplication
        if (0 !== $result['hits']['total']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
117
118
            foreach ($result['hits']['hits'] as $hit) {
119
                $resultSet = new stdClass();
120
                $resultSet->id = $hit['_source']['id'];
121
                $resultSet->lang = $hit['_source']['lang'];
122
                $resultSet->question = $hit['_source']['question'];
123
                $resultSet->answer = $hit['_source']['answer'];
124
                $resultSet->keywords = $hit['_source']['keywords'];
125
                $resultSet->category_id = $hit['_source']['category_id'];
126
                $resultSet->score = $hit['_score'];
127
128
                $this->resultSet[] = $resultSet;
129
            }
130
131
        } else {
132
            $this->resultSet = [];
0 ignored issues
show
Documentation Bug introduced by
It seems like array() of type array is incompatible with the declared type resource of property $resultSet.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
133
        }
134
135
        return $this->resultSet;
136
    }
137
138
    /**
139
     * Prepares the autocomplete search and executes it.
140
     *
141
     * @param string $searchTerm Search term for autocompletion
142
     *
143
     * @throws PMF_Search_Exception
144
     *
145
     * @return array
146
     */
147
    public function autoComplete($searchTerm)
148
    {
149
        $searchParams = [
150
            'index' => $this->esConfig['index'],
151
            'type' => $this->esConfig['type'],
152
            'size' => 1000,
153
            'body' => [
154
                'query' => [
155
                    'filtered' => [
156
                        'filter' => [
157
                            'term' => [
158
                                'lang' => $this->getLanguage()
159
                            ]
160
                        ],
161
                        'query' => [
162
                            'bool' => [
163
                                'should' => [
164
                                    [ 'match' => [ 'question' => $searchTerm ] ],
165
                                    [ 'match' => [ 'answer' => $searchTerm ] ],
166
                                    [ 'match' => [ 'keywords' => $searchTerm ] ]
167
                                ]
168
                            ]
169
                        ]
170
                    ]
171
                ]
172
            ]
173
        ];
174
175
        $result = $this->client->search($searchParams);
176
177 View Code Duplication
        if (0 !== $result['hits']['total']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
178
179
            foreach ($result['hits']['hits'] as $hit) {
180
                $resultSet = new stdClass();
181
                $resultSet->id = $hit['_source']['id'];
182
                $resultSet->lang = $hit['_source']['lang'];
183
                $resultSet->question = $hit['_source']['question'];
184
                $resultSet->answer = $hit['_source']['answer'];
185
                $resultSet->keywords = $hit['_source']['keywords'];
186
                $resultSet->category_id = $hit['_source']['category_id'];
187
                $resultSet->score = $hit['_score'];
188
189
                $this->resultSet[] = $resultSet;
190
            }
191
192
        } else {
193
            $this->resultSet = [];
0 ignored issues
show
Documentation Bug introduced by
It seems like array() of type array is incompatible with the declared type resource of property $resultSet.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
194
        }
195
196
        return $this->resultSet;
197
    }
198
199
    /**
200
     * Returns the current category ID
201
     *
202
     * @return array
203
     */
204
    public function getCategoryIds()
205
    {
206
        return $this->categoryIds;
207
    }
208
209
    /**
210
     * Sets the current category ID
211
     *
212
     * @param array $categoryIds
213
     */
214
    public function setCategoryIds(Array $categoryIds)
215
    {
216
        $this->categoryIds = $categoryIds;
217
    }
218
219
    /**
220
     * Returns the current language, empty string if all languages
221
     *
222
     * @return string
223
     */
224
    public function getLanguage()
225
    {
226
        return $this->language;
227
    }
228
229
    /**
230
     * Sets the current language
231
     *
232
     * @param string $language
233
     */
234
    public function setLanguage($language)
235
    {
236
        $this->language = $language;
237
    }
238
}
239