| Conditions | 10 |
| Paths | 11 |
| Total Lines | 42 |
| Code Lines | 31 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 86 | public function prepareNestedData($data, $lft = 0, $rgt = null, $root = 0) |
||
| 87 | { |
||
| 88 | $res = []; |
||
| 89 | foreach ($data as $row) { |
||
| 90 | $currentRoot = isset($row[$this->rootAttribute]) ? $row[$this->rootAttribute] : 0; |
||
| 91 | if (is_null($rgt) || $row[$this->rightAttribute] < $rgt) { |
||
| 92 | if ($lft + 1 == $row[$this->leftAttribute] && $root == $currentRoot) { |
||
| 93 | if ($row[$this->leftAttribute] + 1 !== $row[$this->rightAttribute]) { |
||
| 94 | $res[] = [ |
||
| 95 | 'id' => $row['id'], |
||
| 96 | 'text' => $row[$this->modelLabelAttribute], |
||
| 97 | 'children' => self::prepareNestedData( |
||
| 98 | $data, |
||
| 99 | $row[$this->leftAttribute], |
||
| 100 | $row[$this->rightAttribute], |
||
| 101 | $currentRoot |
||
| 102 | ), |
||
| 103 | ]; |
||
| 104 | } else { |
||
| 105 | $res[] = [ |
||
| 106 | 'id' => $row['id'], |
||
| 107 | 'text' => $row[$this->modelLabelAttribute], |
||
| 108 | 'children' => [] |
||
| 109 | ]; |
||
| 110 | } |
||
| 111 | $lft = $row[$this->rightAttribute]; |
||
| 112 | } else if ($row[$this->leftAttribute] == 1 && $root !== $currentRoot) { |
||
| 113 | $res[] = [ |
||
| 114 | 'id' => $row['id'], |
||
| 115 | 'text' => $row[$this->modelLabelAttribute], |
||
| 116 | 'children' => self::prepareNestedData( |
||
| 117 | $data, |
||
| 118 | $row[$this->leftAttribute], |
||
| 119 | $row[$this->rightAttribute], |
||
| 120 | $currentRoot |
||
| 121 | ), |
||
| 122 | ]; |
||
| 123 | } |
||
| 124 | } |
||
| 125 | } |
||
| 126 | return $res; |
||
| 127 | } |
||
| 128 | } |