Total Complexity | 48 |
Total Lines | 458 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Generic 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 Generic, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | abstract class Generic implements Sortable, AutoTranslatable |
||
16 | { |
||
17 | use AcceptsCustomFormat, AppliesSorting, AutoTranslatesInstances; |
||
18 | |||
19 | /** @var string */ |
||
20 | protected $id; |
||
21 | |||
22 | /** @var string */ |
||
23 | protected $title; |
||
24 | |||
25 | /** @var string */ |
||
26 | protected $name; |
||
27 | |||
28 | /** @var mixed */ |
||
29 | protected $value; |
||
30 | |||
31 | /** @var string */ |
||
32 | protected $description; |
||
33 | |||
34 | /** @var Model */ |
||
35 | protected $model; |
||
36 | |||
37 | /** @var bool */ |
||
38 | protected $showLabel = true; |
||
39 | |||
40 | /** @var array */ |
||
41 | protected $visibility = [ |
||
42 | Scaffolding::PAGE_INDEX => true, |
||
43 | Scaffolding::PAGE_EDIT => true, |
||
44 | Scaffolding::PAGE_VIEW => true, |
||
45 | ]; |
||
46 | |||
47 | /** @var array */ |
||
48 | protected $attributes = [ |
||
49 | 'class' => 'form-control', |
||
50 | ]; |
||
51 | |||
52 | /** |
||
53 | * Generic constructor. |
||
54 | * |
||
55 | * @param string $title |
||
56 | * @param null|string $id |
||
57 | */ |
||
58 | private function __construct(string $title, string $id = null) |
||
59 | { |
||
60 | $this->setId( |
||
61 | snake_case($id ?: $title) |
||
62 | ); |
||
63 | |||
64 | if ($this->translator()->has($key = $this->translationKey())) { |
||
65 | $this->setTitle((string) $this->translator()->trans($key)); |
||
66 | } else { |
||
67 | $this->setTitle( |
||
68 | 'id' === $this->id |
||
69 | ? 'ID' |
||
70 | : StringHumanizer::humanize(str_replace(['_id', '-', '_'], ['', ' ', ' '], $this->id)) |
||
71 | ); |
||
72 | } |
||
73 | |||
74 | if ($this->translator()->has($key = $this->descriptionKey())) { |
||
75 | $this->setDescription((string) $this->translator()->trans($key)); |
||
76 | } |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * @param $title |
||
81 | * @param null $id |
||
|
|||
82 | * @param \Closure $callback |
||
83 | * |
||
84 | * @return static |
||
85 | */ |
||
86 | public static function make($title, $id = null, \Closure $callback = null): self |
||
87 | { |
||
88 | $instance = new static($title, $id); |
||
89 | |||
90 | if (null !== $callback) { |
||
91 | $callback->call($instance, $instance); |
||
92 | } |
||
93 | |||
94 | return $instance; |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * Create new element from another. |
||
99 | * |
||
100 | * @param Generic $element |
||
101 | * @return static |
||
102 | */ |
||
103 | public static function makeFrom(Generic $element): self |
||
104 | { |
||
105 | return static::make($element->title(), $element->id()); |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * Switch to a new element type. |
||
110 | * |
||
111 | * @param string $className |
||
112 | * @return mixed |
||
113 | */ |
||
114 | public function switchTo(string $className) |
||
115 | { |
||
116 | return forward_static_call_array([$className, "make"], [$this->title(), $this->id()]); |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * @param Model $model |
||
121 | * |
||
122 | * @return static |
||
123 | */ |
||
124 | public function setModel(Model $model): self |
||
125 | { |
||
126 | $this->model = $model; |
||
127 | |||
128 | return $this; |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * @return Model |
||
133 | */ |
||
134 | public function getModel() |
||
135 | { |
||
136 | return $this->model; |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * Render Element. |
||
141 | * |
||
142 | * @param string $page |
||
143 | * |
||
144 | * @return mixed |
||
145 | */ |
||
146 | final public function render(string $page = 'index') |
||
147 | { |
||
148 | if ($this->format) { |
||
149 | // Each Field can define its own data for custom formatter. |
||
150 | $withData = method_exists($this, 'renderWith') |
||
151 | ? $this->renderWith() |
||
152 | : [$this->value(), $this->model]; |
||
153 | |||
154 | return $this->callFormatter($withData); |
||
155 | } |
||
156 | |||
157 | $data = [ |
||
158 | 'field' => $this, |
||
159 | 'model' => $this->model, |
||
160 | 'attributes' => $this->getAttributes(), |
||
161 | ]; |
||
162 | |||
163 | if (method_exists($this, $dataGetter = 'on'.title_case($page))) { |
||
164 | $data += call_user_func([$this, $dataGetter]); |
||
165 | } |
||
166 | |||
167 | if (View::exists($view = $this->template($page))) { |
||
168 | return View::make($view, $data); |
||
169 | } |
||
170 | |||
171 | return View::make($this->template($page, 'Key'), $data); |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * Return Element ID. |
||
176 | * |
||
177 | * @return string |
||
178 | */ |
||
179 | public function id(): string |
||
180 | { |
||
181 | return $this->id; |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * Form name. |
||
186 | * |
||
187 | * @return string |
||
188 | */ |
||
189 | public function name(): string |
||
190 | { |
||
191 | if (null === $this->name) { |
||
192 | $parts = explode('.', $this->id()); |
||
193 | |||
194 | if (count($parts) > 1) { |
||
195 | $first = array_first($parts); |
||
196 | $other = array_slice($parts, 1); |
||
197 | |||
198 | $other = array_map(function ($part) { |
||
199 | return "[$part]"; |
||
200 | }, $other); |
||
201 | |||
202 | return $this->name = implode('', array_merge([$first], $other)); |
||
203 | } |
||
204 | |||
205 | return $this->name = $this->id(); |
||
206 | } |
||
207 | |||
208 | return $this->name; |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * @param string $name |
||
213 | * @return Generic |
||
214 | */ |
||
215 | public function setName(string $name): self |
||
216 | { |
||
217 | $this->name = $name; |
||
218 | |||
219 | return $this; |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * @param string $id |
||
224 | * |
||
225 | * @return static |
||
226 | */ |
||
227 | public function setId(string $id): self |
||
228 | { |
||
229 | $this->id = $id; |
||
230 | |||
231 | return $this; |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * Return Element title. |
||
236 | * |
||
237 | * @return string |
||
238 | */ |
||
239 | public function title(): string |
||
240 | { |
||
241 | return $this->title; |
||
242 | } |
||
243 | |||
244 | /** |
||
245 | * @return string |
||
246 | */ |
||
247 | public function getDescription(): ?string |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | * @param string $title |
||
254 | * |
||
255 | * @return static |
||
256 | */ |
||
257 | public function setTitle(string $title): self |
||
258 | { |
||
259 | $this->title = $title; |
||
260 | |||
261 | return $this; |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * @param null|string $description |
||
266 | * |
||
267 | * @return static |
||
268 | */ |
||
269 | public function setDescription(?string $description): self |
||
270 | { |
||
271 | $this->description = $description; |
||
272 | |||
273 | return $this; |
||
274 | } |
||
275 | |||
276 | /** |
||
277 | * @param bool $showLabel |
||
278 | * |
||
279 | * @return static |
||
280 | */ |
||
281 | public function hideLabel(bool $hideLabel): self |
||
282 | { |
||
283 | $this->showLabel = !$hideLabel; |
||
284 | |||
285 | return $this; |
||
286 | } |
||
287 | |||
288 | /** |
||
289 | * @return bool |
||
290 | */ |
||
291 | public function isHiddenLabel(): bool |
||
292 | { |
||
293 | return !$this->showLabel; |
||
294 | } |
||
295 | |||
296 | /** |
||
297 | * string $page. |
||
298 | * |
||
299 | * @return bool |
||
300 | */ |
||
301 | public function isVisibleOnPage(string $page): bool |
||
302 | { |
||
303 | return (bool) $this->visibility[$page] ?? false; |
||
304 | } |
||
305 | |||
306 | /** |
||
307 | * @param array|string $pages |
||
308 | * |
||
309 | * @return static |
||
310 | */ |
||
311 | public function hideOnPages($pages): self |
||
312 | { |
||
313 | return $this->setPagesVisibility((array) $pages, false); |
||
314 | } |
||
315 | |||
316 | /** |
||
317 | * @param array|string $pages |
||
318 | * |
||
319 | * @return static |
||
320 | */ |
||
321 | public function showOnPages($pages): self |
||
322 | { |
||
323 | return $this->setPagesVisibility((array) $pages, true); |
||
324 | } |
||
325 | |||
326 | /** |
||
327 | * Make column sortable. |
||
328 | * |
||
329 | * @param null|\Closure $callback |
||
330 | * |
||
331 | * @return static |
||
332 | */ |
||
333 | public function sortable(\Closure $callback = null): self |
||
341 | } |
||
342 | |||
343 | /** |
||
344 | * Remove column from Sortable collection. |
||
345 | * |
||
346 | * @return static |
||
347 | */ |
||
348 | public function disableSorting(): self |
||
349 | { |
||
350 | app('scaffold.module')->removeSortable($this->id()); |
||
351 | |||
352 | return $this; |
||
353 | } |
||
354 | |||
355 | /** |
||
356 | * Set value. |
||
357 | * |
||
358 | * @param $value |
||
359 | * |
||
360 | * @return Generic |
||
361 | */ |
||
362 | public function setValue($value): self |
||
363 | { |
||
364 | $this->value = $value; |
||
365 | |||
366 | return $this; |
||
367 | } |
||
368 | |||
369 | /** |
||
370 | * Extract Element value. |
||
371 | * |
||
372 | * @return mixed |
||
373 | */ |
||
374 | public function value() |
||
375 | { |
||
376 | if (null !== $this->value) { |
||
377 | return $this->value; |
||
378 | } |
||
379 | |||
380 | if (!$this->model) { |
||
381 | return null; |
||
382 | } |
||
383 | |||
384 | return $this->model->getAttribute($this->id); |
||
385 | } |
||
386 | |||
387 | /** |
||
388 | * @param $key |
||
389 | * @param null $value |
||
390 | * @param mixed $attribute |
||
391 | * |
||
392 | * @return static |
||
393 | */ |
||
394 | public function setAttribute($attribute, $value = null): self |
||
395 | { |
||
396 | if (is_array($attribute)) { |
||
397 | foreach ($attribute as $key => $value) { |
||
398 | $this->setAttribute($key, $value); |
||
399 | } |
||
400 | } else { |
||
401 | $this->attributes[$attribute] = $value; |
||
402 | } |
||
403 | |||
404 | return $this; |
||
405 | } |
||
406 | |||
407 | /** |
||
408 | * @return array |
||
409 | */ |
||
410 | public function getAttributes(): array |
||
411 | { |
||
412 | return $this->attributes; |
||
413 | } |
||
414 | |||
415 | /** |
||
416 | * @param mixed $pages |
||
417 | * @param bool $visibility |
||
418 | * |
||
419 | * @return $this |
||
420 | */ |
||
421 | protected function setPagesVisibility($pages, bool $visibility): self |
||
422 | { |
||
423 | $pages = array_intersect($pages, array_keys($this->visibility)); |
||
424 | |||
425 | foreach ($pages as $page) { |
||
426 | $this->visibility[$page] = $visibility; |
||
427 | } |
||
428 | |||
429 | return $this; |
||
430 | } |
||
431 | |||
432 | /** |
||
433 | * @param string $page |
||
434 | * @param string $field |
||
435 | * |
||
436 | * @return string |
||
437 | */ |
||
438 | protected function template(string $page, string $field = null): string |
||
439 | { |
||
440 | return sprintf( |
||
441 | 'administrator::fields.%s.%s', |
||
442 | snake_case($field ?? class_basename($this)), |
||
443 | $page |
||
444 | ); |
||
445 | } |
||
446 | |||
447 | /** |
||
448 | * @return string |
||
449 | */ |
||
450 | public function translationKey() |
||
451 | { |
||
452 | $key = sprintf('administrator::columns.%s.%s', $this->translatableModule()->url(), $this->id); |
||
453 | |||
454 | if (!$this->translator()->has($key)) { |
||
455 | $key = sprintf('administrator::columns.%s.%s', 'global', $this->id); |
||
456 | } |
||
457 | |||
458 | return $key; |
||
459 | } |
||
460 | |||
461 | /** |
||
462 | * @return string |
||
463 | */ |
||
464 | public function descriptionKey() |
||
473 | } |
||
474 | } |
||
475 |