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 |
||
| 16 | class SummaryFormContext implements Context |
||
| 17 | { |
||
| 18 | use CommonContextTrait; |
||
| 19 | |||
| 20 | private $elementMap = array( |
||
| 21 | 'name' => '#sf-nameForm', |
||
| 22 | 'location' => '#sf-locationForm', |
||
| 23 | 'employees' => '#sf-employeesManagement', |
||
| 24 | 'workflow' => '#sf-workflowSettings', |
||
| 25 | 'jobTitleAndLocation' => '#general-locationForm', |
||
| 26 | 'jobClassification' => '#sf-general-classifications', |
||
| 27 | 'customerNote' => '#sf-general-customerNote', |
||
| 28 | 'personalInformations' => '#sf-contact-contact', |
||
| 29 | 'resumePersonalInformations' => '#sf-contact', |
||
| 30 | ); |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @When I click edit on :name form |
||
| 34 | * @TODO: [ZF3] move this method to CoreContext |
||
| 35 | */ |
||
| 36 | public function iClickEditOnForm($name) |
||
| 37 | { |
||
| 38 | $this->iClickForm($name); |
||
| 39 | $name = Inflector::camelize($name); |
||
| 40 | $type = $this->elementMap[$name]; |
||
| 41 | $locator = $type.' .sf-summary .sf-controls button'; |
||
| 42 | $element = $this->minkContext->getSession()->getPage()->find('css',$locator); |
||
| 43 | if(!$element){ |
||
| 44 | throw new \Exception('No element found with this locator: "'.$locator.'"'); |
||
| 45 | } |
||
| 46 | $element->click(); |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @When I click :form form |
||
| 51 | */ |
||
| 52 | public function iClickForm($name) |
||
| 53 | { |
||
| 54 | $name = Inflector::camelize($name); |
||
| 55 | $type = $this->elementMap[$name]; |
||
| 56 | $locator = $type.' .sf-summary'; |
||
| 57 | $session = $this->minkContext->getSession(); |
||
| 58 | $script = <<<EOC |
||
| 59 | var tElement = jQuery("$locator .sf-controls"); |
||
| 60 | tElement.css('display','block'); |
||
| 61 | tElement.css('visibility','visible'); |
||
| 62 | EOC; |
||
| 63 | $session->executeScript($script); |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @When I save :type form |
||
| 68 | */ |
||
| 69 | public function iSaveForm($type) |
||
| 70 | { |
||
| 71 | $type = Inflector::camelize($type); |
||
| 72 | $method = 'iSave'.$type; |
||
| 73 | if(method_exists($this,$method)){ |
||
| 74 | call_user_func([$this,$method]); |
||
| 75 | }else{ |
||
| 76 | $locator = $this->elementMap[$type].'-buttons-submit'; |
||
| 77 | $this->coreContext->scrollIntoView($locator); |
||
| 78 | $element = $this->minkContext->getSession()->getPage()->find('css',$locator); |
||
| 79 | $element->click(); |
||
| 80 | } |
||
| 81 | } |
||
| 82 | |||
| 83 | public function iSaveOrganizationName() |
||
| 84 | { |
||
| 85 | $locator = '#nameForm-buttons-submit'; |
||
| 86 | $this->coreContext->scrollIntoView($locator); |
||
| 87 | $element = $this->minkContext->getSession()->getPage()->find('css',$locator); |
||
| 88 | $element->click(); |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Saving organization workflow |
||
| 93 | */ |
||
| 94 | public function iSaveWorkflow() |
||
| 95 | { |
||
| 96 | $locator = '#workflowSettings-buttons-submit'; |
||
| 97 | $element = $this->minkContext->getSession()->getPage()->find('css',$locator); |
||
| 98 | $element->click(); |
||
| 99 | } |
||
| 100 | |||
| 101 | public function iSaveOrganizationLocation() |
||
| 102 | { |
||
| 103 | $locator = '#locationForm-buttons-submit'; |
||
| 104 | $this->coreContext->scrollIntoView($locator); |
||
| 105 | $element = $this->minkContext->getSession()->getPage()->find('css',$locator); |
||
| 106 | $element->click(); |
||
| 107 | } |
||
| 108 | |||
| 109 | |||
| 110 | public function iSaveJobClassification() |
||
| 111 | { |
||
| 112 | $locator = '#general-classifications-buttons-submit'; |
||
| 113 | $this->coreContext->scrollIntoView($locator); |
||
| 114 | $element = $this->minkContext->getSession()->getPage()->find('css',$locator); |
||
| 115 | $element->click(); |
||
| 116 | } |
||
| 117 | |||
| 118 | public function iSaveCustomerNote() |
||
| 119 | { |
||
| 120 | $locator = '#general-customerNote-buttons-submit'; |
||
| 121 | $this->coreContext->scrollIntoView('#sf-general-customerNote'); |
||
| 122 | $element = $this->minkContext->getSession()->getPage()->find('css',$locator); |
||
| 123 | $element->click(); |
||
| 124 | } |
||
| 125 | |||
| 126 | } |