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 |
||
11 | class SelectAjax extends Select implements Initializable, WithRoutesInterface |
||
12 | { |
||
13 | protected static $route = 'selectajax'; |
||
14 | protected $view = 'form.element.selectajax'; |
||
15 | protected $search_url = null; |
||
16 | protected $search = null; |
||
17 | |||
18 | /** |
||
19 | * @param string $path |
||
20 | * @param string|null $label |
||
21 | */ |
||
22 | View Code Duplication | public function __construct($path, $label = null) |
|
|
|||
23 | { |
||
24 | parent::__construct($path, $label); |
||
25 | |||
26 | $this->setLoadOptionsQueryPreparer(function ($item, Builder $query) { |
||
27 | $repository = app(RepositoryInterface::class); |
||
28 | $repository->setModel($this->getModelForOptions()); |
||
29 | $key = $repository->getModel()->getKeyName(); |
||
30 | |||
31 | return $query->where([$key => $this->getValueFromModel()]); |
||
32 | }); |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * @param Router $router |
||
37 | */ |
||
38 | 285 | View Code Duplication | public static function registerRoutes(Router $router) |
49 | |||
50 | /** |
||
51 | * @return null |
||
52 | */ |
||
53 | public function getSearch() |
||
61 | |||
62 | /** |
||
63 | * @param $search |
||
64 | * @return $this |
||
65 | */ |
||
66 | public function setSearch($search) |
||
72 | |||
73 | /** |
||
74 | * @param $url |
||
75 | * @return $this |
||
76 | */ |
||
77 | public function setSearchUrl($url) |
||
83 | |||
84 | /** |
||
85 | * @return array |
||
86 | */ |
||
87 | public function mutateOptions() |
||
91 | |||
92 | /** |
||
93 | * @return string |
||
94 | */ |
||
95 | View Code Duplication | public function getSearchUrl() |
|
103 | |||
104 | /** |
||
105 | * @return array |
||
106 | */ |
||
107 | public function toArray() |
||
122 | } |
||
123 |
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.