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) |
|
30 | { |
||
31 | if (func_num_args() > 1) { |
||
32 | $message = $this->_prepareLogMessage(func_get_args()); |
||
33 | } |
||
34 | |||
35 | return $this->_log(Zend_Log::ERR, $message); |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * Logs warnings |
||
40 | * |
||
41 | * @param $message |
||
42 | * @return string |
||
43 | */ |
||
44 | View Code Duplication | public function logWarn($message) |
|
45 | { |
||
46 | if (func_num_args() > 1) { |
||
47 | $message = $this->_prepareLogMessage(func_get_args()); |
||
48 | } |
||
49 | |||
50 | return $this->_log(Zend_Log::WARN, $message); |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * Logs notices |
||
55 | * |
||
56 | * @param $message |
||
57 | * @return string |
||
58 | */ |
||
59 | View Code Duplication | public function logNotice($message) |
|
60 | { |
||
61 | if (func_num_args() > 1) { |
||
62 | $message = $this->_prepareLogMessage(func_get_args()); |
||
63 | } |
||
64 | |||
65 | return $this->_log(Zend_Log::NOTICE, $message); |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * Logs info. |
||
70 | * |
||
71 | * @param $message |
||
72 | * @return string |
||
73 | */ |
||
74 | View Code Duplication | public function logInfo($message) |
|
75 | { |
||
76 | if (func_num_args() > 1) { |
||
77 | $message = $this->_prepareLogMessage(func_get_args()); |
||
78 | } |
||
79 | |||
80 | return $this->_log(Zend_Log::INFO, $message); |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Logs debug. |
||
85 | * |
||
86 | * @param $message |
||
87 | * @return string |
||
88 | */ |
||
89 | public function logDebug($message) |
||
90 | { |
||
91 | if ( ! Mage::helper('turpentine/varnish')->getVarnishDebugEnabled()) { |
||
92 | return; |
||
93 | } |
||
94 | |||
95 | if (func_num_args() > 1) { |
||
96 | $message = $this->_prepareLogMessage(func_get_args()); |
||
97 | } |
||
98 | |||
99 | return $this->_log(Zend_Log::DEBUG, $message); |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * Prepares advanced log message. |
||
104 | * |
||
105 | * @param array $args |
||
106 | * @return string |
||
107 | */ |
||
108 | protected function _prepareLogMessage(array $args) |
||
109 | { |
||
110 | $pattern = $args[0]; |
||
111 | $substitutes = array_slice($args, 1); |
||
112 | |||
113 | if ( ! $this->_validatePattern($pattern, $substitutes)) { |
||
114 | return $pattern; |
||
115 | } |
||
116 | |||
117 | return vsprintf($pattern, $substitutes); |
||
118 | } |
||
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 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.