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.

AddSimpleProductToCart   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 70
Duplicated Lines 4.29 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 9
lcom 1
cbo 9
dl 3
loc 70
rs 10
c 1
b 0
f 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A requireQty() 0 4 1
A setQty() 0 5 1
A execute() 3 12 4
A clickAddToCart() 0 18 2

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\Actions\Cart;
4
5
use Magium\Actions\StaticActionInterface;
6
use Magium\Magento\Extractors\Catalog\Cart\AddToCart;
7
use Magium\Magento\Themes\AbstractThemeConfiguration;
8
use Magium\WebDriver\ExpectedCondition;
9
use Magium\WebDriver\WebDriver;
10
11
class AddSimpleProductToCart implements StaticActionInterface
12
{
13
14
    const ACTION = 'Cart\AddSimpleProductToCart';
15
16
    const EXCEPTION_NO_ELEMENT = 'Magium\Magento\Actions\Cart\NoSuchElementException';
17
    const EXCEPTION_ADD_TO_CART_FAILED = 'Magium\Magento\Actions\Cart\AddToCartFailedException';
18
19
    protected $webDriver;
20
    protected $theme;
21
    protected $addToCart;
22
23
    protected $requireQty;
24
    protected $addQty;
25
26
    public function __construct(
27
        WebDriver $webDriver,
28
        AbstractThemeConfiguration $themeConfiguration,
29
        AddToCart $addToCart
30
    )
31
    {
32
        $this->webDriver = $webDriver;
33
        $this->theme = $themeConfiguration;
34
        $this->addToCart = $addToCart;
35
    }
36
37
    public function requireQty($require = true)
38
    {
39
        $this->requireQty = $require;
40
    }
41
42
    public function setQty($number)
43
    {
44
        $this->requireQty();
45
        $this->addQty = $number;
46
    }
47
48
    public function execute()
49
    {
50
        if ($this->requireQty || $this->addQty > 1) {
51 View Code Duplication
            if (!$this->webDriver->elementExists($this->theme->getSimpleProductQtyXpath(), 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...
52
                throw new NoSuchElementException('Could not find the simple add to cart element with the Xpath: ' . $this->theme->getAddToCartXpath());
53
            }
54
            $element = $this->webDriver->byXpath($this->theme->getSimpleProductQtyXpath());
55
            $element->clear();
56
            $element->sendKeys($this->addQty);
57
        }
58
        $this->clickAddToCart();
59
    }
60
61
    protected function clickAddToCart()
62
    {
63
        $element = $this->addToCart->getElement();
64
        // Because, for some reason, the M2 design HIDES the add-to-cart button as part of the default theme.   Why
65
        // would you want to hide the second most important button on the site?
66
67
        try {
68
            $element->click();
69
        } catch (\Exception $e) {
70
            $e2 = $this->webDriver->byXpath($this->theme->getAddToCartXpath() . '/ancestor::li');
71
            $this->webDriver->getMouse()->mouseMove($e2->getCoordinates());
72
            $element->click();
73
        }
74
        $this->webDriver->wait()->until(ExpectedCondition::elementExists($this->theme->getAddToCartSuccessXpath(), 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...
75
        $element = $this->webDriver->byXpath($this->theme->getAddToCartSuccessXpath());
76
        $this->webDriver->wait()->until(ExpectedCondition::visibilityOf($element));
0 ignored issues
show
Documentation introduced by
\Magium\WebDriver\Expect...:visibilityOf($element) 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...
77
78
    }
79
80
}
81