Passed
Pull Request — master (#82)
by Sam
02:27
created

lexicalDistance()   C

Complexity

Conditions 12
Paths 26

Size

Total Lines 39
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 20
nc 26
nop 2
dl 0
loc 39
rs 5.1612
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Digia\GraphQL\Util;
4
5
/**
6
 * @param string $input
7
 * @param array  $options
8
 * @return array
9
 */
10
function suggestionList(string $input, array $options): array
11
{
12
    $optionsByDistance = [];
13
    $oLength = count($options);
14
    $inputThreshold = strlen($input) / 2;
15
16
    /** @noinspection ForeachInvariantsInspection */
17
    for ($i = 0; $i < $oLength; $i++) {
18
        $distance = levenshtein($input, $options[$i]);
19
        $threshold = max($inputThreshold, strlen($options[$i]) / 2, 1);
20
        if ($distance <= $threshold) {
21
            $optionsByDistance[$options[$i]] = $distance;
22
        }
23
    }
24
25
    $result = array_keys($optionsByDistance);
26
27
    usort($result, function ($a, $b) use ($optionsByDistance) {
28
        return $optionsByDistance[$a] - $optionsByDistance[$b];
29
    });
30
31
    return $result;
32
}
33