| Conditions | 21 |
| Paths | 70 |
| Total Lines | 103 |
| 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 |
||
| 61 | public function sortTree() |
||
| 62 | { |
||
| 63 | $level = 0; |
||
| 64 | $count = 0; |
||
| 65 | $pageNum = $this->getPage(); |
||
| 66 | $perPage = $this->getNumPerPage(); |
||
| 67 | |||
| 68 | $sortedObjects = []; |
||
| 69 | $rootObjects = []; |
||
| 70 | $childObjects = []; |
||
| 71 | |||
| 72 | foreach ($this->objects as $object) { |
||
| 73 | // Repair bad hierarchy. |
||
| 74 | if ($object->hasMaster() && $object->getMaster()->id() === $object->id()) { |
||
| 75 | $object->setMaster(0); |
||
| 76 | $object->update([ 'master' ]); |
||
| 77 | } |
||
| 78 | |||
| 79 | if ($object->hasMaster()) { |
||
| 80 | $childObjects[$object->getMaster()->id()][] = $object; |
||
| 81 | } else { |
||
| 82 | $rootObjects[] = $object; |
||
| 83 | } |
||
| 84 | } |
||
| 85 | |||
| 86 | if (empty($rootObjects) && !empty($childObjects)) { |
||
| 87 | foreach ($childObjects as $parentId => $children) { |
||
| 88 | $parentObj = $children[0]->getMaster(); |
||
| 89 | $parentObj->auxiliary = true; |
||
| 90 | |||
| 91 | $rootObjects[] = $parentObj; |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | $this->objects = &$rootObjects; |
||
| 96 | |||
| 97 | if ($perPage < 1) { |
||
| 98 | foreach ($this->objects as $object) { |
||
| 99 | $object->level = $level; |
||
| 100 | $sortedObjects[$object->id()] = $object; |
||
| 101 | |||
| 102 | $count++; |
||
| 103 | |||
| 104 | if (isset($childObjects[$object->id()])) { |
||
| 105 | $this->sortDescendantObjects( |
||
| 106 | $object, |
||
| 107 | $childObjects, |
||
| 108 | $count, |
||
| 109 | ($level + 1), |
||
| 110 | $sortedObjects |
||
| 111 | ); |
||
| 112 | } |
||
| 113 | } |
||
| 114 | } else { |
||
| 115 | $start = (( $pageNum - 1 ) * $perPage); |
||
| 116 | $end = ($start + $perPage); |
||
| 117 | |||
| 118 | foreach ($this->objects as $object) { |
||
| 119 | if ($count >= $end) { |
||
| 120 | break; |
||
| 121 | } |
||
| 122 | |||
| 123 | if ($count >= $start) { |
||
| 124 | $object->level = $level; |
||
| 125 | $sortedObjects[$object->id()] = $object; |
||
| 126 | } |
||
| 127 | |||
| 128 | $count++; |
||
| 129 | |||
| 130 | if (isset($childObjects[$object->id()])) { |
||
| 131 | $this->sortDescendantObjects( |
||
| 132 | $object, |
||
| 133 | $childObjects, |
||
| 134 | $count, |
||
| 135 | ($level + 1), |
||
| 136 | $sortedObjects |
||
| 137 | ); |
||
| 138 | } |
||
| 139 | } |
||
| 140 | |||
| 141 | // If we are on the last page, display orphaned descendants. |
||
| 142 | if ($childObjects && $count < $end) { |
||
| 143 | foreach ($childObjects as $orphans) { |
||
| 144 | foreach ($orphans as $descendants) { |
||
| 145 | if ($count >= $end) { |
||
| 146 | break; |
||
| 147 | } |
||
| 148 | |||
| 149 | if ($count >= $start) { |
||
| 150 | $descendants->level = 0; |
||
| 151 | $sortedObjects[$descendants->id()] = $descendants; |
||
| 152 | } |
||
| 153 | |||
| 154 | $count++; |
||
| 155 | } |
||
| 156 | } |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 160 | $this->objects = $sortedObjects; |
||
| 161 | |||
| 162 | return $this; |
||
| 163 | } |
||
| 164 | |||
| 359 |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.