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 SEO_Metadata_SiteTree_DataExtension extends DataExtension |
||
|
|||
13 | { |
||
14 | |||
15 | |||
16 | /* Overload Model |
||
17 | ------------------------------------------------------------------------------*/ |
||
18 | |||
19 | private static $db = array( |
||
20 | 'MetaTitle' => 'Varchar(128)', |
||
21 | 'MetaDescription' => 'Text', // redundant, but included for backwards-compatibility |
||
22 | 'ExtraMeta' => 'HTMLText', // redundant, but included for backwards-compatibility |
||
23 | ); |
||
24 | |||
25 | |||
26 | /* Overload Methods |
||
27 | ------------------------------------------------------------------------------*/ |
||
28 | |||
29 | // CMS Fields |
||
30 | public function updateCMSFields(FieldList $fields) |
||
82 | |||
83 | /** |
||
84 | * Main function to format & output metadata as an HTML string. |
||
85 | * |
||
86 | * Use the `updateMetadata($config, $owner, $metadata)` update hook when extending `DataExtension`s. |
||
87 | * |
||
88 | * @return string |
||
89 | */ |
||
90 | public function Metadata() |
||
146 | |||
147 | |||
148 | /* Helper Methods |
||
149 | ------------------------------------------------------------------------------*/ |
||
150 | |||
151 | /** |
||
152 | * Returns a given string as a HTML comment. |
||
153 | * |
||
154 | * @var string $comment |
||
155 | * |
||
156 | * @return string |
||
157 | */ |
||
158 | public function MarkupComment($comment) |
||
159 | { |
||
160 | |||
161 | // return |
||
162 | return '<!-- ' . $comment . ' -->' . PHP_EOL; |
||
163 | |||
164 | } |
||
165 | |||
166 | /** |
||
167 | * Returns markup for a HTML meta element. |
||
168 | * |
||
169 | * @var string $name |
||
170 | * @var string $content |
||
171 | * @var bool $encode |
||
172 | * |
||
173 | * @return string |
||
174 | */ |
||
175 | View Code Duplication | public function MarkupMeta($name, $content, $encode = false) |
|
176 | { |
||
177 | |||
178 | // return |
||
179 | return '<meta name="' . $name . '" content="' . $this->encodeContent($content, $encode) . '" />' . PHP_EOL; |
||
180 | |||
181 | } |
||
182 | |||
183 | /** |
||
184 | * Returns markup for a HTML link element. |
||
185 | * |
||
186 | * @param string $rel |
||
187 | * @param string $href |
||
188 | * @param string $type |
||
189 | * @param string $sizes |
||
190 | * |
||
191 | * @return string |
||
192 | */ |
||
193 | public function MarkupLink($rel, $href, $type = '', $sizes = '') |
||
210 | |||
211 | /** |
||
212 | * Returns markup for an Open Graph meta element. |
||
213 | * |
||
214 | * @var string $property |
||
215 | * @var string $content |
||
216 | * @var bool $encode |
||
217 | * |
||
218 | * @return string |
||
219 | */ |
||
220 | View Code Duplication | public function MarkupFacebook($property, $content, $encode = true) |
|
227 | |||
228 | /** |
||
229 | * Returns markup for a Twitter Cards meta element. |
||
230 | * |
||
231 | * @var string $name |
||
232 | * @var string $content |
||
233 | * @var bool $encode |
||
234 | * |
||
235 | * @return string |
||
236 | */ |
||
237 | View Code Duplication | public function MarkupTwitter($name, $content, $encode = true) |
|
244 | |||
245 | /** |
||
246 | * Returns markup for a Schema.org meta element. |
||
247 | * |
||
248 | * @var string $itemprop |
||
249 | * @var string $content |
||
250 | * @var bool $encode |
||
251 | * |
||
252 | * @return string |
||
253 | */ |
||
254 | View Code Duplication | public function MarkupSchema($itemprop, $content, $encode = true) |
|
261 | |||
262 | |||
263 | /* Meta Methods |
||
264 | ------------------------------------------------------------------------------*/ |
||
265 | |||
266 | /** |
||
267 | * Generates HTML title based on configuration settings. |
||
268 | * |
||
269 | * @return bool|string |
||
270 | */ |
||
271 | public function GenerateTitle() |
||
278 | |||
279 | /** |
||
280 | * Returns description from the page `MetaDescription`, or the first paragraph of the `Content` attribute. |
||
281 | * |
||
282 | * @return bool|string |
||
283 | */ |
||
284 | public function GenerateDescription() |
||
294 | |||
295 | /** |
||
296 | * Generates description from the first paragraph of the `Content` attribute. |
||
297 | * |
||
298 | * @return bool|string |
||
299 | */ |
||
300 | public function GenerateDescriptionFromContent() |
||
324 | |||
325 | /** |
||
326 | * Returns a plain or HTML-encoded string according to the current charset & encoding settings. |
||
327 | * |
||
328 | * @param string $content |
||
329 | * @param bool $encode |
||
330 | * |
||
331 | * @return string |
||
332 | */ |
||
333 | public function encodeContent($content, $encode = true) { |
||
340 | |||
341 | } |
||
342 |
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.