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.

BaseMenu::navigateTo()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
dl 0
loc 20
rs 9.2
c 3
b 0
f 0
cc 4
eloc 10
nc 4
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\Navigators\ConfigurableNavigatorInterface;
10
use Magium\Util\Log\Logger;
11
use Magium\WebDriver\ExpectedCondition;
12
use Magium\WebDriver\WebDriver;
13
14
class BaseMenu implements ConfigurableNavigatorInterface
15
{
16
    const NAVIGATOR = 'BaseMenu';
17
    protected $webdriver;
18
    protected $themeConfiguration;
19
    protected $loaded;
20
    protected $logger;
21
    
22
    public function __construct(
23
        NavigableThemeInterface $theme,
24
        WebDriver $webdriver,
25
        WaitForPageLoaded $loaded,
26
        Logger $logger
27
    )
28
    {
29
        $this->themeConfiguration = $theme;
30
        $this->webdriver = $webdriver;
31
        $this->loaded = $loaded;
32
        $this->logger = $logger;
33
    }
34
35
    protected function pathAction($path, &$xpath)
36
    {
37
38
        usleep(500000); // Give the UI some time to update
39
        $xpath .= '/descendant::' . $this->themeConfiguration->getNavigationChildXPathSelector($path);
40
41
        $element = $this->webdriver->byXpath($xpath . '/a');
42
43
        $this->execute($element);
44
45
        return $element;
46
    }
47
48
    protected function execute(WebDriverElement $element)
49
    {
50
        $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...
51
        if ($this->themeConfiguration->getUseClicksToNavigate()) {
52
            $this->webdriver->getMouse()->click();
53
        }
54
    }
55
    
56
    public function navigateTo($path)
57
    {
58
        $paths = explode('/', $path);
59
        $xpath = $this->themeConfiguration->getNavigationBaseXPathSelector();
60
61
        $this->webdriver->wait()->until(ExpectedCondition::visibilityOf($this->webdriver->byXpath($xpath)));
0 ignored issues
show
Documentation introduced by
\Magium\WebDriver\Expect...river->byXpath($xpath)) is of type object<Magium\WebDriver\ExpectedCondition>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
62
63
        $element = null;
64
        
65
        foreach ($paths as $p) {
66
            $element = $this->pathAction($p, $xpath);
67
        }
68
69
        if (!$this->themeConfiguration->getUseClicksToNavigate() && $element instanceof WebDriverElement) {
70
            $element->click();
71
            $this->loaded->execute($element);
72
        
73
        }
74
75
    }
76
    
77
}