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:
Complex classes like AbstractAction often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AbstractAction, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | abstract class AbstractAction |
||
10 | { |
||
11 | const ROW_ID_PLACEHOLDER = ':rowId:'; |
||
12 | |||
13 | /** |
||
14 | * @var \ZfcDatagrid\Column\AbstractColumn[] |
||
15 | */ |
||
16 | protected $linkColumnPlaceholders = []; |
||
17 | |||
18 | /** |
||
19 | * @var array |
||
20 | */ |
||
21 | protected $htmlAttributes = []; |
||
22 | |||
23 | /** |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $showOnValueOperator = 'OR'; |
||
27 | |||
28 | /** |
||
29 | * @var array |
||
30 | */ |
||
31 | protected $showOnValues = []; |
||
32 | |||
33 | public function __construct() |
||
37 | |||
38 | /** |
||
39 | * Set the link. |
||
40 | * |
||
41 | * @param string $href |
||
42 | */ |
||
43 | public function setLink($href) |
||
47 | |||
48 | /** |
||
49 | * @return string |
||
50 | */ |
||
51 | public function getLink() |
||
55 | |||
56 | /** |
||
57 | * This is needed public for rowClickAction... |
||
58 | * |
||
59 | * @param array $row |
||
60 | * |
||
61 | * @return string |
||
62 | */ |
||
63 | public function getLinkReplaced(array $row) |
||
82 | |||
83 | /** |
||
84 | * Get the column row value placeholder |
||
85 | * $action->setLink('/myLink/something/id/'.$action->getRowIdPlaceholder().'/something/'.$action->getColumnRowPlaceholder($myCol));. |
||
86 | * |
||
87 | * @param AbstractColumn $col |
||
88 | * |
||
89 | * @return string |
||
90 | */ |
||
91 | public function getColumnValuePlaceholder(AbstractColumn $col) |
||
97 | |||
98 | /** |
||
99 | * @return \ZfcDatagrid\Column\AbstractColumn[] |
||
100 | */ |
||
101 | public function getLinkColumnPlaceholders() |
||
105 | |||
106 | /** |
||
107 | * Returns the rowId placeholder |
||
108 | * Can be used e.g. |
||
109 | * $action->setLink('/myLink/something/id/'.$action->getRowIdPlaceholder());. |
||
110 | * |
||
111 | * @return string |
||
112 | */ |
||
113 | public function getRowIdPlaceholder() |
||
117 | |||
118 | /** |
||
119 | * Set a HTML attributes. |
||
120 | * |
||
121 | * @param string $name |
||
122 | * @param string $value |
||
123 | */ |
||
124 | public function setAttribute($name, $value) |
||
128 | |||
129 | /** |
||
130 | * Get a HTML attribute. |
||
131 | * |
||
132 | * @param string $name |
||
133 | * |
||
134 | * @return string |
||
135 | */ |
||
136 | public function getAttribute($name) |
||
144 | |||
145 | /** |
||
146 | * Removes an HTML attribute. |
||
147 | * |
||
148 | * @param string $name |
||
149 | */ |
||
150 | public function removeAttribute($name) |
||
156 | |||
157 | /** |
||
158 | * Get all HTML attributes. |
||
159 | * |
||
160 | * @return array |
||
161 | */ |
||
162 | public function getAttributes() |
||
166 | |||
167 | /** |
||
168 | * Get the string version of the attributes. |
||
169 | * |
||
170 | * @param array $row |
||
171 | * |
||
172 | * @return string |
||
173 | */ |
||
174 | View Code Duplication | protected function getAttributesString(array $row) |
|
186 | |||
187 | /** |
||
188 | * Set the title attribute. |
||
189 | * |
||
190 | * @param string $name |
||
191 | */ |
||
192 | public function setTitle($name) |
||
196 | |||
197 | /** |
||
198 | * Get the title attribute. |
||
199 | * |
||
200 | * @return string |
||
201 | */ |
||
202 | public function getTitle() |
||
206 | |||
207 | /** |
||
208 | * Add a css class. |
||
209 | * |
||
210 | * @param string $className |
||
211 | */ |
||
212 | public function addClass($className) |
||
222 | |||
223 | /** |
||
224 | * Display the values with AND or OR (if multiple showOnValues are defined). |
||
225 | * |
||
226 | * @param string $operator |
||
227 | */ |
||
228 | View Code Duplication | public function setShowOnValueOperator($operator = 'OR') |
|
236 | |||
237 | /** |
||
238 | * Get the show on value operator, e.g. |
||
239 | * OR, AND. |
||
240 | * |
||
241 | * @return string |
||
242 | */ |
||
243 | public function getShowOnValueOperator() |
||
247 | |||
248 | /** |
||
249 | * Show this action only on the values defined. |
||
250 | * |
||
251 | * @param Column\AbstractColumn $col |
||
252 | * @param string $value |
||
253 | * @param string $comparison |
||
254 | */ |
||
255 | public function addShowOnValue(Column\AbstractColumn $col, $value = null, $comparison = Filter::EQUAL) |
||
263 | |||
264 | /** |
||
265 | * @return array |
||
266 | */ |
||
267 | public function getShowOnValues() |
||
271 | |||
272 | /** |
||
273 | * @return bool |
||
274 | */ |
||
275 | public function hasShowOnValues() |
||
283 | |||
284 | /** |
||
285 | * Display this action on this row? |
||
286 | * |
||
287 | * @param array $row |
||
288 | * |
||
289 | * @return bool |
||
290 | */ |
||
291 | View Code Duplication | public function isDisplayed(array $row) |
|
327 | |||
328 | /** |
||
329 | * Get the HTML from the type. |
||
330 | * |
||
331 | * @return string |
||
332 | */ |
||
333 | abstract protected function getHtmlType(); |
||
334 | |||
335 | /** |
||
336 | * @param array $row |
||
337 | * |
||
338 | * @return string |
||
339 | */ |
||
340 | public function toHtml(array $row) |
||
344 | } |
||
345 |
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.