GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#60)
by Kevin
09:13
created

BaseMenu::execute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Magium\Magento\Navigators;
4
5
6
use Facebook\WebDriver\WebDriverElement;
7
use Magium\Actions\WaitForPageLoaded;
8
use Magium\Magento\Themes\NavigableThemeInterface;
9
use Magium\Util\Log\Logger;
10
use Magium\WebDriver\ExpectedCondition;
11
use Magium\WebDriver\WebDriver;
12
13
class BaseMenu
14
{
15
    const NAVIGATOR = 'BaseMenu';
16
    protected $webdriver;
17
    protected $themeConfiguration;
18
    protected $loaded;
19
    protected $logger;
20
    
21
    public function __construct(
22
        NavigableThemeInterface $theme,
23
        WebDriver $webdriver,
24
        WaitForPageLoaded $loaded,
25
        Logger $logger
26
    )
27
    {
28
        $this->themeConfiguration = $theme;
29
        $this->webdriver = $webdriver;
30
        $this->loaded = $loaded;
31
        $this->logger = $logger;
32
    }
33
34
    protected function pathAction($path)
35
    {
36
        $xpath = $this->themeConfiguration->getNavigationBaseXPathSelector();
37
        usleep(500000); // Give the UI some time to update
38
        $xpath .= '/descendant::' . $this->themeConfiguration->getNavigationChildXPathSelector($path);
39
40
        $element = $this->webdriver->byXpath($xpath . '/a');
41
42
        $this->execute($element);
43
44
        return $element;
45
    }
46
47
    protected function execute(WebDriverElement $element)
48
    {
49
        $this->webdriver->getMouse()->mouseMove($element->getCoordinates());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Facebook\WebDriver\WebDriverElement as the method getCoordinates() does only exist in the following implementations of said interface: Facebook\WebDriver\Remote\RemoteWebElement, Facebook\WebDriver\Suppo...s\EventFiringWebElement.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

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

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
50
    }
51
    
52
    public function navigateTo($path)
53
    {
54
        $paths = explode('/', $path);
55
        $xpath = $this->themeConfiguration->getNavigationBaseXPathSelector();
56
57
        $this->webdriver->wait()->until(ExpectedCondition::visibilityOf($this->webdriver->byXpath($xpath)));
58
59
        $element = null;
60
        
61
        foreach ($paths as $p) {
62
            $element = $this->pathAction($p);
63
        }
64
65
        if ($element instanceof WebDriverElement) {
66
            $element->click();
67
            $this->loaded->execute($element);
68
        
69
        }
70
71
    }
72
    
73
}