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 | * Magic __get method |
||
33 | * |
||
34 | * @param $proporty |
||
35 | */ |
||
36 | public function __get($property) |
||
43 | /** |
||
44 | * Renders and returns output for the given file with its array of data. |
||
45 | * |
||
46 | * @param string $filePath |
||
47 | * @param array $data |
||
48 | * @return string Rendered output |
||
49 | * |
||
50 | */ |
||
51 | public function render($filePath, $data = null){ |
||
66 | |||
67 | /** |
||
68 | * Renders and returns output with header and footer for the given file with its array of data. |
||
69 | * |
||
70 | * @param string $layoutDir |
||
71 | * @param string $filePath |
||
72 | * @param array $data |
||
73 | * @return string Rendered output |
||
74 | */ |
||
75 | public function renderWithLayouts($layoutDir, $filePath, $data = null){ |
||
93 | |||
94 | /** |
||
95 | * Render a JSON view. |
||
96 | * |
||
97 | * @param array $data |
||
98 | * @return string Rendered output |
||
99 | * |
||
100 | */ |
||
101 | public function renderJson($data){ |
||
108 | |||
109 | /** |
||
110 | * Renders errors |
||
111 | * A json respond will be sent in case of ajax call |
||
112 | * |
||
113 | * @param array $errors |
||
114 | * @return mixed Rendered output |
||
115 | */ |
||
116 | View Code Duplication | public function renderErrors($errors){ |
|
127 | |||
128 | /** |
||
129 | * Renders success message |
||
130 | * A json respond will be sent in case of ajax call |
||
131 | * |
||
132 | * @param string $message |
||
133 | * @return mixed Rendered output |
||
134 | */ |
||
135 | View Code Duplication | public function renderSuccess($message){ |
|
146 | |||
147 | /** *********************************************** **/ |
||
148 | /** ************** JSON View ************** **/ |
||
149 | /** *********************************************** **/ |
||
150 | |||
151 | /** |
||
152 | * Serialize array to JSON and used for the response |
||
153 | * |
||
154 | * @param array $data |
||
155 | * @return string Rendered output |
||
156 | * |
||
157 | */ |
||
158 | public function jsonEncode($data){ |
||
161 | |||
162 | /** *********************************************** **/ |
||
163 | /** ************** Text Helper ************** **/ |
||
164 | /** *********************************************** **/ |
||
165 | |||
166 | /** |
||
167 | * Cuts a string to the length of $length and replaces the last characters |
||
168 | * with the ellipsis => '...' if the text is longer than length. |
||
169 | * |
||
170 | * @param string $str |
||
171 | * @param string $len |
||
172 | * @return string the truncated string |
||
173 | */ |
||
174 | public function truncate($str, $len){ |
||
184 | |||
185 | /** |
||
186 | * formats timestamp string coming from the database to "Month Day, Year" |
||
187 | * |
||
188 | * @param string $timestamp MySQL TIMESTAMP |
||
189 | * @return string Date after formatting. |
||
190 | */ |
||
191 | public function timestamp($timestamp){ |
||
199 | |||
200 | /** |
||
201 | * formats Unix timestamp string to "Month Day, Year" |
||
202 | * |
||
203 | * @param integer $unixtime Unix timestamp |
||
204 | * @return string Date after formatting. |
||
205 | */ |
||
206 | public function unixtime($unixtime){ |
||
211 | |||
212 | /** |
||
213 | * formats Unix timestamp to be used in Date Picker in form of: "day/month/year" |
||
214 | * |
||
215 | * @param integer $unixtime Unix timestamp |
||
216 | * @return string Date after formatting. |
||
217 | */ |
||
218 | public function datePicker($unixtime){ |
||
223 | |||
224 | /** |
||
225 | * Surround the links in a context with anchor tags. |
||
226 | * |
||
227 | * @param string $str |
||
228 | * @return string |
||
229 | * @see http://stackoverflow.com/questions/5341168/best-way-to-make-links-clickable-in-block-of-text |
||
230 | */ |
||
231 | public function autoLinks($str){ |
||
234 | |||
235 | /** |
||
236 | * Converts characters to HTML entities |
||
237 | * This is important to avoid XSS attacks, and attempts to inject malicious code in your page. |
||
238 | * |
||
239 | * @param string $str The string. |
||
240 | * @return string |
||
241 | */ |
||
242 | public function encodeHTML($str){ |
||
245 | |||
246 | /** |
||
247 | * It's same as encodeHTML(), But, also use nl2br() function in PHP |
||
248 | * |
||
249 | * @param string The string. |
||
250 | * @return string The string after converting characters and inserting br tags. |
||
251 | */ |
||
252 | public function encodeHTMLWithBR($str){ |
||
255 | |||
256 | } |
||
257 |
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.