Completed
Push — master ( 786f42...798816 )
by Dmitry
13:21
created

AddToCartAction::putPositionsToCart()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
ccs 0
cts 4
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
3
/*
4
 * Cart module for Yii2
5
 *
6
 * @link      https://github.com/hiqdev/yii2-cart
7
 * @package   yii2-cart
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hiqdev\yii2\cart\actions;
13
14
use hipanel\modules\finance\cart\AbstractCartPosition;
15
use hiqdev\hiart\Collection;
16
use hiqdev\yii2\cart\CartPositionInterface;
17
use hiqdev\yii2\cart\ImmutableQuantityInterface;
18
use hiqdev\yii2\cart\Module as CartModule;
19
use Yii;
20
21
class AddToCartAction extends \yii\base\Action
22
{
23
    /**
24
     * @var CartPositionInterface The class for new product
25
     */
26
    public $productClass;
27
28
    /**
29
     * @var boolean whether the action expects bulk models load using `selection`
30
     */
31
    public $bulkLoad = false;
32
33
    /**
34
     * @var bool whether client should be redirected to the cart in case of success item adding
35
     */
36
    public $redirectToCart = false;
37
38
    /**
39
     * @var bool whether any errors occurred during save
40
     */
41
    protected $hasErrors = false;
42
43
    /**
44
     * Returns the cart module.
45
     * @return CartModule
46
     */
47
    public function getCartModule()
48
    {
49
        return CartModule::getInstance();
50
    }
51
52
    public function run()
53
    {
54
        $data = null;
0 ignored issues
show
Unused Code introduced by
$data is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
55
        $request = Yii::$app->request;
56
        /** @var CartPositionInterface $model */
57
        $model = Yii::createObject($this->productClass);
0 ignored issues
show
Documentation introduced by
$this->productClass is of type object<hiqdev\yii2\cart\CartPositionInterface>, 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...
58
        $collection = new Collection(); // TODO: drop dependency
59
        $collection->setModel($model);
60
61
        if (!$this->bulkLoad) {
62
            $data = [$request->post() ?: $request->get()];
63
            $collection->load($data);
64
        } else {
65
            $collection->load();
66
        }
67
68
        foreach ($collection->models as $position) {
69
            /** @var CartPositionInterface|AbstractCartPosition $position */
70
            if (!$position->validate()) {
0 ignored issues
show
Bug introduced by
The method validate() does not seem to exist on object<hiqdev\yii2\cart\CartPositionInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
71
                $this->hasErrors = true;
72
                $error = $collection->getFirstError();
73
                if (empty($error)) {
74
                    $error = Yii::t('cart', 'Failed to add item to the cart');
75
                }
76
                Yii::$app->session->addFlash('warning', $error);
77
                Yii::warning('Failed to add item to cart', 'cart');
78
79
                continue;
80
            }
81
        }
82
83
        $this->putPositionsToCart($collection->models);
84
    }
85
86
    protected function putPositionsToCart($positions)
87
    {
88
        $cart = $this->getCartModule()->getCart();
89
        $cart->putPositions($positions);
90
    }
91
92
    protected function afterRun()
93
    {
94
        $this->ensureBehaviors();
95
        if ($this->hasEventHandlers('afterAction')) {
96
            return true;
97
        }
98
99
        $request = Yii::$app->request;
100
101
        if ($request->isAjax) {
102
            Yii::$app->end();
103
        }
104
105
        if ($this->redirectToCart && !$this->hasErrors) {
106
            return $this->controller->redirect('@cart');
0 ignored issues
show
Bug introduced by
The method redirect does only exist in yii\web\Controller, but not in yii\base\Controller.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
107
        } else {
108
            return $this->controller->redirect($request->referrer ?: $this->controller->goHome());
0 ignored issues
show
Bug introduced by
The method goHome does only exist in yii\web\Controller, but not in yii\base\Controller.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
109
        }
110
    }
111
}
112