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 GridWebContext 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 GridWebContext, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class GridWebContext implements MinkAwareContext |
||
25 | { |
||
26 | use MinkDictionary; |
||
27 | |||
28 | /** |
||
29 | * @var AjaxContext |
||
30 | */ |
||
31 | private $ajaxContext; |
||
32 | |||
33 | /** |
||
34 | * @BeforeScenario |
||
35 | */ |
||
36 | public function init(BeforeScenarioScope $scope) |
||
42 | |||
43 | public function toggleBatch() |
||
49 | |||
50 | /** |
||
51 | * @param string|null $cell |
||
52 | */ |
||
53 | public function selectBatch($cell = null) |
||
57 | |||
58 | public function selectBatchAll() |
||
69 | |||
70 | /** |
||
71 | * @param string|int $header |
||
72 | * @param string $sorting |
||
73 | */ |
||
74 | public function followSortingLink($header, $sorting) |
||
85 | |||
86 | /** |
||
87 | * @param string $cell |
||
88 | * @param string $link |
||
89 | */ |
||
90 | public function followColumnActionLink($cell, $link) |
||
101 | |||
102 | public function waitFilters() |
||
108 | |||
109 | public function waitFiltersRefresh() |
||
113 | |||
114 | /** |
||
115 | * @param mixed[] $rows |
||
116 | */ |
||
117 | public function assertGrid(array $rows) |
||
122 | |||
123 | /** |
||
124 | * @param string[] $columns |
||
125 | */ |
||
126 | View Code Duplication | public function assertHeaders(array $columns) |
|
134 | |||
135 | /** |
||
136 | * @param string|int $header |
||
137 | * @param string $value |
||
138 | */ |
||
139 | public function assertHeader($header, $value) |
||
147 | |||
148 | /** |
||
149 | * @param bool $checked |
||
150 | */ |
||
151 | public function assertBatchesState($checked) |
||
163 | |||
164 | /** |
||
165 | * @param string[][] $rows |
||
166 | */ |
||
167 | public function assertBody(array $rows) |
||
173 | |||
174 | /** |
||
175 | * @param int $row |
||
176 | * @param string[] $columns |
||
177 | */ |
||
178 | View Code Duplication | public function assertRow($row, array $columns) |
|
186 | |||
187 | /** |
||
188 | * @param string[] $header |
||
189 | * @param string[] $cells |
||
190 | */ |
||
191 | public function assertColumnCells($header, array $cells) |
||
200 | |||
201 | /** |
||
202 | * @param int $row |
||
203 | * @param int $column |
||
204 | * @param string $value |
||
205 | */ |
||
206 | public function assertCell($row, $column, $value) |
||
220 | |||
221 | /** |
||
222 | * @param string $header |
||
223 | * @param string $sort |
||
224 | */ |
||
225 | public function assertSorting($header, $sort) |
||
246 | |||
247 | /** |
||
248 | * @return bool |
||
249 | */ |
||
250 | public function hastBatch() |
||
260 | |||
261 | /** |
||
262 | * @param string|null $cell |
||
263 | * |
||
264 | * @return NodeElement |
||
265 | */ |
||
266 | public function findBatch($cell = null) |
||
286 | |||
287 | /** |
||
288 | * @param string $header |
||
289 | * |
||
290 | * @return int |
||
291 | */ |
||
292 | public function findHeaderIndex($header) |
||
298 | |||
299 | /** |
||
300 | * @param string|int $header |
||
301 | * |
||
302 | * @return NodeElement |
||
303 | */ |
||
304 | public function findHeader($header) |
||
315 | |||
316 | /** |
||
317 | * @param int|null $row |
||
318 | * @param int|null $column |
||
319 | * @param string|null $value |
||
320 | * |
||
321 | * @return NodeElement|NodeElement[] |
||
322 | */ |
||
323 | public function findCell($row = null, $column = null, $value = null) |
||
359 | |||
360 | /** |
||
361 | * @return NodeElement |
||
362 | */ |
||
363 | public function findGrid() |
||
374 | } |
||
375 |
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.