| 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\Module as CartModule; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Yii; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         return CartModule::getInstance(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 |  |  |         $positions = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         foreach ($collection->models as $position) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             /** @var CartPositionInterface|AbstractCartPosition $position */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             if (!$position->validate()) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 |  |  |             $positions[] = $position; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         $this->putPositionsToCart($positions); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     protected function putPositionsToCart($positions) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $cart = $this->getCartModule()->getCart(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         $cart->putPositions($positions); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 94 |  |  |     protected function afterRun() | 
            
                                                        
            
                                    
            
            
                | 95 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 96 |  |  |         $request = Yii::$app->request; | 
            
                                                        
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 98 |  |  |         if ($request->isAjax) { | 
            
                                                        
            
                                    
            
            
                | 99 |  |  |             Yii::$app->end(); | 
            
                                                        
            
                                    
            
            
                | 100 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 102 |  |  |         if ($this->redirectToCart && !$this->hasErrors) { | 
            
                                                        
            
                                    
            
            
                | 103 |  |  |             return $this->controller->redirect('@cart'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 104 |  |  |         } elseif (isset($request->referrer)) { | 
            
                                                        
            
                                    
            
            
                | 105 |  |  |             if (!$this->hasErrors) { | 
            
                                                        
            
                                    
            
            
                | 106 |  |  |                 Yii::$app->getSession()->setFlash('success', Yii::t('cart', 'Item has been added to cart')); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 107 |  |  |             } | 
            
                                                        
            
                                    
            
            
                | 108 |  |  |             return $this->controller->redirect($request->referrer); | 
            
                                                        
            
                                    
            
            
                | 109 |  |  |         } else { | 
            
                                                        
            
                                    
            
            
                | 110 |  |  |             return $this->controller->goHome(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 111 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 112 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 113 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 114 |  |  |  | 
            
                        
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.