| Conditions | 14 |
| Paths | 243 |
| Total Lines | 53 |
| Code Lines | 33 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 2 |
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 |
||
| 143 | public function end($setter = null) |
||
| 144 | { |
||
| 145 | if (!count($this->stack)) { |
||
| 146 | throw new \UnexpectedValueException("Stack is empty. Did you call end() too many times?"); |
||
| 147 | } |
||
| 148 | $current = array_pop($this->stack); |
||
| 149 | if ($parent = $this->current()) { |
||
| 150 | $parentClassName = get_class($parent); |
||
| 151 | $entityLocalName = Str::classname(get_class($current)); |
||
| 152 | |||
| 153 | if ($current instanceof $parentClassName) { |
||
| 154 | if (method_exists($parent, 'addChildren')) { |
||
| 155 | call_user_func(array($parent, 'addChildren'), $current); |
||
| 156 | } |
||
| 157 | } |
||
| 158 | if (is_null($setter)) { |
||
| 159 | foreach (array('set', 'add') as $methodPrefix) { |
||
| 160 | $methodName = $methodPrefix . $entityLocalName; |
||
| 161 | |||
| 162 | if (method_exists($parent, $methodName)) { |
||
| 163 | $setter = $methodName; |
||
| 164 | break; |
||
| 165 | } |
||
| 166 | } |
||
| 167 | } |
||
| 168 | if (!is_null($setter)) { |
||
| 169 | call_user_func(array($parent, $setter), $current); |
||
| 170 | } |
||
| 171 | |||
| 172 | $parentClassNames = array_merge(class_parents($parentClassName), array($parentClassName)); |
||
| 173 | |||
| 174 | foreach (array_reverse($parentClassNames) as $lParentClassName) { |
||
| 175 | $lParentClass = Str::classname($lParentClassName); |
||
| 176 | $parentSetter = 'set' . $lParentClass; |
||
| 177 | if ($lParentClassName == get_class($current)) { |
||
| 178 | $parentSetter = 'setParent'; |
||
| 179 | } |
||
| 180 | if (method_exists($current, $parentSetter)) { |
||
| 181 | call_user_func( |
||
| 182 | array($current, $parentSetter), |
||
| 183 | $parent |
||
| 184 | ); |
||
| 185 | break; |
||
| 186 | } |
||
| 187 | } |
||
| 188 | foreach ($this->alwaysDo as $callable) { |
||
| 189 | call_user_func($callable, $parent); |
||
| 190 | } |
||
| 191 | } |
||
| 192 | foreach ($this->alwaysDo as $callable) { |
||
| 193 | call_user_func($callable, $current); |
||
| 194 | } |
||
| 195 | return $this; |
||
| 196 | } |
||
| 215 |