Test Failed
Branch master (42e418)
by Pol
01:47
created

NGramsTrait   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
cbo 1
dl 0
loc 65
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A frequency() 0 8 1
B ngramsFactory() 0 18 5
A doNgrams() 0 13 4
1
<?php
2
3
namespace drupol\phpngrams;
4
5
use drupol\phpermutations\Iterators\NGrams;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, drupol\phpngrams\NGrams.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
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
        $ngrams = new NGrams($data, $n);
65
66 12
        for ($j = 0; $j < $length; $j++) {
67 12
            yield array_slice($ngrams->current(), 0, $n);
68 12
            $ngrams->rewind();
69
        }
70 12
    }
71
}
72