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 ElggWidget extends \ElggObject { |
||
17 | |||
18 | /** |
||
19 | * Set subtype to widget. |
||
20 | * |
||
21 | * @return void |
||
22 | */ |
||
23 | protected function initializeAttributes() { |
||
28 | |||
29 | /** |
||
30 | * Get a value from attributes or private settings |
||
31 | * |
||
32 | * @param string $name The name of the value |
||
33 | * @return mixed |
||
34 | */ |
||
35 | public function __get($name) { |
||
51 | |||
52 | /** |
||
53 | * Override entity get and sets in order to save data to private data store. |
||
54 | * |
||
55 | * @param string $name Name |
||
56 | * @return mixed |
||
57 | * @deprecated 1.9 |
||
58 | */ |
||
59 | public function get($name) { |
||
63 | |||
64 | /** |
||
65 | * Set an attribute or private setting value |
||
66 | * |
||
67 | * @param string $name The name of the value to set |
||
68 | * @param mixed $value The value to set |
||
69 | * @return void |
||
70 | */ |
||
71 | View Code Duplication | public function __set($name, $value) { |
|
83 | |||
84 | /** |
||
85 | * Override entity get and sets in order to save data to private data store. |
||
86 | * |
||
87 | * @param string $name Name |
||
88 | * @param string $value Value |
||
89 | * @return bool |
||
90 | * @deprecated 1.9 |
||
91 | */ |
||
92 | public function set($name, $value) { |
||
98 | |||
99 | /** |
||
100 | * Set the widget context |
||
101 | * |
||
102 | * @param string $context The widget context |
||
103 | * @return bool |
||
104 | * @since 1.8.0 |
||
105 | */ |
||
106 | public function setContext($context) { |
||
109 | |||
110 | /** |
||
111 | * Get the widget context |
||
112 | * |
||
113 | * @return string |
||
114 | * @since 1.8.0 |
||
115 | */ |
||
116 | public function getContext() { |
||
119 | |||
120 | /** |
||
121 | * Get the title of the widget |
||
122 | * |
||
123 | * @return string |
||
124 | * @since 1.8.0 |
||
125 | */ |
||
126 | public function getTitle() { |
||
133 | |||
134 | /** |
||
135 | * Move the widget |
||
136 | * |
||
137 | * @param int $column The widget column |
||
138 | * @param int $rank Zero-based rank from the top of the column |
||
139 | * @return void |
||
140 | * @since 1.8.0 |
||
141 | */ |
||
142 | public function move($column, $rank) { |
||
223 | |||
224 | /** |
||
225 | * Saves the widget's settings |
||
226 | * |
||
227 | * Plugins can override the save mechanism using the plugin hook: |
||
228 | * 'widget_settings', <widget handler identifier>. The widget and |
||
229 | * the parameters are passed. The plugin hook handler should return |
||
230 | * true to indicate that it has successfully saved the settings. |
||
231 | * |
||
232 | * @warning The values in the parameter array cannot be arrays |
||
233 | * |
||
234 | * @param array $params An array of name => value parameters |
||
235 | * |
||
236 | * @return bool |
||
237 | * @since 1.8.0 |
||
238 | */ |
||
239 | public function saveSettings($params) { |
||
268 | } |
||
269 |
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.