Conditions | 1 |
Paths | 1 |
Total Lines | 104 |
Code Lines | 78 |
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 |
||
119 | public function stringProvider() { |
||
120 | |||
121 | $provider[] = array( |
||
|
|||
122 | 'TEXT', |
||
123 | '4', |
||
124 | array( |
||
125 | 'text' |
||
126 | ) |
||
127 | ); |
||
128 | |||
129 | $provider[] = array( |
||
130 | '12345678', |
||
131 | '2', |
||
132 | array( |
||
133 | '12', |
||
134 | '23', |
||
135 | '34', |
||
136 | '45', |
||
137 | '56', |
||
138 | '67', |
||
139 | '78' |
||
140 | ) |
||
141 | ); |
||
142 | |||
143 | $provider[] = array( |
||
144 | '12345678', |
||
145 | '3', |
||
146 | array( |
||
147 | '123', |
||
148 | '234', |
||
149 | '345', |
||
150 | '456', |
||
151 | '567', |
||
152 | '678' |
||
153 | ) |
||
154 | ); |
||
155 | |||
156 | $provider[] = array( |
||
157 | 'hello', |
||
158 | '3', |
||
159 | array( |
||
160 | 'hel', |
||
161 | 'ell', |
||
162 | 'llo' |
||
163 | ) |
||
164 | ); |
||
165 | |||
166 | $provider[] = array( |
||
167 | 'Hello World!', |
||
168 | '3', |
||
169 | array( |
||
170 | 'hel', |
||
171 | 'ell', |
||
172 | 'llo', |
||
173 | 'lo ', |
||
174 | 'o w', |
||
175 | ' wo', |
||
176 | 'wor', |
||
177 | 'orl', |
||
178 | 'rld', |
||
179 | 'ld!' |
||
180 | ) |
||
181 | ); |
||
182 | |||
183 | $provider[] = array( |
||
184 | 'Новости', |
||
185 | '3', |
||
186 | array( |
||
187 | 'нов', |
||
188 | 'ово', |
||
189 | 'вос', |
||
190 | 'ост', |
||
191 | 'сти' |
||
192 | ) |
||
193 | ); |
||
194 | |||
195 | $provider[] = array( |
||
196 | '1時36分更新', |
||
197 | '3', |
||
198 | array( |
||
199 | '1時3', |
||
200 | '時36', |
||
201 | '36分', |
||
202 | '6分更', |
||
203 | '分更新' |
||
204 | ) |
||
205 | ); |
||
206 | |||
207 | $provider[] = array( |
||
208 | 'こんにちは世界!', |
||
209 | '2', |
||
210 | array( |
||
211 | 'こん', |
||
212 | 'んに', |
||
213 | 'にち', |
||
214 | 'ちは', |
||
215 | 'は世', |
||
216 | '世界', |
||
217 | '界!' |
||
218 | ) |
||
219 | ); |
||
220 | |||
221 | return $provider; |
||
222 | } |
||
223 | |||
225 |
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.