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 |
||
53 | class SmallTextBox |
||
54 | { |
||
55 | /** |
||
56 | * The string object that will be edited by this string box. |
||
57 | * |
||
58 | * @var Alpha\Model\Type\SmallText |
||
59 | * |
||
60 | * @since 1.0 |
||
61 | */ |
||
62 | public $stringObject; |
||
63 | |||
64 | /** |
||
65 | * The data label for the string object. |
||
66 | * |
||
67 | * @var string |
||
68 | * |
||
69 | * @since 1.0 |
||
70 | */ |
||
71 | public $label; |
||
72 | |||
73 | /** |
||
74 | * The name of the HTML input box. |
||
75 | * |
||
76 | * @var string |
||
77 | * |
||
78 | * @since 1.0 |
||
79 | */ |
||
80 | public $name; |
||
81 | |||
82 | /** |
||
83 | * The display size of the input box. |
||
84 | * |
||
85 | * @var int |
||
86 | * |
||
87 | * @since 1.0 |
||
88 | */ |
||
89 | public $size; |
||
90 | |||
91 | /** |
||
92 | * The constructor. |
||
93 | * |
||
94 | * @param Alpha\Model\Type\SmallText $string The string object that will be edited by this text box. |
||
95 | * @param string $label The data label for the string object. |
||
96 | * @param string $name The name of the HTML input box. |
||
97 | * @param int $size The display size (characters). |
||
98 | * |
||
99 | * @since 1.0 |
||
100 | * |
||
101 | * @throws Alpha\Exception\IllegalArguementException |
||
102 | */ |
||
103 | View Code Duplication | public function __construct($string, $label, $name, $size = 0) |
|
122 | |||
123 | /** |
||
124 | * Renders the HTML and javascript for the string box. |
||
125 | * |
||
126 | * @param bool $readOnly set to true to make the text box readonly (defaults to false) |
||
127 | * |
||
128 | * @return string |
||
129 | * |
||
130 | * @since 1.0 |
||
131 | */ |
||
132 | public function render($readOnly = false) |
||
148 | |||
149 | /** |
||
150 | * Setter for string object. |
||
151 | * |
||
152 | * @param Alpha\Model\Type\SmallText $string |
||
153 | * |
||
154 | * @since 1.0 |
||
155 | * |
||
156 | * @throws Alpha\Exception\IllegalArguementException |
||
157 | */ |
||
158 | public function setStringObject($string) |
||
166 | |||
167 | /** |
||
168 | * Getter for string object. |
||
169 | * |
||
170 | * @return Alpha\Model\Type\SmallText |
||
171 | * |
||
172 | * @since 1.0 |
||
173 | */ |
||
174 | public function getStringObject() |
||
178 | } |
||
179 |
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.