Completed
Push — master ( 42978d...3648fa )
by Konstantin
04:26
created

FuzzySearchHelper   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 97.22%

Importance

Changes 0
Metric Value
wmc 17
c 0
b 0
f 0
lcom 1
cbo 0
dl 0
loc 96
ccs 35
cts 36
cp 0.9722
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
D findBestMatch() 0 32 9
A findMostSimilarWords() 0 20 4
A findFinalSimilirityAndLevenshtein() 0 12 4
1
<?php
2
3
namespace GeoFixer\helpers;
4
5
/**
6
 * Class FuzzySearchHelper
7
 *
8
 * @package GeoFixer\helpers
9
 */
10
class FuzzySearchHelper
11
{
12
    public $similarity = 0;
13
    public $meta_similarity = 0;
14
    public $min_levenshtein = 1000;
15
    public $meta_min_levenshtein = 1000;
16
17
    /**
18
     * Строгий поиск
19
     *
20
     * @param $word
21
     * @param $translited_words
22
     *
23
     * @return bool|mixed
24
     */
25 9
    public function findBestMatch($word, $translited_words)
26
    {
27 9
        $words_array = [];
28 9
        foreach ($translited_words as $russian => $translit) {
29 9
            if (levenshtein(metaphone($word), metaphone($translit)) < mb_strlen(metaphone($word)) / 2) {
30 2
                if (levenshtein($word, $translit) < mb_strlen($word) / 2) {
31 2
                    $words_array[$russian] = $translit;
32
                }
33
            }
34
        }
35
36 9
        $most_similar = $this->findMostSimilarWords($word, $words_array);
37 9
        $this->findFinalSimilirityAndLevenshtein($most_similar, $word);
38
39 9
        foreach ($most_similar as $russian => $latin) {
40 2
            if (levenshtein(metaphone($latin), metaphone($word)) <= $this->meta_min_levenshtein) {
41 2
                if (similar_text(metaphone($latin), metaphone($word)) >= $this->meta_similarity) {
42 2
                    $meta_result[$russian] = $latin;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$meta_result was never initialized. Although not strictly required by PHP, it is generally a good practice to add $meta_result = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
43
                }
44
            }
45
        }
46
47 9
        if (isset($meta_result)) {
48 2
            if (!is_null(key($meta_result))) {
49 2
                return key($meta_result);
50
            } else {
51
                return false;
52
            }
53
        } else {
54 7
            return false;
55
        }
56
    }
57
58
    /**
59
     * Нестрогий поиск
60
     *
61
     * @param $word
62
     * @param $words_array
63
     * @return array
64
     */
65 19
    public function findMostSimilarWords($word, $words_array)
66
    {
67 19
        $min_levenshtein = $this->min_levenshtein;
68 19
        $max_similarity = $this->similarity;
69
70 19
        $result = [];
71
72 19
        foreach ($words_array as $russian => $translit) {
73 12
            if (levenshtein($translit, $word) <= $min_levenshtein) {
74 12
                if (similar_text($translit, $word) >= $max_similarity) {
75 12
                    $min_levenshtein = levenshtein($translit, $word);
76 12
                    $max_similarity = similar_text($translit, $word);
77 12
                    $result = [];
78 12
                    $result[$russian] = $translit;
79
                }
80
            }
81
        }
82
83 19
        return $result;
84
    }
85
86
    /**
87
     * Вспомогательный метод для findBestMatch
88
     *
89
     * @param $most_similar
90
     * @param $word
91
     */
92 9
    private function findFinalSimilirityAndLevenshtein($most_similar, $word)
93
    {
94 9
        foreach ($most_similar as $item) {
95 2
            $this->meta_min_levenshtein = min($this->meta_min_levenshtein, levenshtein(metaphone($item), metaphone($word)));
96
        }
97
98 9
        foreach ($most_similar as $item) {
99 2
            if (levenshtein($item, $word) == $this->meta_min_levenshtein) {
100 2
                $this->meta_similarity = max($this->meta_similarity, similar_text(metaphone($item), metaphone($word)));
101
            }
102
        }
103 9
    }
104
105
}
106