| Conditions | 12 |
| Paths | 8 |
| Total Lines | 63 |
| Code Lines | 34 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 71 | protected function prepareDataProvider() |
||
| 72 | { |
||
| 73 | $filter = Yii::$app->request->get($this->filterAttribute); |
||
| 74 | $queryParams = Yii::$app->request->getQueryParams(); |
||
| 75 | |||
| 76 | if (!empty($filter)) { |
||
| 77 | $queryParams[$this->filterAttribute] = json_decode($filter, true); |
||
|
|
|||
| 78 | } |
||
| 79 | |||
| 80 | if (!$this->hardDelete && (empty($queryParams[$this->filterAttribute]) || empty(array_filter($queryParams[$this->filterAttribute])))) { |
||
| 81 | throw new NotFoundHttpException("Param '{$this->filterAttribute}' cannot be empty"); |
||
| 82 | } |
||
| 83 | |||
| 84 | Yii::$app->request->setQueryParams($queryParams); |
||
| 85 | |||
| 86 | $this->prepareDataProvider = function (EDeleteAllAction $action, $filter) { |
||
| 87 | /** @var ActiveDataProvider $dataProvider */ |
||
| 88 | $dataProvider = call_user_func([$action->dataFilter->searchModel, 'getDataProvider']); |
||
| 89 | $dataProvider->query->andWhere($filter); |
||
| 90 | |||
| 91 | if ($this->addQuery) { |
||
| 92 | call_user_func($this->addQuery, $dataProvider->query, null, $action->dataFilter, $dataProvider); |
||
| 93 | } |
||
| 94 | |||
| 95 | if ($this->filterUser) { |
||
| 96 | $filterUserColumn = call_user_func($this->filterUser); |
||
| 97 | |||
| 98 | if ($filterUserColumn !== null) { |
||
| 99 | $dataProvider->query->andWhere([$filterUserColumn => Yii::$app->user->getId()]); |
||
| 100 | } |
||
| 101 | } |
||
| 102 | |||
| 103 | return $dataProvider; |
||
| 104 | }; |
||
| 105 | |||
| 106 | if ($this->hardDelete) { |
||
| 107 | $this->modelClass::deleteAll(); |
||
| 108 | } else { |
||
| 109 | $dataProvider = parent::prepareDataProvider(); |
||
| 110 | |||
| 111 | if ($dataProvider instanceof ActiveDataProvider) { |
||
| 112 | /** @var ActiveQuery $query */ |
||
| 113 | $query = $dataProvider->query; |
||
| 114 | $query |
||
| 115 | ->limit(-1) |
||
| 116 | ->offset(-1) |
||
| 117 | ->orderBy([]); |
||
| 118 | |||
| 119 | $countDeleted = 0; |
||
| 120 | |||
| 121 | foreach ($query->each() as $model) { |
||
| 122 | /** @var $model ActiveRecord */ |
||
| 123 | if ($model->delete()) { |
||
| 124 | $this->_deletedModels[] = $model; |
||
| 125 | $countDeleted++; |
||
| 126 | } |
||
| 127 | } |
||
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | Yii::$app->response->headers->set('X-Total-Deleted', $countDeleted); |
||
| 132 | |||
| 133 | return; |
||
| 134 | } |
||
| 136 |