| Conditions | 7 |
| Paths | 8 |
| Total Lines | 70 |
| Code Lines | 38 |
| 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 |
||
| 180 | public function getEagerLoadList() |
||
| 181 | { |
||
| 182 | if (!$this->isExpansionSpecified()) { |
||
| 183 | return []; |
||
| 184 | } |
||
| 185 | if (0 === count($this->getChildNodes())) { |
||
| 186 | return []; |
||
| 187 | } |
||
| 188 | // need to use a stack to track chain of parent nodes back to root |
||
| 189 | // each entry has three elements - zeroth being node itself, first being property name, second is |
||
| 190 | // index in parent's children - when that overruns parent's childNodes array, we can pop the parent |
||
| 191 | // node off the stack and move on to grandparent's next child. When we're finished with a node, then and |
||
| 192 | // only then generate its relation chain and stash it. When we're done (stack empty), dedupe the chain stash |
||
| 193 | // and return it. |
||
| 194 | |||
| 195 | // set up tracking stack and scratchpad |
||
| 196 | $trackStack = []; |
||
| 197 | $trackStack[] = ['node' => $this, 'name' => null, 'index' => 0]; |
||
| 198 | $scratchpad = []; |
||
| 199 | |||
| 200 | // now start the dance |
||
| 201 | while (0 < count($trackStack)) { |
||
| 202 | $stackDex = count($trackStack) - 1; |
||
| 203 | assert( |
||
| 204 | self::MAX_EXPAND_TREE_DEPTH > $stackDex, |
||
| 205 | 'Expansion stack too deep - should be less than '. self::MAX_EXPAND_TREE_DEPTH . 'elements' |
||
| 206 | ); |
||
| 207 | $topNode = $trackStack[$stackDex]; |
||
| 208 | $nodes = $topNode['node']->getChildNodes(); |
||
| 209 | // have we finished processing current level? |
||
| 210 | // this treats a leaf node as simply another exhausted parent node with all of its zero children having |
||
| 211 | // been processed |
||
| 212 | $topDex = $topNode['index']; |
||
| 213 | if ($topDex >= count($nodes)) { |
||
| 214 | $eager = ''; |
||
| 215 | foreach ($trackStack as $stack) { |
||
| 216 | $eager .= $stack['name'] . '/'; |
||
| 217 | } |
||
| 218 | $eager = trim($eager, '/'); |
||
| 219 | if (1 < strlen($eager)) { |
||
| 220 | $scratchpad[] = $eager; |
||
| 221 | } |
||
| 222 | array_pop($trackStack); |
||
| 223 | assert( |
||
| 224 | count($trackStack) === $stackDex, |
||
| 225 | 'Exhausted node must shrink tracking stack by exactly one element' |
||
| 226 | ); |
||
| 227 | continue; |
||
| 228 | } |
||
| 229 | |||
| 230 | // dig up key |
||
| 231 | $key = array_keys($nodes)[$topDex]; |
||
| 232 | // prep payload for this child |
||
| 233 | $payload = ['node' => $nodes[$key], 'name' => $key, 'index' => 0]; |
||
| 234 | array_push($trackStack, $payload); |
||
| 235 | // advance index pointer on parent |
||
| 236 | $trackStack[$stackDex]['index']++; |
||
| 237 | // $stackDex already decrements stack count by 1, so we have to bump it up by two to net out to a +1 |
||
| 238 | assert( |
||
| 239 | count($trackStack) === $stackDex + 2, |
||
| 240 | 'Non-exhausted node must expand tracking stack by exactly one element' |
||
| 241 | ); |
||
| 242 | } |
||
| 243 | |||
| 244 | // dedupe scratchpad |
||
| 245 | $scratchpad = array_unique($scratchpad); |
||
| 246 | // deliberately shuffle scratchpad to falsify any ordering assumptions downstream |
||
| 247 | shuffle($scratchpad); |
||
| 248 | return $scratchpad; |
||
| 249 | } |
||
| 250 | } |
||
| 251 |