| Conditions | 1 |
| Paths | 1 |
| Total Lines | 147 |
| Code Lines | 119 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 43 | public function stringProvider() { |
||
| 44 | |||
| 45 | $provider[] = array( |
||
|
|
|||
| 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 | |||
| 192 |
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
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey 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.