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 |
||
21 | class AttachmentErrorContext |
||
22 | { |
||
23 | /** |
||
24 | * Holds our static instance of the class |
||
25 | * @var object |
||
26 | */ |
||
27 | private static $_context = null; |
||
28 | |||
29 | /** |
||
30 | * Holds all of the attachment ids |
||
31 | * @var array |
||
32 | */ |
||
33 | private $_attachs = null; |
||
34 | |||
35 | /** |
||
36 | * Holds any errors found |
||
37 | * @var ErrorContext|null |
||
38 | */ |
||
39 | private $_generic_error = null; |
||
40 | |||
41 | /** |
||
42 | * Holds if the error is generic of specific to an attachment |
||
43 | * @var string |
||
44 | */ |
||
45 | private $_active_attach = null; |
||
46 | |||
47 | /** |
||
48 | * Add attachment |
||
49 | * |
||
50 | * - Automatically activate the attachments added |
||
51 | * |
||
52 | * @param string $id |
||
53 | * @param string $name |
||
54 | */ |
||
55 | public function addAttach($id, $name) |
||
73 | |||
74 | /** |
||
75 | * Sets the active attach (errors are "attached" to that) |
||
76 | * |
||
77 | * @param string|null $id A valid attachment, if invalid it defaults to 'generic' |
||
78 | */ |
||
79 | public function activate($id = null) |
||
88 | |||
89 | /** |
||
90 | * Add an error |
||
91 | * |
||
92 | * @param string $error error code |
||
93 | * @param string|null $lang_file = null |
||
94 | */ |
||
95 | public function addError($error, $lang_file = null) |
||
111 | |||
112 | /** |
||
113 | * Removes an error |
||
114 | * |
||
115 | * @param string $error error code |
||
116 | */ |
||
117 | public function removeError($error) |
||
124 | |||
125 | /** |
||
126 | * If this error context has errors stored. |
||
127 | * |
||
128 | * @param string|null $attachID |
||
129 | * @param int|null $severity the severity level |
||
130 | */ |
||
131 | View Code Duplication | public function hasErrors($attachID = null, $severity = null) |
|
153 | |||
154 | /** |
||
155 | * If this error context has a particular error code. |
||
156 | * |
||
157 | * @param string $error_code the code of the error |
||
158 | * @param string|null $attachID |
||
159 | */ |
||
160 | View Code Duplication | public function hasError($error_code, $attachID = null) |
|
187 | |||
188 | /** |
||
189 | * Prepare the errors for display. |
||
190 | * |
||
191 | * - Return an array containing the error strings |
||
192 | * - If severity is null the function returns all the errors |
||
193 | * |
||
194 | * @param int|null $severity = null the severity level wanted |
||
195 | */ |
||
196 | public function prepareErrors($severity = null) |
||
223 | |||
224 | public function getName() |
||
228 | |||
229 | /** |
||
230 | * Return the type of the error |
||
231 | */ |
||
232 | public function getErrorType() |
||
236 | |||
237 | /** |
||
238 | * Find and return Attachment_ErrorContext instance if it exists, |
||
239 | * or create it if it doesn't exist |
||
240 | */ |
||
241 | public static function context() |
||
248 | } |
||
249 |