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 |
||
| 21 | class XsltBlock extends AbstractBlockHandler |
||
| 22 | { |
||
| 23 | public function display(array $properties) |
||
| 24 | { |
||
| 25 | if (!$this->hasPermission('xsltblock::', "$properties[title]::", ACCESS_OVERVIEW)) { |
||
| 26 | return ''; |
||
| 27 | } |
||
| 28 | |||
| 29 | $doc = new DOMDocument(); |
||
| 30 | $xsl = new XSLTProcessor(); |
||
| 31 | |||
| 32 | // load stylesheet |
||
| 33 | View Code Duplication | if (isset($properties['styleurl']) && !empty($properties['styleurl'])) { |
|
|
|
|||
| 34 | $doc->load($properties['styleurl']); |
||
| 35 | } else { |
||
| 36 | $doc->loadXML($properties['stylecontents']); |
||
| 37 | } |
||
| 38 | $xsl->importStyleSheet($doc); |
||
| 39 | |||
| 40 | // load xml source |
||
| 41 | View Code Duplication | if (isset($properties['docurl']) && !empty($properties['docurl'])) { |
|
| 42 | $doc->load($properties['docurl']); |
||
| 43 | } else { |
||
| 44 | $doc->loadXML($properties['doccontents']); |
||
| 45 | } |
||
| 46 | |||
| 47 | // apply stylesheet and return output |
||
| 48 | return $xsl->transformToXML($doc); |
||
| 49 | } |
||
| 50 | |||
| 51 | public function getFormClassName() |
||
| 55 | |||
| 56 | public function getFormTemplate() |
||
| 60 | |||
| 61 | public function getFormOptions() |
||
| 67 | } |
||
| 68 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.