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 |
||
19 | class DataElement extends Widget { |
||
20 | |||
21 | public function __construct($identifier, $modelInstance=NULL) { |
||
22 | parent::__construct($identifier, null,$modelInstance); |
||
23 | $this->_init(new InstanceViewer($identifier), "table", new HtmlTable($identifier, 0,2), false); |
||
24 | $this->content["table"]->setDefinition(); |
||
25 | } |
||
26 | |||
27 | View Code Duplication | public function compile(JsUtils $js=NULL,&$view=NULL){ |
|
39 | |||
40 | /** |
||
41 | * @param HtmlTable $table |
||
42 | */ |
||
43 | protected function _generateContent($table){ |
||
51 | |||
52 | protected function _getFieldName($index){ |
||
55 | |||
56 | protected function _getFieldCaption($index){ |
||
59 | |||
60 | /** |
||
61 | * {@inheritDoc} |
||
62 | * @see \Ajax\common\Widget::getHtmlComponent() |
||
63 | * @return HtmlTable |
||
64 | */ |
||
65 | public function getHtmlComponent() { |
||
68 | |||
69 | /** |
||
70 | * {@inheritdoc} |
||
71 | * @see \Ajax\common\Widget::_setToolbarPosition() |
||
72 | */ |
||
73 | protected function _setToolbarPosition($table, $captions=NULL) { |
||
76 | |||
77 | /** |
||
78 | * The callback function called after the insertion of each row when fromDatabaseObjects is called |
||
79 | * callback function takes the parameters $row : the row inserted and $object: the instance of model used |
||
80 | * @param callable $callback |
||
81 | * @return DataElement |
||
82 | */ |
||
83 | public function onNewRow($callback) { |
||
87 | } |
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.