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 |
||
19 | class CmsFormsContext implements Context |
||
20 | { |
||
21 | use MainContextAwareTrait; |
||
22 | |||
23 | /** |
||
24 | * Get Mink session from MinkContext |
||
25 | */ |
||
26 | public function getSession($name = null) |
||
30 | |||
31 | /** |
||
32 | * Returns fixed step argument (with \\" replaced back to "). |
||
33 | * Copied from {@see MinkContext} |
||
34 | * |
||
35 | * @param string $argument |
||
36 | * @return string |
||
37 | */ |
||
38 | protected function fixStepArgument($argument) |
||
42 | |||
43 | /** |
||
44 | * @Then /^I should( not? |\s*)see an edit page form$/ |
||
45 | */ |
||
46 | public function stepIShouldSeeAnEditPageForm($negative) |
||
57 | |||
58 | /** |
||
59 | * @When /^I fill in the "(?P<field>(?:[^"]|\\")*)" HTML field with "(?P<value>(?:[^"]|\\")*)"$/ |
||
60 | * @When /^I fill in "(?P<value>(?:[^"]|\\")*)" for the "(?P<field>(?:[^"]|\\")*)" HTML field$/ |
||
61 | */ |
||
62 | View Code Duplication | public function stepIFillInTheHtmlFieldWith($field, $value) |
|
73 | |||
74 | /** |
||
75 | * @When /^I append "(?P<value>(?:[^"]|\\")*)" to the "(?P<field>(?:[^"]|\\")*)" HTML field$/ |
||
76 | */ |
||
77 | View Code Duplication | public function stepIAppendTotheHtmlField($field, $value) |
|
88 | |||
89 | /** |
||
90 | * @Then /^the "(?P<locator>(?:[^"]|\\")*)" HTML field should(?P<negative> not? |\s*)contain "(?P<html>.*)"$/ |
||
91 | */ |
||
92 | public function theHtmlFieldShouldContain($locator, $negative, $html) |
||
120 | |||
121 | // @codingStandardsIgnoreStart |
||
122 | /** |
||
123 | * Checks formatting in the HTML field, by analyzing the HTML node surrounding |
||
124 | * the text for certain properties. |
||
125 | * |
||
126 | * Example: Given "my text" in the "Content" HTML field should be right aligned |
||
127 | * Example: Given "my text" in the "Content" HTML field should not be bold |
||
128 | * |
||
129 | * @todo Use an actual DOM parser for more accurate assertions |
||
130 | * |
||
131 | * @Given /^"(?P<text>([^"]*))" in the "(?P<field>(?:[^"]|\\")*)" HTML field should(?P<negate>(?: not)?) be (?P<formatting>(.*))$/ |
||
132 | */ |
||
133 | public function stepContentInHtmlFieldShouldHaveFormatting($text, $field, $negate, $formatting) { |
||
160 | // @codingStandardsIgnoreEnd |
||
161 | |||
162 | /** |
||
163 | * Selects the first textual match in the HTML editor. Does not support |
||
164 | * selection across DOM node boundaries. |
||
165 | * |
||
166 | * @When /^I select "(?P<text>([^"]*))" in the "(?P<field>(?:[^"]|\\")*)" HTML field$/ |
||
167 | */ |
||
168 | public function stepIHighlightTextInHtmlField($text, $field) |
||
201 | |||
202 | /** |
||
203 | * @Given /^I should( not? |\s*)see a "([^"]*)" field$/ |
||
204 | */ |
||
205 | public function iShouldSeeAField($negative, $text) |
||
222 | |||
223 | /** |
||
224 | * Click on the element with the provided CSS Selector |
||
225 | * |
||
226 | * @When /^I press the "([^"]*)" HTML field button$/ |
||
227 | */ |
||
228 | public function iClickOnTheHtmlFieldButton($button) |
||
239 | |||
240 | /* |
||
241 | * @example Given the CMS settings has the following data |
||
242 | * | Title | My site title | |
||
243 | * | Theme | My site theme | |
||
244 | * @Given /^the CMS settings have the following data$/ |
||
245 | */ |
||
246 | public function theCmsSettingsHasData(TableNode $fieldsTable) |
||
256 | |||
257 | /** |
||
258 | * Locate an HTML editor field |
||
259 | * |
||
260 | * @param string $locator Raw html field identifier as passed from |
||
261 | * @return NodeElement |
||
262 | */ |
||
263 | protected function getHtmlField($locator) |
||
271 | |||
272 | /** |
||
273 | * @Given /^the "([^"]*)" field ((?:does not have)|(?:has)) property "([^"]*)"$/ |
||
274 | */ |
||
275 | public function assertTheFieldHasProperty($name, $cond, $property) |
||
296 | } |
||
297 |
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.