Complex classes like TreeRepository often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use TreeRepository, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class TreeRepository extends BaseRepository implements TreeRepositoryInterface |
||
10 | { |
||
11 | /** |
||
12 | * Extrepat/Baum tree type |
||
13 | * https://github.com/etrepat/baum. |
||
14 | */ |
||
15 | const TreeTypeBaum = 0; |
||
16 | |||
17 | /** |
||
18 | * Lasychaser/Laravel-nestedset tree type |
||
19 | * https://github.com/lazychaser/laravel-nestedset. |
||
20 | */ |
||
21 | const TreeTypeKalnoy = 1; |
||
22 | |||
23 | /** |
||
24 | * Simple tree type (with `parent_id` and `order` fields). |
||
25 | */ |
||
26 | const TreeTypeSimple = 2; |
||
27 | |||
28 | /** |
||
29 | * Tree type. |
||
30 | * @var int |
||
31 | */ |
||
32 | protected $type; |
||
33 | |||
34 | /** |
||
35 | * Parent field name. |
||
36 | * @var string |
||
37 | */ |
||
38 | protected $parentField = 'parent_id'; |
||
39 | |||
40 | /** |
||
41 | * Order field name. |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $orderField = 'order'; |
||
45 | |||
46 | /** |
||
47 | * Root parent id value. |
||
48 | * @var null |
||
49 | */ |
||
50 | protected $rootParentId = null; |
||
51 | |||
52 | /** |
||
53 | * @param string $class |
||
54 | * |
||
55 | * @return $this |
||
56 | */ |
||
57 | public function setClass($class) |
||
65 | |||
66 | /** |
||
67 | * Get tree structure. |
||
68 | * |
||
69 | * @param \Illuminate\Database\Eloquent\Collection $collection |
||
70 | * |
||
71 | * @return mixed |
||
72 | */ |
||
73 | public function getTree(\Illuminate\Database\Eloquent\Collection $collection) |
||
89 | |||
90 | /** |
||
91 | * Get or set tree type. |
||
92 | * |
||
93 | * @return int |
||
94 | */ |
||
95 | public function getType() |
||
99 | |||
100 | /** |
||
101 | * @param int $type |
||
102 | * |
||
103 | * @return $this |
||
104 | */ |
||
105 | public function setType($type) |
||
111 | |||
112 | /** |
||
113 | * @param int $type |
||
114 | * |
||
115 | * @return bool |
||
116 | */ |
||
117 | public function isType($type) |
||
121 | |||
122 | /** |
||
123 | * Get parent field name. |
||
124 | * |
||
125 | * @return string |
||
126 | */ |
||
127 | public function getParentField() |
||
131 | |||
132 | /** |
||
133 | * @param string $parentField |
||
134 | * |
||
135 | * @return $this |
||
136 | */ |
||
137 | public function setParentField($parentField) |
||
143 | |||
144 | /** |
||
145 | * Get order field name. |
||
146 | * |
||
147 | * @return string |
||
148 | */ |
||
149 | public function getOrderField() |
||
153 | |||
154 | /** |
||
155 | * @param string $orderField |
||
156 | * |
||
157 | * @return $this |
||
158 | */ |
||
159 | public function setOrderField($orderField) |
||
165 | |||
166 | /** |
||
167 | * Get or set parent field name. |
||
168 | * |
||
169 | * @return string |
||
170 | */ |
||
171 | public function getRootParentId() |
||
175 | |||
176 | /** |
||
177 | * @param string $rootParentId |
||
178 | * |
||
179 | * @return $this |
||
180 | */ |
||
181 | public function setRootParentId($rootParentId) |
||
187 | |||
188 | /** |
||
189 | * Reorder tree by $data value. |
||
190 | * |
||
191 | * @param $data |
||
192 | */ |
||
193 | public function reorder(array $data) |
||
204 | |||
205 | /** |
||
206 | * Move tree node in nested-set tree type. |
||
207 | * |
||
208 | * @param $id |
||
209 | * @param $parentId |
||
210 | * @param $left |
||
211 | * @param $right |
||
212 | */ |
||
213 | protected function move($id, $parentId, $left, $right) |
||
223 | |||
224 | /** |
||
225 | * Get left column name. |
||
226 | * |
||
227 | * @param $instance |
||
228 | * |
||
229 | * @return mixed |
||
230 | * @throws Exception |
||
231 | */ |
||
232 | protected function getLeftColumn($instance) |
||
241 | |||
242 | /** |
||
243 | * Get right column name. |
||
244 | * |
||
245 | * @param $instance |
||
246 | * |
||
247 | * @return mixed |
||
248 | * @throws Exception |
||
249 | */ |
||
250 | protected function getRightColumn($instance) |
||
259 | |||
260 | /** |
||
261 | * Get parent column name. |
||
262 | * |
||
263 | * @param $instance |
||
264 | * |
||
265 | * @return mixed |
||
266 | * @throws Exception |
||
267 | */ |
||
268 | protected function getParentColumn($instance) |
||
277 | |||
278 | /** |
||
279 | * Detect tree type. |
||
280 | * @return $this |
||
281 | */ |
||
282 | protected function detectType() |
||
302 | |||
303 | /** |
||
304 | * Call several methods and get first result. |
||
305 | * |
||
306 | * @param $instance |
||
307 | * @param $methods |
||
308 | * |
||
309 | * @return mixed |
||
310 | * @throws Exception |
||
311 | */ |
||
312 | protected function callMethods($instance, $methods) |
||
322 | |||
323 | /** |
||
324 | * Recursive reorder nested-set tree type. |
||
325 | * |
||
326 | * @param $root |
||
327 | * @param $parentId |
||
328 | * @param $left |
||
329 | * |
||
330 | * @return mixed |
||
331 | */ |
||
332 | protected function recursiveReorder($root, $parentId, $left) |
||
344 | |||
345 | /** |
||
346 | * Recursive reoder simple tree type. |
||
347 | * |
||
348 | * @param $data |
||
349 | * @param $parentId |
||
350 | */ |
||
351 | protected function recursiveReorderSimple(array $data, $parentId) |
||
366 | |||
367 | /** |
||
368 | * Get children for simple tree type structure. |
||
369 | * |
||
370 | * @param $collection |
||
371 | * @param $id |
||
372 | * |
||
373 | * @return Collection |
||
374 | */ |
||
375 | protected function getChildren($collection, $id) |
||
390 | |||
391 | /** |
||
392 | * Create simple tree type structure. |
||
393 | * @return static |
||
394 | */ |
||
395 | protected function createSimpleTree() |
||
408 | } |
||
409 |
The break statement is not necessary if it is preceded for example by a return statement:
If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.