Conditions | 1 |
Paths | 1 |
Total Lines | 88 |
Code Lines | 53 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
84 | public function stringProvider() { |
||
85 | |||
86 | $provider[] = array( |
||
|
|||
87 | "安全テスト", |
||
88 | array( '安全', 'テスト' ) |
||
89 | ); |
||
90 | |||
91 | // Would expect 'すもも', 'も', 'もも', 'も', 'もも', 'の', 'うち', '。' |
||
92 | $provider[] = array( |
||
93 | "すもももももももものうち。", |
||
94 | array( 'すもも', 'も', 'も', 'も', 'も', 'も', 'もの', 'うち', '。' ) |
||
95 | ); |
||
96 | |||
97 | $provider[] = array( |
||
98 | "李も桃も桃のうち。", |
||
99 | array( '李', 'も', '桃', 'も', '桃', 'の', 'うち', '。' ) |
||
100 | ); |
||
101 | |||
102 | $provider[] = array( |
||
103 | "إسرائيل", |
||
104 | array( 'إسرائيل' ) |
||
105 | ); |
||
106 | |||
107 | $provider[] = array( |
||
108 | "검색엔ㅇㅏ진", |
||
109 | array( '검색엔', 'ㅇㅏ', '진' ) |
||
110 | ); |
||
111 | |||
112 | $provider[] = array( |
||
113 | "검색엔ㅇㅏ진1234abcdfrA", |
||
114 | array( '검색엔', 'ㅇㅏ', '진', '1234abcdfrA' ) |
||
115 | ); |
||
116 | |||
117 | $provider[] = array( |
||
118 | "1234abcdfrA", |
||
119 | array( '1234abcdfrA' ) |
||
120 | ); |
||
121 | |||
122 | $provider[] = array( |
||
123 | "公明執ようなSNSもストーカー行為の対象に", |
||
124 | array( |
||
125 | '公明', '執よう','な','SNS', 'も', |
||
126 | 'ストーカー', '行為', 'の', '対象', 'に' |
||
127 | ) |
||
128 | ); |
||
129 | |||
130 | $provider[] = array( |
||
131 | "公明執", |
||
132 | array( '公明', '執' ) |
||
133 | ); |
||
134 | |||
135 | $provider[] = array( |
||
136 | "IQテスト", |
||
137 | array( 'IQ', 'テスト' ) |
||
138 | ); |
||
139 | |||
140 | $provider[] = array( |
||
141 | "foo テスト bar", |
||
142 | array( 'foo', 'テスト', 'bar' ) |
||
143 | ); |
||
144 | |||
145 | $provider[] = array( |
||
146 | "foo テスト bar 123abc ^&'", |
||
147 | array( 'foo', 'テスト', 'bar', '123abc', '^', '&', "'" ) |
||
148 | ); |
||
149 | |||
150 | $provider[] = array( |
||
151 | "was discovered in 1957 and first sold as a medication in 1971", |
||
152 | array( |
||
153 | 'was', 'discovered', 'in', '1957', 'and', |
||
154 | 'first', 'sold', 'as', 'a', 'medication', 'in', '1971' |
||
155 | ) |
||
156 | ); |
||
157 | |||
158 | // See JaTinySegmenterTokenizerTest for comparison |
||
159 | $provider[] = array( |
||
160 | '日本語の新聞記事であれば文字単位で95%程度の精度で分かち書きが行えます。 ', |
||
161 | array( |
||
162 | '日本語', 'の', '新聞', '記事', 'で', |
||
163 | 'あれ', 'ば', '文字', '単位', |
||
164 | 'で', '95', '%', '程度', |
||
165 | 'の', '精度', 'で', '分かち書き', |
||
166 | 'が', '行', 'え', 'ます', '。' |
||
167 | ) |
||
168 | ); |
||
169 | |||
170 | return $provider; |
||
171 | } |
||
172 | |||
174 |
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:
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 thebar
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.