JaTinySegmenterTokenizerTest::stringProvider()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 147
Code Lines 119

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 147
rs 8.2857
c 0
b 0
f 0
cc 1
eloc 119
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Onoi\Tesa\Tests;
4
5
use Onoi\Tesa\Tokenizer\JaTinySegmenterTokenizer;
6
7
/**
8
 * @covers \Onoi\Tesa\Tokenizer\JaTinySegmenterTokenizer
9
 * @group onoi-tesa
10
 *
11
 * @license GNU GPL v2+
12
 * @since 0.1
13
 *
14
 * @author mwjames
15
 */
16
class JaTinySegmenterTokenizerTest extends \PHPUnit_Framework_TestCase {
17
18
	public function testCanConstruct() {
19
20
		$tokenizer = $this->getMockBuilder( '\Onoi\Tesa\Tokenizer\Tokenizer' )
21
			->disableOriginalConstructor()
22
			->getMockForAbstractClass();
23
24
		$this->assertInstanceOf(
25
			'\Onoi\Tesa\Tokenizer\JaTinySegmenterTokenizer',
26
			new JaTinySegmenterTokenizer( $tokenizer )
27
		);
28
	}
29
30
	/**
31
	 * @dataProvider stringProvider
32
	 */
33
	public function testTokenize( $string, $expected ) {
34
35
		$instance = new JaTinySegmenterTokenizer();
36
37
		$this->assertEquals(
38
			$expected,
39
			$instance->tokenize( $string )
40
		);
41
	}
42
43
	public function stringProvider() {
44
45
		$provider[] = array(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$provider was never initialized. Although not strictly required by PHP, it is generally a good practice to add $provider = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
46
			'極めてコンパクトな日本語分かち書きソフトウェアです。',
47
			array(
48
				'極め', // should be 極めて
49
				'て',
50
				'コンパクト',
51
				'な',
52
				'日本',
53
				'語分',
54
				'かち',
55
				'書き',
56
				'ソフトウェア',
57
				'です',
58
				'。'
59
			)
60
		);
61
62
		$provider[] = array(
63
			'日本語の新聞記事であれば文字単位で95%程度の精度で分かち書きが行えます。 ',
64
			array(
65
				'日本語',
66
				'の',
67
				'新聞',
68
				'記事',
69
				'で',
70
				'あれ',
71
				'ば',
72
				'文字',
73
				'単位',
74
				'で',
75
				'9',
76
				'5',
77
				'%',
78
				'程度',
79
				'の',
80
				'精度',
81
				'で',
82
				'分かち',
83
				'書き',
84
				'が',
85
				'行え',
86
				'ます',
87
				'。'
88
89
			)
90
		);
91
92
		$provider[] = array(
93
			'私の名前は中野です',
94
			array(
95
				'私',
96
				'の',
97
				'名前',
98
				'は',
99
				'中野',
100
				'です'
101
			)
102
		);
103
104
		$provider[] = array(
105
			'TinySegmenterは25kBで書かれています。',
106
			array(
107
				'TinySegmenter',
108
				'は',
109
				'2',
110
				'5',
111
				'kB',
112
				'で',
113
				'書か',
114
				'れ',
115
				'て',
116
				'い',
117
				'ます',
118
				'。'
119
			)
120
		);
121
122
		$provider[] = array(
123
			'隣の客はAK47振りかざしてギャアギャアわめきたてる客だ。',
124
			array(
125
				'隣',
126
				'の',
127
				'客',
128
				'は',
129
				'AK',
130
				'4',
131
				'7',
132
				'振り',
133
				'かざ', // should be かざし
134
				'し',
135
				'て',
136
				'ギャアギャア',
137
				'わめき',
138
				'た',
139
				'てる',
140
				'客',
141
				'だ',
142
				'。'
143
			)
144
		);
145
146
		// See JaCompoundGroupTokenizerTest for comparison
147
		$provider[] = array(
148
			'と歓声を上げていました。 十勝農業改良普及センターによりますと',
149
			array(
150
				'と',
151
				'歓声',
152
				'を',
153
				'上げ',
154
				'て',
155
				'い',
156
				'まし',
157
				'た',
158
				'。',
159
				'十勝農業',
160
				'改良',
161
				'普及',
162
				'センター',
163
				'により',
164
				'ます',
165
				'と'
166
			)
167
		);
168
169
		// See IcuWordBoundaryTokenizerTest
170
		$provider[] = array(
171
			"公明執ようなSNSもストーカー行為の対象に",
172
			array(
173
				'公明執',
174
				'よう',
175
				'な',
176
				'SNS',
177
				'も',
178
				'ストーカー',
179
				'行為',
180
				'の',
181
				'対象',
182
				'に'
183
			)
184
		);
185
186
		// https://github.com/chezou/TinySegmenter.jl/blob/master/test/timemachineu8j.txt
187
188
		return $provider;
189
	}
190
191
}
192