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 |
||
10 | abstract class Dispatcher extends DNRoot { |
||
11 | |||
12 | /** |
||
13 | * The CSRF token name that is used for all frondend dispatchers |
||
14 | */ |
||
15 | const SECURITY_TOKEN_NAME = 'DispatcherSecurityID'; |
||
16 | |||
17 | /** |
||
18 | * Generate the data structure used by the frontend component. |
||
19 | * |
||
20 | * @param string $name of the component |
||
21 | * @return array |
||
22 | */ |
||
23 | abstract public function getModel($name); |
||
24 | |||
25 | /** |
||
26 | * Renders the initial HTML needed to bootstrap the react component. |
||
27 | * |
||
28 | * Usage: $getReactComponent(YourComponent); |
||
29 | * |
||
30 | * @param string $name Used to name the DOM elements and obtain the initial model. |
||
31 | * @return string A snippet good for adding to a SS template. |
||
32 | */ |
||
33 | public function getReactComponent($name) { |
||
42 | |||
43 | /** |
||
44 | * We want to separate the dispatchers security token from the static HTML |
||
45 | * security token since it's possible that they get out of sync with eachother. |
||
46 | * |
||
47 | * We do this by giving the token a separate name. |
||
48 | * |
||
49 | * Don't manually reset() this token, that will cause issues when people have |
||
50 | * several tabs open. The token will be recreated when the user session times |
||
51 | * out. |
||
52 | * |
||
53 | * @return SecurityToken |
||
54 | */ |
||
55 | protected function getSecurityToken() { |
||
58 | |||
59 | /** |
||
60 | * @see getSecurityToken() |
||
61 | */ |
||
62 | protected function checkSecurityToken() { |
||
68 | |||
69 | /** |
||
70 | * Return the validator errors as AJAX response. |
||
71 | * |
||
72 | * @param int $code HTTP status code. |
||
73 | * @param array $validatorErrors Result of calling Validator::validate, e.g. |
||
74 | * [{"fieldName":"Name","message":"Message.","messageType":"bad"}] |
||
75 | * @return \SS_HTTPResponse |
||
76 | */ |
||
77 | public function asJSONValidatorErrors($code, $validatorErrors) { |
||
84 | |||
85 | /** |
||
86 | * Return field-specific errors as AJAX response. |
||
87 | * |
||
88 | * @param int $code HTTP status code. |
||
89 | * @param array $fieldErrors FieldName => message structure. |
||
90 | * @return \SS_HTTPResponse |
||
91 | */ |
||
92 | public function asJSONFormFieldErrors($code, $fieldErrors) { |
||
101 | |||
102 | /** |
||
103 | * Respond to an AJAX request. |
||
104 | * Automatically updates the security token and proxy pending redirects. |
||
105 | * |
||
106 | * @deprecated the use of getAPIResponse() is encouraged |
||
107 | * @param array $data Data to be passed to the frontend. |
||
108 | * |
||
109 | * @return SS_HTTPResponse |
||
110 | */ |
||
111 | public function asJSON($data = []) { |
||
135 | |||
136 | /** |
||
137 | * Return an XHR response object without any CSRF token information |
||
138 | * |
||
139 | * @param array $output |
||
140 | * @param int $statusCode |
||
141 | * @return SS_HTTPResponse |
||
142 | */ |
||
143 | protected function getAPIResponse($output, $statusCode) { |
||
151 | |||
152 | /** |
||
153 | * Decode the data submitted by the Form.jsx control. |
||
154 | * |
||
155 | * @return array |
||
156 | */ |
||
157 | protected function getFormData() { |
||
160 | |||
161 | /** |
||
162 | * @param string|array |
||
163 | * @return string|array |
||
164 | */ |
||
165 | View Code Duplication | protected function trimWhitespace($val) { |
|
173 | |||
174 | /** |
||
175 | * Remove control characters from the input. |
||
176 | * |
||
177 | * @param string|array |
||
178 | * @return string|array |
||
179 | */ |
||
180 | View Code Duplication | protected function stripNonPrintables($val) { |
|
188 | |||
189 | } |
||
190 |
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.