1 | <?php |
||
20 | class AddToCartAction extends \yii\base\Action |
||
21 | { |
||
22 | /** |
||
23 | * @var CartPositionInterface The class for new product |
||
24 | */ |
||
25 | public $productClass; |
||
26 | |||
27 | /** |
||
28 | * @var boolean whether the action expects bulk models load using `selection` |
||
29 | */ |
||
30 | public $bulkLoad = false; |
||
31 | |||
32 | /** |
||
33 | * @var bool whether client should be redirected to the cart in case of success item adding |
||
34 | */ |
||
35 | public $redirectToCart = false; |
||
36 | |||
37 | /** |
||
38 | * @var bool whether any errors occurred during save |
||
39 | */ |
||
40 | protected $hasErrors = false; |
||
41 | |||
42 | /** |
||
43 | * Returns the cart module. |
||
44 | * @return CartModule |
||
45 | */ |
||
46 | public function getCartModule() |
||
50 | |||
51 | public function run() |
||
52 | { |
||
53 | $data = null; |
||
|
|||
54 | $request = Yii::$app->request; |
||
55 | /** @var CartPositionInterface $model */ |
||
56 | $model = Yii::createObject($this->productClass); |
||
57 | $collection = new Collection(); // TODO: drop dependency |
||
58 | $collection->setModel($model); |
||
59 | |||
60 | if (!$this->bulkLoad) { |
||
61 | $data = [$request->post() ?: $request->get()]; |
||
62 | $collection->load($data); |
||
63 | } else { |
||
64 | $collection->load(); |
||
65 | } |
||
66 | |||
67 | foreach ($collection->models as $position) { |
||
68 | /** @var CartPositionInterface|AbstractCartPosition $position */ |
||
69 | if (!$position->validate()) { |
||
70 | $this->hasErrors = true; |
||
71 | $error = $collection->getFirstError(); |
||
72 | if (empty($error)) { |
||
73 | $error = Yii::t('cart', 'Failed to add item to the cart'); |
||
74 | } |
||
75 | Yii::$app->session->addFlash('warning', $error); |
||
76 | Yii::warning('Failed to add item to cart', 'cart'); |
||
77 | |||
78 | continue; |
||
79 | } |
||
80 | } |
||
81 | |||
82 | $this->putPositionsToCart($collection->models); |
||
83 | } |
||
84 | |||
85 | protected function putPositionsToCart($positions) |
||
90 | |||
91 | protected function afterRun() |
||
115 | } |
||
116 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
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.