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 |
||
13 | class DrupalContext extends RawDrupalContext implements SnippetAcceptingContext { |
||
14 | |||
15 | /** |
||
16 | * Assert viewing content given its type and title. |
||
17 | * |
||
18 | * @param string $type |
||
19 | * Content type machine name. |
||
20 | * @param string $title |
||
21 | * Content title. |
||
22 | * |
||
23 | * @Given I am visiting the :type content :title |
||
24 | * @Given I visit the :type content :title |
||
25 | */ |
||
26 | public function iAmViewingTheContent($type, $title) { |
||
29 | |||
30 | /** |
||
31 | * Assert editing content given its type and title. |
||
32 | * |
||
33 | * @param string $type |
||
34 | * Content type machine name. |
||
35 | * @param string $title |
||
36 | * Content title. |
||
37 | * |
||
38 | * @Given I am editing the :type content :title |
||
39 | * @Given I edit the :type content :title |
||
40 | */ |
||
41 | public function iAmEditingTheContent($type, $title) { |
||
44 | |||
45 | /** |
||
46 | * Assert deleting content given its type and title. |
||
47 | * |
||
48 | * @param string $type |
||
49 | * Content type machine name. |
||
50 | * @param string $title |
||
51 | * Content title. |
||
52 | * |
||
53 | * @Given I am deleting the :type content :title |
||
54 | * @Given I delete the :type content :title |
||
55 | */ |
||
56 | public function iAmDeletingTheContent($type, $title) { |
||
59 | |||
60 | /** |
||
61 | * Provides a common step definition callback for node pages. |
||
62 | * |
||
63 | * @param string $op |
||
64 | * The operation being performed: 'view', 'edit', 'delete'. |
||
65 | * @param string $type |
||
66 | * The node type either as id or as label. |
||
67 | * @param string $title |
||
68 | * The node title. |
||
69 | * |
||
70 | * @throws ExpectationException |
||
71 | * When the node does not exist. |
||
72 | */ |
||
73 | View Code Duplication | protected function visitContentPage($op, $type, $title) { |
|
93 | |||
94 | } |
||
95 |
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.