Conditions | 2 |
Paths | 2 |
Total Lines | 83 |
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 |
||
163 | public function testThreeLevelParse() |
||
164 | { |
||
165 | $unit1 = new Unit('customer'); |
||
166 | $unit1->setIsEntityCondition(function ($map) { |
||
167 | return !empty($map['email']); |
||
168 | }); |
||
169 | $unit2 = new Unit('address'); |
||
170 | $unit2->setParent($unit1); |
||
171 | $unit2->setIsEntityCondition(function ($map, $oldmap) { |
||
172 | return $map['street'] != $oldmap['street']; |
||
173 | }); |
||
174 | |||
175 | $unit3 = new Unit('address_data'); |
||
176 | $unit3->setParent($unit2); |
||
177 | |||
178 | $bag = new SimpleBag(); |
||
179 | $bag->addSet([$unit1, $unit2, $unit3]); |
||
180 | |||
181 | $entities = [ |
||
182 | [ |
||
183 | 'email' => '[email protected]', |
||
184 | 'name' => 'bob', |
||
185 | 'address' => [ |
||
186 | [ |
||
187 | 'street' => 'charity str.', |
||
188 | 'addr_name' => 'bob', |
||
189 | 'address_data' => [ |
||
190 | [ |
||
191 | 'phone' => '123', |
||
192 | 'fax' => '244', |
||
193 | ], |
||
194 | [ |
||
195 | 'phone' => '432', |
||
196 | 'fax' => '6766', |
||
197 | ] |
||
198 | ], |
||
199 | ], |
||
200 | ] |
||
201 | ], |
||
202 | [ |
||
203 | 'email' => '[email protected]', |
||
204 | 'name' => 'paul', |
||
205 | 'address' => [ |
||
206 | [ |
||
207 | 'street' => 'buckingham ave.', |
||
208 | 'addr_name' => 'collin', |
||
209 | 'address_data' => [ |
||
210 | [ |
||
211 | 'phone' => '222', |
||
212 | 'fax' => '333', |
||
213 | ] |
||
214 | ], |
||
215 | ], |
||
216 | [ |
||
217 | 'street' => 'mirabelle str.', |
||
218 | 'addr_name' => 'rich', |
||
219 | 'address_data' => [ |
||
220 | [ |
||
221 | 'phone' => '323', |
||
222 | 'fax' => '24234', |
||
223 | ] |
||
224 | ], |
||
225 | ], |
||
226 | ] |
||
227 | ], |
||
228 | ]; |
||
229 | $expected = [ |
||
230 | [ |
||
231 | ['email' => '[email protected]', 'name' => 'bob', 'street' => 'charity str.', 'addr_name' => 'bob', 'phone' => '123', 'fax' => '244'], |
||
232 | ['email' => null, 'name' => null, 'street' => null, 'addr_name' => null, 'phone' => '432', 'fax' => '6766'], |
||
233 | ], |
||
234 | [ |
||
235 | ['email' => '[email protected]', 'name' => 'paul', 'street' => 'buckingham ave.', 'addr_name' => 'collin', 'phone' => '222', 'fax' => '333'], |
||
236 | ['email' => null, 'name' => null, 'street' => 'mirabelle str.', 'addr_name' => 'rich', 'phone' => '323', 'fax' => '24234'], |
||
237 | ] |
||
238 | ]; |
||
239 | |||
240 | $shaper = $this->getShaper($bag); |
||
241 | |||
242 | for ($i = 0; $i<count($entities); $i++) { |
||
243 | $this->assertSame($expected[$i], $shaper->parse($entities[$i])); |
||
244 | } |
||
245 | } |
||
246 | } |
||
247 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: