Total Complexity | 43 |
Total Lines | 255 |
Duplicated Lines | 21.96 % |
Changes | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like CachedBuilder 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.
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 CachedBuilder, and based on these observations, apply Extract Interface, too.
1 | <?php namespace GeneaLabs\LaravelModelCaching; |
||
10 | class CachedBuilder extends EloquentBuilder |
||
11 | { |
||
12 | protected function cache(array $tags = []) |
||
21 | } |
||
22 | |||
23 | protected function getCacheKey(array $columns = ['*'], $idColumn = null) : string |
||
24 | { |
||
25 | $key = $this->getModelSlug(); |
||
26 | $key .= $this->getIdColumn($idColumn ?: ''); |
||
27 | $key .= $this->getQueryColumns($columns); |
||
28 | $key .= $this->getWhereClauses(); |
||
29 | $key .= $this->getWithModels(); |
||
30 | $key .= $this->getOrderByClauses(); |
||
31 | $key .= $this->getOffsetClause(); |
||
32 | $key .= $this->getLimitClause(); |
||
33 | |||
34 | return $key; |
||
35 | } |
||
36 | |||
37 | protected function getIdColumn(string $idColumn) : string |
||
38 | { |
||
39 | |||
40 | return $idColumn ? "_{$idColumn}" : ''; |
||
41 | } |
||
42 | |||
43 | protected function getLimitClause() : string |
||
44 | { |
||
45 | if (! $this->query->limit) { |
||
46 | return ''; |
||
47 | } |
||
48 | |||
49 | return "-limit_{$this->query->limit}"; |
||
50 | } |
||
51 | |||
52 | protected function getModelSlug() : string |
||
53 | { |
||
54 | return str_slug(get_class($this->model)); |
||
55 | } |
||
56 | |||
57 | protected function getOffsetClause() : string |
||
58 | { |
||
59 | if (! $this->query->offset) { |
||
60 | return ''; |
||
61 | } |
||
62 | |||
63 | return "-offset_{$this->query->offset}"; |
||
64 | } |
||
65 | |||
66 | protected function getQueryColumns(array $columns) : string |
||
73 | } |
||
74 | |||
75 | protected function getWhereClauses(array $wheres = []) : string |
||
76 | { |
||
77 | return $this->getWheres($wheres) |
||
78 | ->reduce(function ($carry, $where) { |
||
79 | if (in_array($where['type'], ['Exists', 'Nested', 'NotExists'])) { |
||
80 | return '_' . strtolower($where['type']) . $this->getWhereClauses($where['query']->wheres); |
||
81 | } |
||
82 | |||
83 | if ($where['type'] === 'Column') { |
||
84 | return "_{$where['boolean']}_{$where['first']}_{$where['operator']}_{$where['second']}"; |
||
85 | } |
||
86 | |||
87 | if ($where['type'] === 'raw') { |
||
88 | return "_{$where['boolean']}_" . str_slug($where['sql']); |
||
89 | } |
||
90 | |||
91 | $value = array_get($where, 'value'); |
||
92 | $value .= $this->getTypeClause($where); |
||
93 | $value .= $this->getValuesClause($where); |
||
94 | |||
95 | return "{$carry}-{$where['column']}_{$value}"; |
||
96 | }) |
||
97 | . ''; |
||
98 | } |
||
99 | |||
100 | protected function getWithModels() : string |
||
101 | { |
||
102 | $eagerLoads = collect($this->eagerLoad); |
||
103 | |||
104 | if ($eagerLoads->isEmpty()) { |
||
105 | return ''; |
||
106 | } |
||
107 | |||
108 | return '-' . implode('-', $eagerLoads->keys()->toArray()); |
||
109 | } |
||
110 | |||
111 | protected function getOrderByClauses(){ |
||
112 | $orders = collect($this->query->orders); |
||
113 | |||
114 | return $orders->reduce(function($carry, $order){ |
||
115 | return $carry . '_orderBy_' . $order['column'] . '_' . $order['direction']; |
||
116 | }); |
||
117 | } |
||
118 | |||
119 | protected function getMethodKey(string $postfix = null) : string |
||
120 | { |
||
121 | return str_slug(get_class($this->model)) . $postfix; |
||
122 | } |
||
123 | |||
124 | protected function getModelTag() : array |
||
125 | { |
||
126 | return [str_slug(get_class($this->model))]; |
||
127 | } |
||
128 | |||
129 | protected function getCacheTags() : array |
||
151 | } |
||
152 | |||
153 | View Code Duplication | public function avg($column) |
|
154 | { |
||
155 | return $this->cache($this->getModelTag()) |
||
156 | ->rememberForever($this->getMethodKey("-avg_{$column}"), function () use ($column) { |
||
157 | return parent::avg($column); |
||
158 | }); |
||
159 | } |
||
160 | |||
161 | View Code Duplication | public function count($columns = ['*']) |
|
162 | { |
||
163 | return $this->cache($this->getModelTag()) |
||
164 | ->rememberForever($this->getMethodKey("-count"), function () use ($columns) { |
||
165 | return parent::count($columns); |
||
166 | }); |
||
167 | } |
||
168 | |||
169 | public function cursor() |
||
170 | { |
||
171 | return $this->cache($this->getModelTag()) |
||
172 | ->rememberForever($this->getMethodKey("-cursor"), function () { |
||
173 | return collect(parent::cursor()); |
||
174 | }); |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * @SuppressWarnings(PHPMD.ShortVariable) |
||
179 | */ |
||
180 | View Code Duplication | public function find($id, $columns = ['*']) |
|
181 | { |
||
182 | return $this->cache($this->getCacheTags()) |
||
183 | ->rememberForever($this->getCacheKey($columns, $id), function () use ($id, $columns) { |
||
184 | return parent::find($id, $columns); |
||
185 | }); |
||
186 | } |
||
187 | |||
188 | View Code Duplication | public function first($columns = ['*']) |
|
189 | { |
||
190 | return $this->cache($this->getCacheTags()) |
||
191 | ->rememberForever($this->getCacheKey($columns) . '-first', function () use ($columns) { |
||
192 | return parent::first($columns); |
||
193 | }); |
||
194 | } |
||
195 | |||
196 | View Code Duplication | public function get($columns = ['*']) |
|
197 | { |
||
198 | return $this->cache($this->getCacheTags()) |
||
199 | ->rememberForever($this->getCacheKey($columns), function () use ($columns) { |
||
200 | return parent::get($columns); |
||
201 | }); |
||
202 | } |
||
203 | |||
204 | View Code Duplication | public function max($column) |
|
1 ignored issue
–
show
|
|||
205 | { |
||
206 | return $this->cache($this->getModelTag()) |
||
207 | ->rememberForever($this->getMethodKey("-max_{$column}"), function () use ($column) { |
||
208 | return parent::max($column); |
||
209 | }); |
||
210 | } |
||
211 | |||
212 | View Code Duplication | public function min($column) |
|
217 | }); |
||
218 | } |
||
219 | |||
220 | public function pluck($column, $key = null) |
||
231 | }); |
||
232 | } |
||
233 | |||
234 | View Code Duplication | public function sum($column) |
|
235 | { |
||
236 | return $this->cache($this->getModelTag()) |
||
237 | ->rememberForever($this->getMethodKey("-sum_{$column}"), function () use ($column) { |
||
238 | return parent::sum($column); |
||
239 | }); |
||
240 | } |
||
241 | |||
242 | protected function getTypeClause($where) |
||
247 | } |
||
248 | |||
249 | protected function getValuesClause($where) |
||
250 | { |
||
254 | } |
||
255 | |||
256 | protected function getWheres(array $wheres) : Collection |
||
265 | } |
||
266 | } |
||
267 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.