| Conditions | 13 |
| Paths | 11 |
| Total Lines | 44 |
| Lines | 11 |
| Ratio | 25 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 100 | protected function processBasketAction() |
||
| 101 | {
|
||
| 102 | $request = Yii::app()->request; |
||
| 103 | $data = $request->getPost($this->basket->keyCollection); |
||
| 104 | $action = $request->getPost('action');
|
||
| 105 | |||
| 106 | if( $data && $action ) |
||
| 107 | {
|
||
| 108 | switch($action) |
||
| 109 | {
|
||
| 110 | View Code Duplication | case 'remove': |
|
| 111 | $index = Arr::get($data, 'index'); |
||
| 112 | |||
| 113 | if( is_null($index) ) |
||
| 114 | $index = $this->basket->getIndex($data); |
||
| 115 | |||
| 116 | if( is_null($index) || !$this->basket->exists($index) ) |
||
| 117 | throw new CHttpException(500, 'Данный продукт уже удален. Обновите страницу.'); |
||
| 118 | |||
| 119 | $this->basket->remove($index); |
||
| 120 | break; |
||
| 121 | |||
| 122 | case 'changeAmount': |
||
| 123 | if( !$this->basket->exists($data['index']) ) |
||
| 124 | throw new CHttpException(500, 'Продукт не найден. Обновите страницу.'); |
||
| 125 | $amount = intval($data['amount']); |
||
| 126 | $this->basket->changeAmount($data['index'], $amount > 0 ? $amount : 1); |
||
| 127 | break; |
||
| 128 | |||
| 129 | case 'changeItems': |
||
| 130 | if( !$this->basket->exists($data['index']) ) |
||
| 131 | throw new CHttpException(500, 'Продукт не найден. Обновите страницу.'); |
||
| 132 | |||
| 133 | $items = Arr::get($this->basket[$data['index']]->getCollectionElement()->toArray(), 'items', array()); |
||
| 134 | $items = $data; |
||
| 135 | $this->basket->changeItems($data['index'], $items); |
||
| 136 | break; |
||
| 137 | |||
| 138 | case 'add': |
||
| 139 | $this->basket->add($data); |
||
| 140 | break; |
||
| 141 | } |
||
| 142 | } |
||
| 143 | } |
||
| 144 | |||
| 156 | } |
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:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: