CdbStopwordAnalyzer   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 93.33%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 2
dl 0
loc 111
ccs 28
cts 30
cp 0.9333
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A isAvailable() 0 3 1
A getLocation() 0 3 1
A getTargetByLanguage() 0 3 1
A isStopWord() 0 8 3
B createCdbByLanguage() 0 27 4
1
<?php
2
3
namespace Onoi\Tesa\StopwordAnalyzer;
4
5
use Cdb\Reader;
6
use Cdb\Writer;
7
use Exception;
8
use RuntimeException;
9
10
/**
11
 * @license GNU GPL v2+
12
 * @since 0.1
13
 *
14
 * @author mwjames
15
 */
16
class CdbStopwordAnalyzer implements StopwordAnalyzer {
17
18
	/**
19
	 * Any change to the content of its data files should be reflected in a
20
	 * version change (the version number does not necessarily correlate with
21
	 * the library version)
22
	 */
23
	const VERSION = '0.1.cdb';
24
25
	/**
26
	 * @var Cdb
27
	 */
28
	private $cdb;
29
30
	/**
31
	 * @since 0.1
32
	 *
33
	 * @param string $target
34
	 */
35 12
	public function __construct( $target ) {
36
		try {
37 12
			$this->cdb = Reader::open( $target );
0 ignored issues
show
Documentation Bug introduced by
It seems like \Cdb\Reader::open($target) of type object<Cdb\Reader\DBA> or object<Cdb\Reader\PHP> is incompatible with the declared type object<Onoi\Tesa\StopwordAnalyzer\Cdb> of property $cdb.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
38 12
		} catch( Exception $e ) {
39
			// Do nothing
40
		}
41 12
	}
42
43
	/**
44
	 * @since 0.1
45
	 *
46
	 * @return boolean
47
	 */
48
	public function isAvailable() {
49
		return $this->cdb !== null;
50
	}
51
52
	/**
53
	 * @since 0.1
54
	 *
55
	 * @param string $language
0 ignored issues
show
Bug introduced by
There is no parameter named $language. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
56
	 *
57
	 * @return string
58
	 */
59 21
	public static function getLocation() {
60 21
		return str_replace( array( '\\', '/' ), DIRECTORY_SEPARATOR, __DIR__ . '/data/' );
61
	}
62
63
	/**
64
	 * @since 0.1
65
	 *
66
	 * @param string $language
67
	 *
68
	 * @return string
69
	 */
70 20
	public static function getTargetByLanguage( $language ) {
71 20
		return self::getLocation() . 'cdb/' . strtolower( $language )  . '.cdb';
72
	}
73
74
	/**
75
	 * @since 0.1
76
	 *
77
	 * @param string $word
78
	 *
79
	 * @return boolean
80
	 */
81 12
	public function isStopWord( $word ) {
82
83 12
		if ( $this->cdb !== null && $this->cdb->get( $word ) !== false ) {
84 11
			return true;
85
		}
86
87 5
		return false;
88
	}
89
90
	/**
91
	 * @since 0.1
92
	 *
93
	 * @param string $location
94
	 * @param string $language
95
	 *
96
	 * @return boolean
97
	 */
98 11
	public static function createCdbByLanguage( $location, $language ) {
99
100 11
		$language = strtolower( $language );
101 11
		$source = $location . $language . '.json';
102
103 11
		if ( !file_exists( $source ) ) {
104 1
			throw new RuntimeException( "{$source} is not available." );
105
		}
106
107 10
		$contents = json_decode( file_get_contents( $source ), true );
108
109 10
		if ( !isset( $contents['list'] ) ) {
110 2
			throw new RuntimeException( "JSON is missing the `list` index." );
111
		}
112
113 8
		$writer = Writer::open(
114 8
			self::getTargetByLanguage( $language )
115 8
		);
116
117 8
		foreach ( $contents['list'] as $words ) {
118 8
			$writer->set( trim( $words ), true );
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
119 8
		}
120
121 8
		$writer->close();
122
123 8
		return true;
124
	}
125
126
}
127