| Conditions | 2 |
| Paths | 2 |
| Total Lines | 59 |
| 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 | $bag = new SimpleBag(); |
||
| 110 | $bag->addSet([$unit1, $unit2]); |
||
| 111 | |||
| 112 | $entities = [ |
||
| 113 | [ |
||
| 114 | 'email' => '[email protected]', |
||
| 115 | 'name' => 'bob', |
||
| 116 | 'address' => [ |
||
| 117 | [ |
||
| 118 | 'addr_name' => 'billy', |
||
| 119 | 'street' => 'charity str.', |
||
| 120 | ], |
||
| 121 | ] |
||
| 122 | ], |
||
| 123 | [ |
||
| 124 | 'email' => '[email protected]', |
||
| 125 | 'name' => 'paul', |
||
| 126 | 'address' => [ |
||
| 127 | [ |
||
| 128 | 'addr_name' => 'paul', |
||
| 129 | 'street' => 'buckingham ave.', |
||
| 130 | ], |
||
| 131 | [ |
||
| 132 | 'addr_name' => 'megan', |
||
| 133 | 'street' => 'mirabelle str.', |
||
| 134 | ], |
||
| 135 | [ |
||
| 136 | 'addr_name' => 'tiffany', |
||
| 137 | 'street' => 'mirabelle str.', |
||
| 138 | ], |
||
| 139 | ] |
||
| 140 | ], |
||
| 141 | ]; |
||
| 142 | $expected = [ |
||
| 143 | [ |
||
| 144 | ['email' => '[email protected]', 'name' => 'bob', 'addr_name' => 'billy', 'street' => 'charity str.'], |
||
| 145 | ], |
||
| 146 | [ |
||
| 147 | ['email' => '[email protected]', 'name' => 'paul', 'addr_name' => 'paul', 'street' => 'buckingham ave.'], |
||
| 148 | ['email' => '[email protected]', 'name' => 'paul', 'addr_name' => 'megan', 'street' => 'mirabelle str.'], |
||
| 149 | ['email' => '[email protected]', 'name' => 'paul', 'addr_name' => 'tiffany', 'street' => 'mirabelle str.'], |
||
| 150 | ] |
||
| 151 | ]; |
||
| 152 | |||
| 153 | $shaper = $this->getShaper($bag); |
||
| 154 | |||
| 155 | for ($i = 0; $i<count($entities); $i++) { |
||
| 156 | $this->assertSame($expected[$i], $shaper->parse($entities[$i])); |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 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: