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 |
||
15 | class ContentContext extends RawDrupalContext { |
||
16 | |||
17 | /** |
||
18 | * Assert viewing content given its type and title. |
||
19 | * |
||
20 | * @param string $type |
||
21 | * Content type machine name. |
||
22 | * @param string $title |
||
23 | * Content title. |
||
24 | * |
||
25 | * @Given I am visiting the :type content :title |
||
26 | * @Given I visit the :type content :title |
||
27 | */ |
||
28 | public function iAmViewingTheContent($type, $title) { |
||
31 | |||
32 | /** |
||
33 | * Assert editing content given its type and title. |
||
34 | * |
||
35 | * @param string $type |
||
36 | * Content type machine name. |
||
37 | * @param string $title |
||
38 | * Content title. |
||
39 | * |
||
40 | * @Given I am editing the :type content :title |
||
41 | * @Given I edit the :type content :title |
||
42 | */ |
||
43 | public function iAmEditingTheContent($type, $title) { |
||
46 | |||
47 | /** |
||
48 | * Assert deleting content given its type and title. |
||
49 | * |
||
50 | * @param string $type |
||
51 | * Content type machine name. |
||
52 | * @param string $title |
||
53 | * Content title. |
||
54 | * |
||
55 | * @Given I am deleting the :type content :title |
||
56 | * @Given I delete the :type content :title |
||
57 | */ |
||
58 | public function iAmDeletingTheContent($type, $title) { |
||
61 | |||
62 | /** |
||
63 | * Provides a common step definition callback for node pages. |
||
64 | * |
||
65 | * @param string $op |
||
66 | * The operation being performed: 'view', 'edit', 'delete'. |
||
67 | * @param string $type |
||
68 | * The node type either as id or as label. |
||
69 | * @param string $title |
||
70 | * The node title. |
||
71 | */ |
||
72 | protected function visitContentPage($op, $type, $title) { |
||
81 | |||
82 | /** |
||
83 | * Assert that given user can perform given operation on given content. |
||
84 | * |
||
85 | * @param string $name |
||
86 | * User name. |
||
87 | * @param string $op |
||
88 | * Operation: view, edit or delete. |
||
89 | * @param string $title |
||
90 | * Content title. |
||
91 | * |
||
92 | * @throws \Exception |
||
93 | * If user cannot perform given operation on given content. |
||
94 | * |
||
95 | * @Then :name can :op content :content |
||
96 | * @Then :name can :op :content content |
||
97 | */ |
||
98 | View Code Duplication | public function userCanContent($name, $op, $title) { |
|
106 | |||
107 | /** |
||
108 | * Assert that given user cannot perform given operation on given content. |
||
109 | * |
||
110 | * @param string $name |
||
111 | * User name. |
||
112 | * @param string $op |
||
113 | * Operation: view, edit or delete. |
||
114 | * @param string $title |
||
115 | * Content title. |
||
116 | * |
||
117 | * @throws \Exception |
||
118 | * If user can perform given operation on given content. |
||
119 | * |
||
120 | * @Then :name can not :op content :content |
||
121 | * @Then :name cannot :op content :content |
||
122 | * @Then :name cannot :op :content content |
||
123 | */ |
||
124 | View Code Duplication | public function userCanNotContent($name, $op, $title) { |
|
132 | |||
133 | /** |
||
134 | * Assert presence of content edit link given its name and content title. |
||
135 | * |
||
136 | * @param string $link |
||
137 | * Link "name" HTML attribute. |
||
138 | * @param string $title |
||
139 | * Content title. |
||
140 | * |
||
141 | * @throws \Behat\Mink\Exception\ExpectationException |
||
142 | * If no edit link for given content has been found. |
||
143 | * |
||
144 | * @Then I should see the link :link to edit content :content |
||
145 | * @Then I should see a link :link to edit content :content |
||
146 | */ |
||
147 | public function assertContentEditLink($link, $title) { |
||
152 | |||
153 | /** |
||
154 | * Assert absence of content edit link given its content title. |
||
155 | * |
||
156 | * @param string $title |
||
157 | * Content title. |
||
158 | * |
||
159 | * @throws \Behat\Mink\Exception\ExpectationException |
||
160 | * If edit link for given content has been found. |
||
161 | * |
||
162 | * @Then I should not see a link to edit content :content |
||
163 | * @Then I should not see the link to edit content :content |
||
164 | */ |
||
165 | public function assertNoContentEditLink($title) { |
||
170 | |||
171 | /** |
||
172 | * Create content defined in YAML format. |
||
173 | * |
||
174 | * @param PyStringNode $string |
||
175 | * The text in yaml format that represents the content. |
||
176 | * |
||
177 | * @Given the following content: |
||
178 | */ |
||
179 | public function assertContent(PyStringNode $string) { |
||
189 | |||
190 | /** |
||
191 | * Assert translation for given content. |
||
192 | * |
||
193 | * @param string $content_type |
||
194 | * The node type for which to add the translation. |
||
195 | * @param string $title |
||
196 | * The title to identify the content by. |
||
197 | * @param PyStringNode $string |
||
198 | * The text in yaml format that represents the translation. |
||
199 | * |
||
200 | * @Given the following translation for :content_type content :title: |
||
201 | */ |
||
202 | public function assertTranslation($content_type, $title, PyStringNode $string) { |
||
211 | |||
212 | /** |
||
213 | * Get the edit link for a node. |
||
214 | * |
||
215 | * @param string $link |
||
216 | * The link name. |
||
217 | * @param string $title |
||
218 | * The node title. |
||
219 | * |
||
220 | * @return \Behat\Mink\Element\NodeElement|null |
||
221 | * The link if found. |
||
222 | * |
||
223 | * @throws \Exception |
||
224 | */ |
||
225 | protected function getContentEditLink($link, $title) { |
||
245 | |||
246 | /** |
||
247 | * Get the yaml parser from the behat container. |
||
248 | * |
||
249 | * @return \NuvoleWeb\Drupal\DrupalExtension\Component\PyStringYamlParser |
||
250 | * The parser. |
||
251 | */ |
||
252 | protected function getYamlParser() { |
||
255 | |||
256 | } |
||
257 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: