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 VictoireContext 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 VictoireContext, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class VictoireContext extends RawMinkContext |
||
| 20 | { |
||
| 21 | use KernelDictionary; |
||
| 22 | protected $minkContext; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @BeforeScenario |
||
| 26 | * |
||
| 27 | * @param BeforeScenarioScope $scope |
||
| 28 | */ |
||
| 29 | public function gatherContexts(BeforeScenarioScope $scope) |
||
| 30 | { |
||
| 31 | $environment = $scope->getEnvironment(); |
||
| 32 | $this->minkContext = $environment->getContext('Victoire\Tests\Features\Context\MinkContext'); |
||
| 33 | } |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @BeforeScenario |
||
| 37 | * |
||
| 38 | * @param BeforeScenarioScope $scope |
||
| 39 | */ |
||
| 40 | public function resetViewsReference(BeforeScenarioScope $scope) |
||
|
|
|||
| 41 | { |
||
| 42 | $viewsReferences = $this->getContainer()->get('victoire_core.view_helper')->buildViewsReferences(); |
||
| 43 | $this->getContainer()->get('victoire_view_reference.manager')->saveReferences($viewsReferences); |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @AfterStep |
||
| 48 | * |
||
| 49 | * @param AfterStepScope $scope |
||
| 50 | * |
||
| 51 | * @throws \Exception |
||
| 52 | */ |
||
| 53 | public function lookForJSErrors(AfterStepScope $scope) |
||
| 54 | { |
||
| 55 | /* @var Session $session */ |
||
| 56 | $session = $this->getSession(); |
||
| 57 | |||
| 58 | if (!($session->getDriver() instanceof Selenium2Driver)) { |
||
| 59 | return; |
||
| 60 | } |
||
| 61 | |||
| 62 | try { |
||
| 63 | $errors = $session->evaluateScript('window.jsErrors'); |
||
| 64 | $session->evaluateScript('window.jsErrors = []'); |
||
| 65 | } catch (\Exception $e) { |
||
| 66 | throw $e; |
||
| 67 | } |
||
| 68 | if (!$errors || empty($errors)) { |
||
| 69 | return; |
||
| 70 | } |
||
| 71 | $file = sprintf('%s:%d', $scope->getFeature()->getFile(), $scope->getStep()->getLine()); |
||
| 72 | $message = sprintf('Found %d javascript error%s', count($errors), count($errors) > 0 ? 's' : ''); |
||
| 73 | echo '-------------------------------------------------------------'.PHP_EOL; |
||
| 74 | echo $file.PHP_EOL; |
||
| 75 | echo $message.PHP_EOL; |
||
| 76 | echo '-------------------------------------------------------------'.PHP_EOL; |
||
| 77 | foreach ($errors as $index => $error) { |
||
| 78 | echo sprintf(' #%d: %s', $index, $error).PHP_EOL; |
||
| 79 | } |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @Given I am logged in as :email |
||
| 84 | */ |
||
| 85 | public function iAmLoggedInAsUser($email) |
||
| 86 | { |
||
| 87 | $this->minkContext->visit('/login'); |
||
| 88 | $this->minkContext->fillField('username', $email); |
||
| 89 | $this->minkContext->fillField('password', 'test'); |
||
| 90 | $this->minkContext->pressButton('_submit'); |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @Given I login as visitor |
||
| 95 | */ |
||
| 96 | public function iLoginAsVisitor() |
||
| 97 | { |
||
| 98 | $this->getSession()->getDriver()->stop(); |
||
| 99 | $baseUrl = $this->minkContext->getMinkParameter('base_url'); |
||
| 100 | $url = str_replace('[email protected]:test', '[email protected]:test', $baseUrl); |
||
| 101 | $this->minkContext->setMinkParameter('base_url', $url); |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @Given /^I visit homepage through domain "([^"]*)"$/ |
||
| 106 | */ |
||
| 107 | public function ivisitHomepageThroughDomain($domain) |
||
| 108 | { |
||
| 109 | $this->getSession()->getDriver()->stop(); |
||
| 110 | $url = sprintf('http://[email protected]:test@%s:8000/app_domain.php', $domain); |
||
| 111 | $this->minkContext->setMinkParameter('base_url', $url); |
||
| 112 | $this->minkContext->visitPath('/'); |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @Then /^I fill in wysiwyg with "([^"]*)"$/ |
||
| 117 | */ |
||
| 118 | public function iFillInWysiwygOnFieldWith($arg) |
||
| 119 | { |
||
| 120 | $js = 'CKEDITOR.instances.victoire_widget_form_ckeditor_content.setData("'.$arg.'");'; |
||
| 121 | $this->getSession()->executeScript($js); |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @Then /^I select "([^"]*)" from the "([^"]*)" select of "([^"]*)" slot$/ |
||
| 126 | */ |
||
| 127 | public function iSelectFromTheSelectOfSlot($widget, $nth, $slot) |
||
| 128 | { |
||
| 129 | $slot = $this->getSession()->getPage()->find('xpath', 'descendant-or-self::*[@id="vic-slot-'.$slot.'"]'); |
||
| 130 | $selects = $slot->findAll('css', 'select[role="menu"]'); |
||
| 131 | $selects[$nth - 1]->selectOption(str_replace('\\"', '"', $widget)); |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @Then /^I switch to "([^"]*)" mode$/ |
||
| 136 | */ |
||
| 137 | View Code Duplication | public function iSwitchToMode($mode) |
|
| 138 | { |
||
| 139 | $element = $this->findOrRetry($this->getSession()->getPage(), 'xpath', 'descendant-or-self::*[@for="mode-switcher--'.$mode.'"]'); |
||
| 140 | |||
| 141 | if (null === $element) { |
||
| 142 | $message = sprintf('Element not found in the page after 10 seconds"'); |
||
| 143 | throw new \Behat\Mink\Exception\ResponseTextException($message, $this->getSession()); |
||
| 144 | } |
||
| 145 | $element->click(); |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @Then /^I (open|close|toggle) the hamburger menu$/ |
||
| 150 | */ |
||
| 151 | View Code Duplication | public function iOpenTheHamburgerMenu() |
|
| 152 | { |
||
| 153 | $element = $this->findOrRetry( |
||
| 154 | $this->getSession()->getPage(), |
||
| 155 | 'xpath', |
||
| 156 | 'descendant-or-self::*[@id="vic-menu-leftnavbar-trigger"]' |
||
| 157 | ); |
||
| 158 | |||
| 159 | if (null === $element) { |
||
| 160 | $message = sprintf('Element not found in the page after 10 seconds"'); |
||
| 161 | throw new \Behat\Mink\Exception\ResponseTextException($message, $this->getSession()); |
||
| 162 | } |
||
| 163 | $element->click(); |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @When I open the widget mode drop for entity :entity |
||
| 168 | */ |
||
| 169 | View Code Duplication | public function iOpenTheWidgetModeDrop($entity) |
|
| 170 | { |
||
| 171 | $element = $this->findOrRetry( |
||
| 172 | $this->getSession()->getPage(), |
||
| 173 | 'css', |
||
| 174 | '[id^="picker-'.strtolower($entity).'"] .v-mode-trigger' |
||
| 175 | ); |
||
| 176 | if (null === $element) { |
||
| 177 | $message = sprintf('Element not found in the page after 10 seconds"'); |
||
| 178 | throw new \Behat\Mink\Exception\ResponseTextException($message, $this->getSession()); |
||
| 179 | } |
||
| 180 | $element->click(); |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @When I open the widget style tab :key |
||
| 185 | */ |
||
| 186 | public function iOpenTheWidgetStyleTab($key) |
||
| 187 | { |
||
| 188 | $element = $this->findOrRetry( |
||
| 189 | $this->getSession()->getPage(), |
||
| 190 | 'css', |
||
| 191 | '[title="style-'.$key.'"]' |
||
| 192 | ); |
||
| 193 | if (null === $element) { |
||
| 194 | $message = sprintf('Element not found in the page after 10 seconds"'); |
||
| 195 | throw new \Behat\Mink\Exception\ResponseTextException($message, $this->getSession()); |
||
| 196 | } |
||
| 197 | $element->click(); |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @When I follow the float action button |
||
| 202 | */ |
||
| 203 | View Code Duplication | public function iFollowTheFloatAction() |
|
| 204 | { |
||
| 205 | $element = $this->findOrRetry( |
||
| 206 | $this->getSession()->getPage(), |
||
| 207 | 'css', |
||
| 208 | '#v-float-container [data-flag="v-drop v-drop-fab"]' |
||
| 209 | ); |
||
| 210 | if (null === $element) { |
||
| 211 | $message = sprintf('Element not found in the page after 10 seconds"'); |
||
| 212 | throw new \Behat\Mink\Exception\ResponseTextException($message, $this->getSession()); |
||
| 213 | } |
||
| 214 | $element->click(); |
||
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @When I open the widget quantum collapse for entity :entity |
||
| 219 | */ |
||
| 220 | View Code Duplication | public function iOpenTheWidgetQuantumCollapse($entity) |
|
| 221 | { |
||
| 222 | $element = $this->findOrRetry( |
||
| 223 | $this->getSession()->getPage(), |
||
| 224 | 'css', |
||
| 225 | '[id^="picker-'.strtolower($entity).'"][data-state="visible"] [id^="picker-'.strtolower($entity).'"][data-state="visible"] .v-widget-form__quantum-btn' |
||
| 226 | ); |
||
| 227 | |||
| 228 | if (null === $element) { |
||
| 229 | $message = sprintf('Element not found in the page after 10 seconds"'); |
||
| 230 | throw new \Behat\Mink\Exception\ResponseTextException($message, $this->getSession()); |
||
| 231 | } |
||
| 232 | $element->click(); |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @When I open the widget quantum collapse when static |
||
| 237 | */ |
||
| 238 | View Code Duplication | public function iOpenTheWidgetQuantumCollapseWhenStatic() |
|
| 239 | { |
||
| 240 | $element = $this->findOrRetry( |
||
| 241 | $this->getSession()->getPage(), |
||
| 242 | 'css', |
||
| 243 | '[data-state="visible"] [id^="picker-static"] .v-widget-form__quantum-btn' |
||
| 244 | ); |
||
| 245 | |||
| 246 | if (null === $element) { |
||
| 247 | $message = sprintf('Element not found in the page after 10 seconds"'); |
||
| 248 | throw new \Behat\Mink\Exception\ResponseTextException($message, $this->getSession()); |
||
| 249 | } |
||
| 250 | $element->click(); |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @Then /^I open the settings menu$/ |
||
| 255 | */ |
||
| 256 | View Code Duplication | public function iOpenTheSettingsMenu() |
|
| 257 | { |
||
| 258 | $element = $this->findOrRetry( |
||
| 259 | $this->getSession()->getPage(), |
||
| 260 | 'xpath', |
||
| 261 | 'descendant-or-self::*[@id="v-settings-link"]' |
||
| 262 | ); |
||
| 263 | |||
| 264 | if (null === $element) { |
||
| 265 | $message = sprintf('Element not found in the page after 10 seconds"'); |
||
| 266 | throw new \Behat\Mink\Exception\ResponseTextException($message, $this->getSession()); |
||
| 267 | } |
||
| 268 | $element->click(); |
||
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @Then /^I open the additionals menu drop$/ |
||
| 273 | */ |
||
| 274 | View Code Duplication | public function iOpenTheAdditionalsMenuDrop() |
|
| 275 | { |
||
| 276 | $element = $this->findOrRetry( |
||
| 277 | $this->getSession()->getPage(), |
||
| 278 | 'xpath', |
||
| 279 | 'descendant-or-self::*[@id="v-additionals-drop"]' |
||
| 280 | ); |
||
| 281 | |||
| 282 | if (null === $element) { |
||
| 283 | $message = sprintf('Element not found in the page after 10 seconds"'); |
||
| 284 | throw new \Behat\Mink\Exception\ResponseTextException($message, $this->getSession()); |
||
| 285 | } |
||
| 286 | $element->click(); |
||
| 287 | } |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @When I follow the tab :name |
||
| 291 | */ |
||
| 292 | public function iFollowTheTab($name) |
||
| 293 | { |
||
| 294 | $element = $this->findOrRetry($this->getSession()->getPage(), 'xpath', sprintf('descendant-or-self::a[contains(@class, "v-tabs-nav__anchor") and normalize-space(text()) = "%s"]', $name)); |
||
| 295 | |||
| 296 | // @TODO When the new styleguide is completly integrated, remove. |
||
| 297 | if (null === $element) { |
||
| 298 | $element = $this->findOrRetry($this->getSession()->getPage(), 'xpath', sprintf('descendant-or-self::a[@data-toggle="vic-tab" and normalize-space(text()) = "%s"]', $name)); |
||
| 299 | } |
||
| 300 | |||
| 301 | if (null === $element) { |
||
| 302 | $message = sprintf('Element not found in the page after 10 seconds"'); |
||
| 303 | throw new \Behat\Mink\Exception\ResponseTextException($message, $this->getSession()); |
||
| 304 | } |
||
| 305 | $element->click(); |
||
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * @When I follow the drop trigger :name |
||
| 310 | */ |
||
| 311 | View Code Duplication | public function iFollowTheDropTrigger($name) |
|
| 321 | |||
| 322 | /** |
||
| 323 | * @When I follow the drop anchor :name |
||
| 324 | */ |
||
| 325 | public function iFollowTheDropAnchor($name) |
||
| 326 | { |
||
| 327 | $page = $this->getSession()->getPage(); |
||
| 328 | $elements = $page->findAll('xpath', sprintf('descendant-or-self::a[contains(@class, "v-drop__anchor") and normalize-space(text()) = "%s"]', $name)); |
||
| 329 | |||
| 330 | if (count($elements) < 1) { |
||
| 331 | $message = sprintf('Element not found in the page after 10 seconds"'); |
||
| 332 | throw new \Behat\Mink\Exception\ResponseTextException($message, $this->getSession()); |
||
| 333 | } |
||
| 334 | |||
| 335 | foreach ($elements as $element) { |
||
| 336 | if ($element->getText() === $name) { |
||
| 337 | $element->click(); |
||
| 338 | } |
||
| 339 | } |
||
| 340 | } |
||
| 341 | |||
| 342 | /** |
||
| 343 | * @Then /^I submit the widget$/ |
||
| 344 | * @Then /^I submit the modal$/ |
||
| 345 | */ |
||
| 346 | public function iSubmitTheWidget() |
||
| 356 | |||
| 357 | /** |
||
| 358 | * @Given /^I edit an "([^"]*)" widget$/ |
||
| 359 | * @Given /^I edit the "([^"]*)" widget$/ |
||
| 360 | */ |
||
| 361 | public function iEditTheWidget($widgetType) |
||
| 376 | |||
| 377 | /** |
||
| 378 | * @Then /^"([^"]*)" should precede "([^"]*)"$/ |
||
| 379 | */ |
||
| 380 | public function shouldPrecedeForTheQuery($textBefore, $textAfter) |
||
| 394 | |||
| 395 | /** |
||
| 396 | * @When /^I select the option "(?P<option>[^"]*)" in the dropdown "(?P<dropdown>[^"]*)"$/ |
||
| 397 | */ |
||
| 398 | public function iSelectTheOptionInTheDropdown($option, $dropdown) |
||
| 405 | |||
| 406 | /** |
||
| 407 | * @Then /^I attach image with id "(\d+)" to victoire field "(.+)"$/ |
||
| 408 | */ |
||
| 409 | public function attachImageToVictoireScript($imageId, $fieldId) |
||
| 414 | |||
| 415 | /** |
||
| 416 | * @Then I should find css element :element with selector :selector and value :value |
||
| 417 | */ |
||
| 418 | public function iShouldFindCssWithSelectorAndValue($element, $selector, $value) |
||
| 429 | |||
| 430 | /** |
||
| 431 | * @Then I should see disable drop anchor :name |
||
| 432 | */ |
||
| 433 | View Code Duplication | public function iShouldSeeDisableDropAnchor($name) |
|
| 434 | { |
||
| 435 | $element = $this->findOrRetry($this->getSession()->getPage(), 'xpath', sprintf('descendant-or-self::*[contains(@class, \'v-drop__anchor--disabled\') and normalize-space(.) = "%s"]', $name)); |
||
| 436 | |||
| 437 | if (null === $element) { |
||
| 438 | $message = sprintf('Element not found in the page after 10 seconds"'); |
||
| 439 | throw new \Behat\Mink\Exception\ResponseTextException($message, $this->getSession()); |
||
| 440 | } |
||
| 441 | } |
||
| 442 | |||
| 443 | /** |
||
| 444 | * @Then I should see disable tab :name |
||
| 445 | */ |
||
| 446 | View Code Duplication | public function iShouldSeeDisableTab($name) |
|
| 447 | { |
||
| 448 | $element = $this->findOrRetry($this->getSession()->getPage(), 'xpath', sprintf('descendant-or-self::li[@class="vic-disable" and normalize-space(.) = "%s"]', $name)); |
||
| 455 | |||
| 456 | /** |
||
| 457 | * @Then /^I move the widgetMap "(.+)" "(.+)" the widgetMap "(.*)"$/ |
||
| 458 | */ |
||
| 459 | public function iMoveWidgetUnder($widgetMapMoved, $position, $widgetMapMovedTo) |
||
| 468 | |||
| 469 | /** |
||
| 470 | * @When /^I rename quantum "(.+)" with "(.+)"$/ |
||
| 471 | */ |
||
| 472 | public function iRenameQuantumWith($quantumPosition, $name) |
||
| 487 | |||
| 488 | /** |
||
| 489 | * @When /^I select quantum "(.+)"$/ |
||
| 490 | */ |
||
| 491 | public function iSelectQuantum($quantumName) |
||
| 499 | |||
| 500 | /** |
||
| 501 | * @When /^I create a new quantum$/ |
||
| 502 | */ |
||
| 503 | public function iCreateANewQuantum() |
||
| 510 | |||
| 511 | /** |
||
| 512 | * @When I select :arg1 from the collapse menu |
||
| 513 | */ |
||
| 514 | public function iSelectFromTheCollapseMenu($name) |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Try to find value in element and retry for a given time. |
||
| 546 | * |
||
| 547 | * @param Element $element |
||
| 548 | * @param string $selectorType xpath|css |
||
| 549 | * @param string $value |
||
| 550 | * @param int $timeout |
||
| 551 | * |
||
| 552 | * @return \Behat\Mink\Element\NodeElement|mixed|null|void |
||
| 553 | */ |
||
| 554 | protected function findOrRetry(Element $element, $selectorType, $value, $timeout = 10000) |
||
| 570 | |||
| 571 | /** |
||
| 572 | * Fill Select2 input field and select a value. |
||
| 573 | * |
||
| 574 | * @When /^(?:|I )fill in select2 input "(?P<field>(?:[^"]|\\")*)" with "(?P<value>(?:[^"]|\\")*)" and select "(?P<entry>(?:[^"]|\\")*)"$/ |
||
| 575 | */ |
||
| 576 | public function iFillInSelect2InputWithAndSelect($field, $value, $entry) |
||
| 583 | |||
| 584 | /** |
||
| 585 | * Open Select2 choice list. |
||
| 586 | * |
||
| 587 | * @param DocumentElement $page |
||
| 588 | * @param string $field |
||
| 589 | * |
||
| 590 | * @throws \Exception |
||
| 591 | */ |
||
| 592 | private function openField(DocumentElement $page, $field) |
||
| 605 | |||
| 606 | /** |
||
| 607 | * Fill Select2 search field. |
||
| 608 | * |
||
| 609 | * @param DocumentElement $page |
||
| 610 | * @param string $field |
||
| 611 | * @param string $value |
||
| 612 | * |
||
| 613 | * @throws \Exception |
||
| 614 | */ |
||
| 615 | private function fillSearchField(DocumentElement $page, $field, $value) |
||
| 634 | |||
| 635 | /** |
||
| 636 | * Select value in choice list. |
||
| 637 | * |
||
| 638 | * @param DocumentElement $page |
||
| 639 | * @param string $field |
||
| 640 | * @param string $value |
||
| 641 | * |
||
| 642 | * @throws \Exception |
||
| 643 | */ |
||
| 644 | private function selectValue(DocumentElement $page, $field, $value) |
||
| 657 | |||
| 658 | /** |
||
| 659 | * Wait the end of fetching Select2 results. |
||
| 660 | * |
||
| 661 | * @param int $time Time to wait in seconds |
||
| 662 | */ |
||
| 663 | private function waitForLoadingResults($time = 60) |
||
| 673 | } |
||
| 674 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.