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 |
||
27 | class PedigreeUrlField extends PedigreeHtmlInputAbstract |
||
28 | { |
||
29 | // Define class variables |
||
30 | private $fieldnumber; |
||
31 | private $fieldname; |
||
32 | private $value; |
||
33 | private $defaultvalue; |
||
34 | private $lookuptable; |
||
35 | |||
36 | /** |
||
37 | * Constructor |
||
38 | * |
||
39 | * @param Field $parentObject |
||
40 | * @param PedigreeAnimal $animalObject |
||
41 | */ |
||
42 | View Code Duplication | public function __construct($parentObject, $animalObject) |
|
59 | |||
60 | /** |
||
61 | * @return XoopsFormText |
||
62 | */ |
||
63 | public function editField() |
||
69 | |||
70 | /** |
||
71 | * @param string $name |
||
72 | * |
||
73 | * @return XoopsFormText |
||
74 | */ |
||
75 | public function newField($name = '') |
||
81 | |||
82 | /** |
||
83 | * @return XoopsFormLabel |
||
84 | */ |
||
85 | public function viewField() |
||
91 | |||
92 | /** |
||
93 | * @return string |
||
94 | */ |
||
95 | public function showField() |
||
99 | |||
100 | /** |
||
101 | * @return string |
||
102 | */ |
||
103 | public function showValue() |
||
107 | |||
108 | /** |
||
109 | * @return string |
||
110 | */ |
||
111 | public function getSearchString() |
||
115 | } |
||
116 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.