| Conditions | 2 |
| Paths | 2 |
| Total Lines | 62 |
| 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 |
||
| 100 | public function testTwoLevelParse2() |
||
| 101 | { |
||
| 102 | $unit1 = new Unit('customer'); |
||
| 103 | $unit1->setIsEntityCondition(function ($map) { |
||
| 104 | return !empty($map['email']); |
||
| 105 | }); |
||
| 106 | $unit2 = new Unit('address'); |
||
| 107 | $unit2->setParent($unit1); |
||
| 108 | |||
| 109 | $unit3 = new Unit('address_data'); |
||
| 110 | $unit3->addSibling($unit2); |
||
| 111 | |||
| 112 | $bag = new SimpleBag(); |
||
| 113 | $bag->addSet([$unit1, $unit2, $unit3]); |
||
| 114 | |||
| 115 | $entities = [ |
||
| 116 | [ |
||
| 117 | 'email' => '[email protected]', |
||
| 118 | 'name' => 'bob', |
||
| 119 | 'address' => [ |
||
| 120 | [ |
||
| 121 | 'addr_name' => 'billy', |
||
| 122 | 'street' => 'charity str.', |
||
| 123 | ], |
||
| 124 | ] |
||
| 125 | ], |
||
| 126 | [ |
||
| 127 | 'email' => '[email protected]', |
||
| 128 | 'name' => 'paul', |
||
| 129 | 'address' => [ |
||
| 130 | [ |
||
| 131 | 'addr_name' => 'billy', |
||
| 132 | 'street' => 'buckingham ave.', |
||
| 133 | ], |
||
| 134 | [ |
||
| 135 | 'addr_name' => 'paul', |
||
| 136 | 'street' => 'mirabelle str.', |
||
| 137 | ], |
||
| 138 | [ |
||
| 139 | 'addr_name' => 'megan', |
||
| 140 | 'street' => 'mirabelle str.', |
||
| 141 | ], |
||
| 142 | ] |
||
| 143 | ], |
||
| 144 | ]; |
||
| 145 | $expected = [ |
||
| 146 | [ |
||
| 147 | ['email' => '[email protected]', 'name' => 'bob', 'addr_name' => 'billy', 'street' => 'charity str.'], |
||
| 148 | ], |
||
| 149 | [ |
||
| 150 | ['email' => '[email protected]', 'name' => 'paul', 'addr_name' => 'billy', 'street' => 'buckingham ave.'], |
||
| 151 | ['email' => null, 'name' => null, 'addr_name' => 'paul', 'street' => 'mirabelle str.'], |
||
| 152 | ['email' => null, 'name' => null, 'addr_name' => 'megan', 'street' => 'mirabelle str.'], |
||
| 153 | ] |
||
| 154 | ]; |
||
| 155 | |||
| 156 | $shaper = $this->getShaper($bag); |
||
| 157 | |||
| 158 | for ($i = 0; $i<count($entities); $i++) { |
||
| 159 | $this->assertSame($expected[$i], $shaper->parse($entities[$i])); |
||
| 160 | } |
||
| 161 | } |
||
| 162 | |||
| 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: