Completed
Pull Request — master (#144)
by Mantas
63:58
created

FuzzySearch   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 66
Duplicated Lines 25.76 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 1
cbo 5
dl 17
loc 66
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
B modifySearch() 17 17 5
A getParameters() 0 4 1
A setFuzziness() 0 4 1
A setPrefixLength() 0 4 1
A setMaxExpansions() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/*
4
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ONGR\FilterManagerBundle\Filter\Widget\Search;
13
14
use ONGR\ElasticsearchDSL\Query\BoolQuery;
15
use ONGR\ElasticsearchDSL\Query\FuzzyQuery;
16
use ONGR\ElasticsearchDSL\Search;
17
use ONGR\FilterManagerBundle\Filter\FilterState;
18
use ONGR\FilterManagerBundle\Search\SearchRequest;
19
20
/**
21
 * This class runs match search.
22
 */
23
class FuzzySearch extends AbstractSingleValue
24
{
25
    /**
26
     * @var array Fuzzy query parameters.
27
     */
28
    private $parameters = [];
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 View Code Duplication
    public function modifySearch(Search $search, FilterState $state = null, SearchRequest $request = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
34
    {
35
        if ($state && $state->isActive()) {
36
            if (strpos($this->getField(), ',') !== false) {
37
                $subQuery = new BoolQuery();
38
                foreach (explode(',', $this->getField()) as $field) {
39
                    $subQuery->add(new FuzzyQuery($field, $state->getValue(), $this->getParameters()), 'should');
40
                }
41
                $search->addQuery($subQuery, 'must');
42
            } else {
43
                $search->addQuery(
44
                    new FuzzyQuery($this->getField(), $state->getValue(), $this->getParameters()),
45
                    'must'
46
                );
47
            }
48
        }
49
    }
50
51
    /**
52
     * @return array
53
     */
54
    public function getParameters()
55
    {
56
        return array_filter($this->parameters);
57
    }
58
59
    /**
60
     * The maximum edit distance.
61
     *
62
     * @param string|int|float $fuzziness
63
     */
64
    public function setFuzziness($fuzziness)
65
    {
66
        $this->parameters['fuzziness'] = $fuzziness;
67
    }
68
69
    /**
70
     * The number of initial characters which will not be “fuzzified”.
71
     *
72
     * @param int $prefixLength
73
     */
74
    public function setPrefixLength($prefixLength)
75
    {
76
        $this->parameters['prefix_length'] = $prefixLength;
77
    }
78
79
    /**
80
     * The maximum number of terms that the fuzzy query will expand to.
81
     *
82
     * @param int $maxExpansions
83
     */
84
    public function setMaxExpansions($maxExpansions)
85
    {
86
        $this->parameters['max_expansions'] = $maxExpansions;
87
    }
88
}
89