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 |
||
16 | class DatatableView |
||
17 | { |
||
18 | |||
19 | /** @var array the columns map with name -> label */ |
||
20 | private $columns; |
||
21 | |||
22 | /** @var bool Indicates if the columnConfigurations should be reset on a call to columns. */ |
||
23 | private $resetColumns = true; |
||
24 | |||
25 | /** @var string The view that should be used to render the table */ |
||
26 | private $tableView; |
||
27 | |||
28 | /** @var string The view that should be used to render the script */ |
||
29 | private $scriptView; |
||
30 | |||
31 | /** @var string The id that the table will get in the DOM. Used to create a fitting script for the table */ |
||
32 | private $tableId; |
||
33 | |||
34 | /** @var array An array of options that should be noted in the script view of the table */ |
||
35 | private $scriptOptions = []; |
||
36 | |||
37 | /** @var array An array of callback that should be noted in the script view of the table. Only differs in encoding */ |
||
38 | private $scriptCallbacks = []; |
||
39 | |||
40 | /** @var Factory The factory responsible to render the view with the given data */ |
||
41 | private $viewFactory; |
||
42 | |||
43 | /** @var bool true if the columns are also printed as headers on the table, false otherwise */ |
||
44 | private $printHeaders = false; |
||
45 | |||
46 | /** @var Repository The repository responsible for the config value resolution */ |
||
47 | private $configRepository; |
||
48 | |||
49 | /** @var string the URL for the endpoint */ |
||
50 | private $endpointURL = '/'; |
||
51 | |||
52 | /** |
||
53 | * DatatableView constructor, will take a view as a string if a custom one should be used. will also take the |
||
54 | * column configurations to provide out of the box headers for the view. |
||
55 | * If no columns are given the user must provide them before building the view. |
||
56 | * @param string|null $tableView the name of the view that should be rendered for the table |
||
57 | * @param string|null $scriptView the name of the view that should be rendered for the script |
||
58 | * @param Factory $viewFactory The factory used to render the views |
||
59 | * @param Repository $configRepository The repository responsible for config resolution |
||
60 | * @param ColumnConfiguration[] $columnConfiguration The columnConfiguration of the the server side if available |
||
61 | */ |
||
62 | public function __construct( |
||
79 | |||
80 | /** |
||
81 | * Will set a new id on the table. |
||
82 | * @param string $tableId The new id that should be used for the DOM table |
||
83 | * @return $this |
||
84 | */ |
||
85 | public function id($tableId) |
||
93 | |||
94 | /** |
||
95 | * @param string $name the name of the option |
||
96 | * @param mixed $options an array of options |
||
97 | * @return $this |
||
98 | */ |
||
99 | public function option($name, $options) |
||
104 | |||
105 | /** |
||
106 | * @param string $name the name of the callback function |
||
107 | * @param string $callback the body of the callback function |
||
108 | * @return $this |
||
109 | */ |
||
110 | public function callback($name, $callback) |
||
115 | |||
116 | /** |
||
117 | * Indicates that the current columns should have a header on the table |
||
118 | * @return $this |
||
119 | */ |
||
120 | public function headers() |
||
125 | |||
126 | /** |
||
127 | * Sets the endpoint URL that will be passed to templates when rendering html & scripts. |
||
128 | * @param $endpoint_url |
||
129 | * @return $this |
||
130 | */ |
||
131 | public function endpoint($endpoint_url) |
||
136 | |||
137 | /** |
||
138 | * Will set the columns for the view |
||
139 | * @param string $columnName The name of the column |
||
140 | * @param string $label The label for this column |
||
141 | * @return $this |
||
142 | */ |
||
143 | public function columns($columnName, $label = null) |
||
159 | |||
160 | /** |
||
161 | * Will render the table |
||
162 | * |
||
163 | * @return string the rendered view that represents the table |
||
164 | */ |
||
165 | View Code Duplication | public function table() |
|
180 | |||
181 | /** |
||
182 | * Will render the javascript for the table |
||
183 | * |
||
184 | * @return string the rendered view that represents the script |
||
185 | */ |
||
186 | View Code Duplication | public function script() |
|
201 | |||
202 | /** |
||
203 | * Will render the table and directly the script after it. This is a shortcut for |
||
204 | * {@link #table} followed by {@link script} |
||
205 | * @return string Will return the rendered table and the rendered script as string |
||
206 | */ |
||
207 | public function html() |
||
211 | } |
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.