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