Test Failed
Push — master ( 598b28...0e6f4c )
by Pol
14:07
created

NGramsTrait::frequency()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 0
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 2
1
<?php
2
3
namespace drupol\phpngrams;
4
5
trait NGramsTrait
6
{
7
    /**
8
     * @param array $data
9
     * @param int $n
10
     * @param bool $cyclic
11
     *
12
     * @return \Generator
13
     */
14 3
    public function ngramsArray(array $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...
15
    {
16 3
        return $this->doNgrams($data, $n, $cyclic);
17
    }
18
19
    /**
20
     * @param string $data
21
     * @param int $n
22
     * @param bool $cyclic
23
     *
24
     * @return \Generator
25
     */
26 3
    public function ngramsString($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...
27
    {
28 3
        foreach ($this->doNgrams(str_split($data), $n, $cyclic) as $data) {
29 3
            yield implode('', $data);
30
        }
31 3
    }
32
33
    /**
34
     * @param $data
35
     * @param $n
36
     * @param $cyclic
37
     *
38
     * @return \Generator
39
     */
40 6
    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...
41
    {
42 6
        $dataLength = count($data);
43
44 6
        $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...
45
46 6
        $length = (false === $cyclic ? $dataLength - $n + 1 : $dataLength);
47
48 6
        for ($j = 0; $j < $length; $j++) {
49 6
            $ngrams = [];
50 6
            for ($i = $j; $i < $n + $j; $i++) {
51 6
                $ngrams[] = $data[$i%$dataLength];
52
            }
53 6
            yield $ngrams;
54
        }
55 6
    }
56
57
    /**
58
     * @param \Generator $ngrams
59
     * @param string $substring
60
     *
61
     * @return float|int
62
     */
63
    public function frequency(\Generator $ngrams, $substring) {
64
        $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...
65
66
        return count(array_filter($ngrams, function ($n) use ($substring) { return $n === $substring; }))/count($ngrams);
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...
Coding Style introduced by
Opening brace must be the last content on the line
Loading history...
Coding Style introduced by
It is generally recommended to place each PHP statement on a line by itself.

Let’s take a look at an example:

// Bad
$a = 5; $b = 6; $c = 7;

// Good
$a = 5;
$b = 6;
$c = 7;
Loading history...
67
    }
68
}
69