Conditions | 7 |
Paths | 10 |
Total Lines | 43 |
Code Lines | 16 |
Lines | 0 |
Ratio | 0 % |
Tests | 17 |
CRAP Score | 7 |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
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 | } |
||
52 |