Test Failed
Push — master ( 53dff4...be6004 )
by Pol
05:20
created

NGramsTrait::doNgrams()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 8
nop 3
crap 4
1
<?php
2
3
namespace drupol\phpngrams;
4
5
use drupol\phpermutations\Iterators\Shift;
6
7
trait NGramsTrait
8
{
9
10
    /**
11
     * @param \Generator $ngrams
12
     * @param string $substring
13
     *
14
     * @return float|int
15
     */
16 2
    public function frequency(\Generator $ngrams, $substring)
17
    {
18 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...
19
20 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...
21 2
            return $n === $substring;
22 2
        }))/count($ngrams);
23
    }
24
25
    /**
26
     * @param $data
27
     * @param int $n
28
     * @param bool $cyclic
29
     *
30
     * @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...
31
     */
32 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...
33
    {
34 12
        $ngrams = [];
35
36 12
        if (is_string($data)) {
37 6
            foreach ($this->doNgrams(str_split($data), $n, $cyclic) as $item) {
38 6
                yield implode('', $item);
39
            }
40
        }
41
42 12
        if (is_array($data)) {
43 8
            foreach ($this->doNgrams($data, $n, $cyclic) as $item) {
44 8
                yield $item;
45
            }
46
        }
47
48 12
        return $ngrams;
49
    }
50
51
    /**
52
     * @param $data
53
     * @param $n
54
     * @param $cyclic
55
     *
56
     * @return \Generator
57
     */
58 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...
59
    {
60 12
        $dataLength = count($data);
61 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...
62 12
        $length = (false === $cyclic ? $dataLength - $n + 1 : $dataLength);
63
64 12
        $shift = new Shift($data);
65
66 12
        for ($j = 0; $j < $length; $j++) {
67 12
            yield array_slice($shift->current(), 0, $n);
68 12
            $shift->rewind();
69
        }
70 12
    }
71
}
72