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.

CategoryTreeNode   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 68
Duplicated Lines 11.76 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
lcom 1
cbo 8
dl 8
loc 68
rs 10
c 1
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getNode() 0 6 1
A setRootCategory() 0 4 1
C extract() 8 35 7

How to fix   Duplicated Code   

Duplicated Code

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
2
3
namespace Magium\Magento\Extractors\Admin\Xtree;
4
5
use Facebook\WebDriver\Exception\ElementNotVisibleException;
6
use Facebook\WebDriver\WebDriverElement;
7
use Magium\AbstractTestCase;
8
use Magium\Extractors\AbstractExtractor;
9
use Magium\InvalidInstructionException;
10
use Magium\Magento\Themes\Admin\ThemeConfiguration;
11
use Magium\WebDriver\ExpectedCondition;
12
use Magium\WebDriver\WebDriver;
13
14
class CategoryTreeNode extends AbstractExtractor
15
{
16
17
    const EXTRACTOR = 'Admin\Xtree\CategoryTreeNode';
18
19
    protected $node;
20
    protected $nodePath;
21
    protected $rootCategory;
22
23
    public function __construct(WebDriver $webDriver, AbstractTestCase $testCase, ThemeConfiguration $theme)
24
    {
25
        parent::__construct($webDriver, $testCase, $theme);
26
    }
27
28
    /**
29
     * @param $nodePath
30
     * @return WebDriverElement
31
     */
32
33
    public function getNode($nodePath)
34
    {
35
        $this->nodePath = $nodePath;
36
        $this->extract();
37
        return $this->node;
38
    }
39
40
    public function setRootCategory($rootCategory)
41
    {
42
        $this->rootCategory = $rootCategory;
43
    }
44
45
    public function extract()
46
    {
47
        if (!$this->nodePath) {
48
            throw new InvalidInstructionException('Node path was not specified');
49
        }
50
        $parts = explode('/', $this->nodePath);
51
52
        $xpath = $this->theme->getXTreeRootXpath();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Magium\Themes\ThemeConfigurationInterface as the method getXTreeRootXpath() does only exist in the following implementations of said interface: Magium\Magento\Themes\Admin\ThemeConfiguration.

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...
53
        if ($this->rootCategory) {
54
            $xpath = $this->theme->getXTreeNamedRootXpath($this->rootCategory);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Magium\Themes\ThemeConfigurationInterface as the method getXTreeNamedRootXpath() does only exist in the following implementations of said interface: Magium\Magento\Themes\Admin\ThemeConfiguration.

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...
55
        }
56
        $xpath .= $this->theme->getXTreeChildNodePrefixXpath();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Magium\Themes\ThemeConfigurationInterface as the method getXTreeChildNodePrefixXpath() does only exist in the following implementations of said interface: Magium\Magento\Themes\Admin\ThemeConfiguration.

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...
57
        
58
        $previousExpandXpath = '';
59
        $element = null;
60
        foreach ($parts as $part) {
61
            $xpath .= '/' . $this->theme->getXTreeChildXpath($part);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Magium\Themes\ThemeConfigurationInterface as the method getXTreeChildXpath() does only exist in the following implementations of said interface: Magium\Magento\Themes\Admin\ThemeConfiguration.

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...
62 View Code Duplication
            if(!$this->webDriver->elementExists($xpath, WebDriver::BY_XPATH)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
63
                if ($previousExpandXpath) {
64
                    $this->testCase->assertElementExists($previousExpandXpath, WebDriver::BY_XPATH);
65
                    $this->webDriver->byXpath($previousExpandXpath)->click();
66
                    $this->webDriver->wait()->until(ExpectedCondition::elementExists($xpath, WebDriver::BY_XPATH));
0 ignored issues
show
Documentation introduced by
\Magium\WebDriver\Expect...er\WebDriver::BY_XPATH) is of type object<Facebook\WebDrive...riverExpectedCondition>, 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...
67
                    $this->testCase->sleep('1s'); // Give the menu time to render
68
                }
69
            }
70
            $this->testCase->assertElementExists($xpath, WebDriver::BY_XPATH);
71
            $element = $this->webDriver->byXpath($xpath);
72
            if (!$element->isDisplayed()) {
73
                throw new ElementNotVisibleException('Node element is not visible and cannot be made visible: ' . $part);
74
            }
75
            $previousExpandXpath = $xpath . $this->theme->getXTreeChildNodeExpandPrefixXpath();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Magium\Themes\ThemeConfigurationInterface as the method getXTreeChildNodeExpandPrefixXpath() does only exist in the following implementations of said interface: Magium\Magento\Themes\Admin\ThemeConfiguration.

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...
76
            $xpath .= $this->theme->getXTreeChildNodePrefixXpath();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Magium\Themes\ThemeConfigurationInterface as the method getXTreeChildNodePrefixXpath() does only exist in the following implementations of said interface: Magium\Magento\Themes\Admin\ThemeConfiguration.

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...
77
        }
78
        $this->node = $element;
79
    }
80
81
}