Levenshtein   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
dl 0
loc 45
ccs 17
cts 17
cp 1
rs 10
c 1
b 0
f 0
wmc 7

1 Method

Rating   Name   Duplication   Size   Complexity  
B closest() 0 43 7
1
<?php
2
3
namespace FaizShukri\Quran\Supports;
4
5
class Levenshtein
6
{
7 16
    public function closest($input, array $words)
8
    {
9
        // no shortest distance found, yet
10 16
        $shortest = -1;
11 16
        $match = [];
12
13
        // loop through words to find the closest
14 16
        foreach ($words as $word) {
15
16
            // calculate the distance between the input word,
17
            // and the current word
18 16
            $lev = levenshtein(strtolower($input), strtolower($word), 1, 2, 3);
19
20
            // check for an exact match
21 16
            if ($lev == 0) {
22
23
                // closest word is this one (exact match)
24 2
                $match = [$word];
25
                // $closest = $word;
26 2
                $shortest = 0;
27
28
                // break out of the loop; we've found an exact match
29 2
                break;
30
            }
31
32
            // if this distance is less than the next found shortest
33
            // distance, OR if a next shortest word has not yet been found
34
35 14
            if ($lev < $shortest || $shortest < 0) {
36
                // set the closest match, and shortest distance
37 14
                $match = [$word];
38
                // $closest = $word;
39 14
                $shortest = $lev;
40 14
            } elseif ($lev == $shortest) {
41 11
                $match[] = $word;
42
            }
43
        }
44
45 16
        if ($shortest > 6) {
46 4
            return [];
47
        }
48
49 12
        return $match;
50
    }
51
}
52