| Conditions | 15 |
| Paths | 75 |
| Total Lines | 63 |
| Code Lines | 46 |
| Lines | 14 |
| Ratio | 22.22 % |
| Changes | 9 | ||
| Bugs | 0 | Features | 4 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 50 | public function navigateTo(array $instructions) |
||
| 51 | { |
||
| 52 | $this->testCase->assertGreaterThan(0, count($instructions), 'Instruction navigator requires at least one instruction'); |
||
| 53 | $useAutomaticTiming = true; |
||
| 54 | foreach ($instructions as $key => $instruction) { |
||
| 55 | if (count($instruction) > 0 && $instruction[0] == self::INSTRUCTION_USE_MANUAL_TIMING) { |
||
| 56 | $useAutomaticTiming = false; |
||
| 57 | } |
||
| 58 | } |
||
| 59 | |||
| 60 | foreach ($instructions as $instruction) { |
||
| 61 | $this->testCase->assertCount(2, $instruction, 'Navigation instructions need to be a 2 member array. First item is the instruction type, the second is the XPath'); |
||
| 62 | list($instruction, $xpath) = $instruction; |
||
| 63 | if ($useAutomaticTiming) { |
||
| 64 | $this->testCase->sleep('100ms'); // Courtesy sleep of 100ms |
||
| 65 | if ($instruction == self::INSTRUCTION_MOUSE_MOVETO || $instruction == self::INSTRUCTION_MOUSE_CLICK) { |
||
| 66 | $this->webdriver->wait()->until(ExpectedCondition::elementExists($xpath, WebDriver::BY_XPATH)); |
||
| 67 | $this->webdriver->wait(5)->until(ExpectedCondition::elementToBeClickable(WebDriverBy::xpath($xpath))); |
||
| 68 | } |
||
| 69 | } |
||
| 70 | |||
| 71 | switch ($instruction) { |
||
| 72 | case self::INSTRUCTION_MOUSE_CLICK: |
||
| 73 | $element = $this->webdriver->byXpath($xpath); |
||
| 74 | if (!$element->isDisplayed()) { |
||
| 75 | throw new ElementNotVisibleException('The element is not visible: ' . $xpath); |
||
| 76 | } |
||
| 77 | $element->click(); |
||
| 78 | break; |
||
| 79 | case self::INSTRUCTION_MOUSE_MOVETO: |
||
| 80 | $element = $this->webdriver->byXpath($xpath); |
||
| 81 | $this->webdriver->getMouse()->mouseMove($element->getCoordinates()); |
||
| 82 | break; |
||
| 83 | case self::INSTRUCTION_WAIT_FOR_EXISTS: |
||
| 84 | $this->webdriver->wait()->until(ExpectedCondition::elementExists($xpath, WebDriver::BY_XPATH)); |
||
| 85 | break; |
||
| 86 | case self::INSTRUCTION_WAIT_FOR_NOT_EXISTS: |
||
| 87 | $this->webdriver->wait()->until( |
||
| 88 | ExpectedCondition::not( |
||
| 89 | ExpectedCondition::elementExists( $xpath, WebDriver::BY_XPATH) |
||
| 90 | ) |
||
| 91 | ); |
||
| 92 | break; |
||
| 93 | View Code Duplication | case self::INSTRUCTION_WAIT_FOR_DISPLAYED: |
|
|
|
|||
| 94 | $element = $this->webdriver->byXpath($xpath); |
||
| 95 | $this->webdriver->wait()->until(ExpectedCondition::visibilityOf($element)); |
||
| 96 | break; |
||
| 97 | View Code Duplication | case self::INSTRUCTION_WAIT_FOR_HIDDEN: |
|
| 98 | $element = $this->webdriver->byXpath($xpath); |
||
| 99 | $this->webdriver->wait()->until( |
||
| 100 | ExpectedCondition::not( |
||
| 101 | ExpectedCondition::visibilityOf( |
||
| 102 | $element |
||
| 103 | ) |
||
| 104 | ) |
||
| 105 | ); |
||
| 106 | break; |
||
| 107 | default: |
||
| 108 | throw new InvalidConfigurationException('Unknown login instruction: ' .$instruction ); |
||
| 109 | } |
||
| 110 | } |
||
| 111 | |||
| 112 | } |
||
| 113 | |||
| 114 | } |
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.