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