Completed
Push — master ( b71f7d...42978d )
by Konstantin
02:00
created

FuzzySearchHelper   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 0
dl 0
loc 89
ccs 44
cts 44
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
C findBestMatch() 0 24 8
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
13
    public $similarity = 0;
14
    public $meta_similarity = 0;
15
    public $min_levenshtein = 1000;
16
    public $meta_min_levenshtein = 1000;
17
18
    /**
19
     * Строгий поиск
20
     *
21
     * @param $word
22
     * @param $translited_words
23
     *
24
     * @return bool|mixed
25
     */
26 9
    public function findBestMatch($word, $translited_words)
27
    {
28 9
        $words_array = [];
29 9
        foreach ($translited_words as $russian => $translit) {
30 9
            if (levenshtein(metaphone($word), metaphone($translit)) < mb_strlen(metaphone($word)) / 2) {
31 2
                if (levenshtein($word, $translit) < mb_strlen($word) / 2) {
32 2
                    $words_array[$russian] = $translit;
33 2
                }
34 2
            }
35 9
        }
36
37 9
        $most_similar = $this->findMostSimilarWords($word, $words_array);
38 9
        $this->findFinalSimilirityAndLevenshtein($most_similar, $word);
39
40 9
        foreach ($most_similar as $russian => $latin) {
41 2
            if (levenshtein(metaphone($latin), metaphone($word)) <= $this->meta_min_levenshtein) {
42 2
                if (similar_text(metaphone($latin), metaphone($word)) >= $this->meta_similarity) {
43 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...
44 2
                }
45 2
            }
46 9
        }
47
48 9
        return @key($meta_result) ? @key($meta_result) : false;
49
    }
50
51
    /**
52
     * Нестрогий поиск
53
     *
54
     * @param $word
55
     * @param $words_array
56
     * @return array
57
     */
58 19
    public function findMostSimilarWords($word, $words_array)
59
    {
60 19
        $this->min_levenshtein = 1000;
61 19
        $this->similarity = 0;
62
63 19
        $result = [];
64
65 19
        foreach ($words_array as $russian => $translit) {
66 12
            if (levenshtein($translit, $word) <= $this->min_levenshtein) {
67 12
                if (similar_text($translit, $word) >= $this->similarity) {
68 12
                    $this->min_levenshtein = levenshtein($translit, $word);
69 12
                    $this->similarity = similar_text($translit, $word);
70 12
                    $result = [];
71 12
                    $result[$russian] = $translit;
72 12
                }
73 12
            }
74 19
        }
75
76 19
        return $result;
77
    }
78
79
    /**
80
     * Вспомогательный метод для findBestMatch
81
     *
82
     * @param $most_similar
83
     * @param $word
84
     */
85 9
    private function findFinalSimilirityAndLevenshtein($most_similar, $word)
86
    {
87 9
        foreach ($most_similar as $item) {
88 2
            $this->meta_min_levenshtein = min($this->meta_min_levenshtein, levenshtein(metaphone($item), metaphone($word)));
89 9
        }
90
91 9
        foreach ($most_similar as $item) {
92 2
            if (levenshtein($item, $word) == $this->meta_min_levenshtein) {
93 2
                $this->meta_similarity = max($this->meta_similarity, similar_text(metaphone($item), metaphone($word)));
94 2
            }
95 9
        }
96 9
    }
97
98
}