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:
| 1 | <?php |
||
| 9 | class Wysiwyg extends AbstractField |
||
| 10 | { |
||
| 11 | use Orderable; |
||
| 12 | use Translatable; |
||
| 13 | |||
| 14 | const SUMMERNOTE = 'summernote'; |
||
| 15 | const TINYMCE = 'tinymce'; |
||
| 16 | |||
| 17 | protected $type = self::SUMMERNOTE; |
||
| 18 | protected $allowedTypes = [ |
||
| 19 | self::SUMMERNOTE, |
||
| 20 | self::TINYMCE, |
||
| 21 | ]; |
||
| 22 | private $options = []; |
||
| 23 | |||
| 24 | 2 | public function value(Request $request) |
|
| 30 | |||
| 31 | 3 | public function type(string $type) |
|
| 39 | |||
| 40 | 8 | public function getType() |
|
| 44 | |||
| 45 | 1 | public function options(array $options) |
|
| 51 | |||
| 52 | 1 | public function getOptions(): array |
|
| 65 | |||
| 66 | 2 | View Code Duplication | public function getListView($model) |
| 67 | { |
||
| 68 | 2 | $template = 'list'; |
|
| 69 | 2 | if ($this->isTranslatable()) { |
|
| 70 | 1 | $template .= '_translatable'; |
|
| 71 | } |
||
| 72 | |||
| 73 | 2 | return view(sprintf('jarboe::crud.fields.wysiwyg.%s.%s', $this->getType(), $template), [ |
|
| 74 | 2 | 'model' => $model, |
|
| 75 | 2 | 'field' => $this, |
|
| 76 | ]); |
||
| 77 | } |
||
| 78 | |||
| 79 | 1 | public function getEditFormView($model) |
|
| 80 | { |
||
| 81 | 1 | $template = $this->isReadonly() ? 'readonly' : 'edit'; |
|
| 82 | |||
| 83 | 1 | return view(sprintf('jarboe::crud.fields.wysiwyg.%s.%s', $this->getType(), $template), [ |
|
| 84 | 1 | 'model' => $model, |
|
| 85 | 1 | 'field' => $this, |
|
| 86 | ]); |
||
| 87 | } |
||
| 88 | |||
| 89 | 1 | public function getCreateFormView() |
|
| 90 | { |
||
| 91 | 1 | return view(sprintf('jarboe::crud.fields.wysiwyg.%s.create', $this->getType()), [ |
|
| 92 | 1 | 'field' => $this, |
|
| 93 | ]); |
||
| 94 | } |
||
| 95 | |||
| 96 | 1 | private function summernoteDefaultOptions(): array |
|
| 116 | |||
| 117 | 1 | private function tinymceDefaultOptions(): array |
|
| 124 | } |
||
| 125 |