Test Failed
Push — master ( e299fb...38e963 )
by Pol
06:27
created

NGramsTrait::ngramsFactory()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 18
c 0
b 0
f 0
ccs 9
cts 9
cp 1
rs 8.8571
nc 4
cc 5
eloc 9
nop 3
crap 5
1
<?php
2
3
namespace drupol\phpngrams;
4
5
trait NGramsTrait
6
{
7
8
    /**
9
     * @param \Generator $ngrams
10
     * @param string $substring
11
     *
12
     * @return float|int
13
     */
14 2
    public function frequency(\Generator $ngrams, $substring)
15
    {
16 2
        $ngrams = iterator_to_array($ngrams);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $ngrams. This often makes code more readable.
Loading history...
17
18 2
        return count(array_filter($ngrams, function ($n) use ($substring) {
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $n. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
19 2
            return $n === $substring;
20 2
        }))/count($ngrams);
21
    }
22
23
    /**
24
     * @param $data
25
     * @param int $n
26
     * @param bool $cyclic
27
     *
28
     * @return bool|\Generator
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use \Generator.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
29
     */
30 12
    public function ngramsFactory($data, $n = 1, $cyclic = true)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $n. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
31
    {
32 12
        $ngrams = [];
33
34 12
        if (is_string($data)) {
35 6
            foreach ($this->doNgrams(str_split($data), $n, $cyclic) as $item) {
36 6
                yield implode('', $item);
37
            }
38
        }
39
40 12
        if (is_array($data)) {
41 8
            foreach ($this->doNgrams($data, $n, $cyclic) as $item) {
42 8
                yield $item;
43
            }
44
        }
45
46 12
        return $ngrams;
47
    }
48
49
    /**
50
     * @param $data
51
     * @param $n
52
     * @param $cyclic
53
     *
54
     * @return \Generator
55
     */
56 12
    private function doNgrams($data, $n = 1, $cyclic = true)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $n. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
57
    {
58 12
        $dataLength = count($data);
59 12
        $n = $n > $dataLength ? $dataLength : $n;
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $n. This often makes code more readable.
Loading history...
60 12
        $length = (false === $cyclic ? $dataLength - $n + 1 : $dataLength);
61
62 12
        for ($j = 0; $j < $length; $j++) {
63 12
            $ngrams = [];
64 12
            for ($i = $j; $i < $n + $j; $i++) {
65 12
                $ngrams[] = $data[$i%$dataLength];
66
            }
67 12
            yield $ngrams;
68
        }
69 12
    }
70
}
71