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 |
||
12 | class View { |
||
|
|||
13 | |||
14 | /** |
||
15 | * controller object that instantiated view object |
||
16 | * |
||
17 | * @var object |
||
18 | */ |
||
19 | public $controller; |
||
20 | |||
21 | /** |
||
22 | * Constructor |
||
23 | * |
||
24 | * @param Controller $controller |
||
25 | */ |
||
26 | public function __construct(Controller $controller){ |
||
30 | |||
31 | /** |
||
32 | * Renders and returns output for the given file with its array of data. |
||
33 | * |
||
34 | * @param string $filePath |
||
35 | * @param array $data |
||
36 | * @return string Rendered output |
||
37 | * |
||
38 | */ |
||
39 | public function render($filePath, $data = null){ |
||
54 | |||
55 | /** |
||
56 | * Renders and returns output with header and footer for the given file with its array of data. |
||
57 | * |
||
58 | * @param string $layoutDir |
||
59 | * @param string $filePath |
||
60 | * @param array $data |
||
61 | * @return string Rendered output |
||
62 | */ |
||
63 | public function renderWithLayouts($layoutDir, $filePath, $data = null){ |
||
81 | |||
82 | /** |
||
83 | * Render a JSON view. |
||
84 | * |
||
85 | * @param array $data |
||
86 | * @return string Rendered output |
||
87 | * |
||
88 | */ |
||
89 | public function renderJson($data = null){ |
||
96 | |||
97 | /** |
||
98 | * Renders errors |
||
99 | * A json respond will be sent in case of ajax call |
||
100 | * |
||
101 | * @param array $errors |
||
102 | * @return mixed Rendered output |
||
103 | */ |
||
104 | View Code Duplication | public function renderErrors($errors){ |
|
115 | |||
116 | /** |
||
117 | * Renders success message |
||
118 | * A json respond will be sent in case of ajax call |
||
119 | * |
||
120 | * @param string $message |
||
121 | * @return mixed Rendered output |
||
122 | */ |
||
123 | View Code Duplication | public function renderSuccess($message){ |
|
134 | |||
135 | /** *********************************************** **/ |
||
136 | /** ************** JSON View ************** **/ |
||
137 | /** *********************************************** **/ |
||
138 | |||
139 | /** |
||
140 | * Serialize array to JSON and used for the response |
||
141 | * |
||
142 | * @param array $data |
||
143 | * @return string Rendered output |
||
144 | * |
||
145 | */ |
||
146 | public function jsonEncode($data){ |
||
149 | |||
150 | /** *********************************************** **/ |
||
151 | /** ************** Text Helper ************** **/ |
||
152 | /** *********************************************** **/ |
||
153 | |||
154 | /** |
||
155 | * Cuts a string to the length of $length and replaces the last characters |
||
156 | * with the ellipsis => '...' if the text is longer than length. |
||
157 | * |
||
158 | * @param string $str |
||
159 | * @param string $len |
||
160 | * @return string the truncated string |
||
161 | */ |
||
162 | public function truncate($str, $len){ |
||
172 | |||
173 | /** |
||
174 | * formats timestamp string coming from the database to "Month Day, Year" |
||
175 | * |
||
176 | * @param string $timestamp MySQL TIMESTAMP |
||
177 | * @return string Date after formatting. |
||
178 | */ |
||
179 | public function timestamp($timestamp){ |
||
187 | |||
188 | /** |
||
189 | * formats Unix timestamp string to "Month Day, Year" |
||
190 | * |
||
191 | * @param integer $unixtime Unix timestamp |
||
192 | * @return string Date after formatting. |
||
193 | */ |
||
194 | public function unixtime($unixtime){ |
||
199 | |||
200 | /** |
||
201 | * formats Unix timestamp to be used in Date Picker in form of: "day/month/year" |
||
202 | * |
||
203 | * @param integer $unixtime Unix timestamp |
||
204 | * @return string Date after formatting. |
||
205 | */ |
||
206 | public function datePicker($unixtime){ |
||
211 | |||
212 | /** |
||
213 | * Surround the links in a context with anchor tags. |
||
214 | * |
||
215 | * @param string $str |
||
216 | * @return string |
||
217 | * @see http://stackoverflow.com/questions/5341168/best-way-to-make-links-clickable-in-block-of-text |
||
218 | */ |
||
219 | public function autoLinks($str){ |
||
222 | |||
223 | /** |
||
224 | * Converts characters to HTML entities |
||
225 | * This is important to avoid XSS attacks, and attempts to inject malicious code in your page. |
||
226 | * |
||
227 | * @param string $str The string. |
||
228 | * @return string |
||
229 | */ |
||
230 | public function encodeHTML($str){ |
||
233 | |||
234 | /** |
||
235 | * It's same as encodeHTML(), But, also use nl2br() function in PHP |
||
236 | * |
||
237 | * @param string The string. |
||
238 | * @return string The string after converting characters and inserting br tags. |
||
239 | */ |
||
240 | public function encodeHTMLWithBR($str){ |
||
243 | |||
244 | } |
||
245 |
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.