| Conditions | 2 |
| Paths | 2 |
| Total Lines | 76 |
| 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 |
||
| 160 | public function testThreeLevelParse() |
||
| 161 | { |
||
| 162 | $unit1 = new Unit('customer'); |
||
| 163 | $unit1->setIsEntityCondition(function ($map) { |
||
| 164 | return !empty($map['email']); |
||
| 165 | }); |
||
| 166 | $unit2 = new Unit('address'); |
||
| 167 | $unit2->setParent($unit1); |
||
| 168 | $unit2->setIsEntityCondition(function ($map, $oldmap) { |
||
| 169 | return $map['street'] != $oldmap['street']; |
||
| 170 | }); |
||
| 171 | |||
| 172 | $unit3 = new Unit('address_data'); |
||
| 173 | $unit3->setParent($unit2); |
||
| 174 | |||
| 175 | $bag = new SimpleBag(); |
||
| 176 | $bag->addSet([$unit1, $unit2, $unit3]); |
||
| 177 | |||
| 178 | $entities = [ |
||
| 179 | [ |
||
| 180 | 'email' => '[email protected]', |
||
| 181 | 'name' => 'bob', |
||
| 182 | 'address' => [ |
||
| 183 | [ |
||
| 184 | 'street' => 'charity str.', |
||
| 185 | 'address_data' => [ |
||
| 186 | [ |
||
| 187 | 'phone' => '123', |
||
| 188 | ], |
||
| 189 | [ |
||
| 190 | 'phone' => '432', |
||
| 191 | ] |
||
| 192 | ], |
||
| 193 | ], |
||
| 194 | ] |
||
| 195 | ], |
||
| 196 | [ |
||
| 197 | 'email' => '[email protected]', |
||
| 198 | 'name' => 'paul', |
||
| 199 | 'address' => [ |
||
| 200 | [ |
||
| 201 | 'street' => 'buckingham ave.', |
||
| 202 | 'address_data' => [ |
||
| 203 | [ |
||
| 204 | 'phone' => '222', |
||
| 205 | ] |
||
| 206 | ], |
||
| 207 | ], |
||
| 208 | [ |
||
| 209 | 'street' => 'mirabelle str.', |
||
| 210 | 'address_data' => [ |
||
| 211 | [ |
||
| 212 | 'phone' => '323', |
||
| 213 | ] |
||
| 214 | ], |
||
| 215 | ], |
||
| 216 | ] |
||
| 217 | ], |
||
| 218 | ]; |
||
| 219 | $expected = [ |
||
| 220 | [ |
||
| 221 | ['email' => '[email protected]', 'name' => 'bob', 'street' => 'charity str.', 'phone' => '123'], |
||
| 222 | ['email' => '[email protected]', 'name' => 'bob', 'street' => 'charity str.', 'phone' => '432'], |
||
| 223 | ], |
||
| 224 | [ |
||
| 225 | ['email' => '[email protected]', 'name' => 'paul', 'street' => 'buckingham ave.', 'phone' => '222'], |
||
| 226 | ['email' => '[email protected]', 'name' => 'paul', 'street' => 'mirabelle str.', 'phone' => '323'], |
||
| 227 | ] |
||
| 228 | ]; |
||
| 229 | |||
| 230 | $shaper = $this->getShaper($bag); |
||
| 231 | |||
| 232 | for ($i = 0; $i<count($entities); $i++) { |
||
| 233 | $this->assertSame($expected[$i], $shaper->parse($entities[$i])); |
||
| 234 | } |
||
| 235 | } |
||
| 236 | } |
||
| 237 |
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: