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 |
||
22 | class Nexcessnet_Turpentine_Helper_Debug extends Mage_Core_Helper_Abstract { |
||
23 | /** |
||
24 | * Logs errors |
||
25 | * |
||
26 | * @param $message |
||
27 | * @return string |
||
28 | */ |
||
29 | View Code Duplication | public function logError($message) |
|
37 | |||
38 | /** |
||
39 | * Logs warnings |
||
40 | * |
||
41 | * @param $message |
||
42 | * @return string |
||
43 | */ |
||
44 | View Code Duplication | public function logWarn($message) |
|
52 | |||
53 | /** |
||
54 | * Logs notices |
||
55 | * |
||
56 | * @param $message |
||
57 | * @return string |
||
58 | */ |
||
59 | View Code Duplication | public function logNotice($message) |
|
67 | |||
68 | /** |
||
69 | * Logs info. |
||
70 | * |
||
71 | * @param $message |
||
72 | * @return string |
||
73 | */ |
||
74 | View Code Duplication | public function logInfo($message) |
|
82 | |||
83 | /** |
||
84 | * Logs debug. |
||
85 | * |
||
86 | * @param $message |
||
87 | * @return string |
||
88 | */ |
||
89 | public function logDebug($message) |
||
101 | |||
102 | /** |
||
103 | * Prepares advanced log message. |
||
104 | * |
||
105 | * @param array $args |
||
106 | * @return string |
||
107 | */ |
||
108 | protected function _prepareLogMessage(array $args) |
||
119 | |||
120 | /** |
||
121 | * Validates string and attributes for substitution as per sprintf function. |
||
122 | * |
||
123 | * NOTE: this could be implemented as shown at |
||
124 | * http://stackoverflow.com/questions/2053664/how-to-check-that-vsprintf-has-the-correct-number-of-arguments-before-running |
||
125 | * although IMHO it's too time consuming to validate the patterns. |
||
126 | * |
||
127 | * @param string $pattern |
||
128 | * @param array $arguments |
||
129 | * @return bool |
||
130 | */ |
||
131 | protected function _validatePattern($pattern, $arguments) |
||
135 | |||
136 | /** |
||
137 | * Dump a variable to output with <pre/> tags and disable cache flag |
||
138 | * |
||
139 | * @param mixed $value |
||
140 | */ |
||
141 | public function dump($value) { |
||
148 | |||
149 | /** |
||
150 | * Log message through Magento's logging facility, works like sprintf |
||
151 | * |
||
152 | * @param string $message |
||
153 | * @param mixed ... |
||
154 | * @return null |
||
155 | */ |
||
156 | public function log($message) { |
||
160 | |||
161 | /** |
||
162 | * Log a backtrace, can pass a already generated backtrace to use |
||
163 | * |
||
164 | * @param array $backTrace |
||
165 | * @return null |
||
166 | */ |
||
167 | public function logBackTrace($backTrace = null) { |
||
188 | |||
189 | /** |
||
190 | * Like var_dump to the log |
||
191 | * |
||
192 | * @param mixed $value |
||
193 | * @return null |
||
194 | */ |
||
195 | public function logValue($value, $name = null) { |
||
202 | |||
203 | /** |
||
204 | * Log a message through Magento's logging facility |
||
205 | * |
||
206 | * @param int $level |
||
207 | * @param string $message |
||
208 | * @return string |
||
209 | */ |
||
210 | protected function _log($level, $message) { |
||
215 | |||
216 | /** |
||
217 | * Get the name of the log file to use |
||
218 | * @return string |
||
219 | */ |
||
220 | protected function _getLogFileName() { |
||
226 | |||
227 | /** |
||
228 | * Check if custom log file should be used |
||
229 | * @return bool |
||
230 | */ |
||
231 | public function useCustomLogFile() { |
||
235 | |||
236 | /** |
||
237 | * Get custom log file name |
||
238 | * @return string |
||
239 | */ |
||
240 | public function getCustomLogFileName() { |
||
244 | |||
245 | /** |
||
246 | * Format a list of function arguments for the backtrace |
||
247 | * |
||
248 | * @param array $args |
||
249 | * @return string |
||
250 | */ |
||
251 | protected function _backtrace_formatArgs($args) { |
||
259 | |||
260 | /** |
||
261 | * Format a value for inclusion in the backtrace |
||
262 | * |
||
263 | * @param mixed $arg |
||
264 | * @return null |
||
265 | */ |
||
266 | protected function _backtrace_formatArgsHelper($arg) { |
||
289 | } |
||
290 |
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.