| Conditions | 11 |
| Paths | 16 |
| Total Lines | 67 |
| Code Lines | 36 |
| 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 |
||
| 67 | protected function prepareDataProvider() |
||
| 68 | { |
||
| 69 | $filter = Yii::$app->request->get($this->filterAttribute); |
||
| 70 | $queryParams = Yii::$app->request->getQueryParams(); |
||
| 71 | $updatedAttributes = []; |
||
| 72 | |||
| 73 | if (!empty($filter)) { |
||
| 74 | $queryParams[$this->filterAttribute] = json_decode($filter, true); |
||
|
|
|||
| 75 | } |
||
| 76 | |||
| 77 | if (!empty($queryParams[$this->updatedAttribute])) { |
||
| 78 | $updatedAttributes = json_decode($queryParams[$this->updatedAttribute], true); |
||
| 79 | } |
||
| 80 | |||
| 81 | if (empty($updatedAttributes)) { |
||
| 82 | throw new BadRequestHttpException("Param '{$this->updatedAttribute}' cannot be empty"); |
||
| 83 | } |
||
| 84 | |||
| 85 | Yii::$app->request->setQueryParams($queryParams); |
||
| 86 | |||
| 87 | $this->prepareDataProvider = function (EIndexAction $action, $filter) use ($extraFilter) { |
||
| 88 | /** @var ActiveDataProvider $dataProvider */ |
||
| 89 | $dataProvider = call_user_func([$action->dataFilter->searchModel, 'getDataProvider']); |
||
| 90 | $dataProvider->query->andWhere($filter); |
||
| 91 | |||
| 92 | if ($this->addQuery) { |
||
| 93 | call_user_func($this->addQuery, $dataProvider->query, $extraFilter, $action->dataFilter); |
||
| 94 | |||
| 95 | if ($action->dataFilter->hasErrors()) { |
||
| 96 | return $action->dataFilter; |
||
| 97 | } |
||
| 98 | } |
||
| 99 | |||
| 100 | if ($this->filterUser) { |
||
| 101 | $filterUserColumn = is_callable($this->filterUser) ? call_user_func($this->filterUser) : $this->filterUser; |
||
| 102 | |||
| 103 | if ($filterUserColumn !== null) { |
||
| 104 | $dataProvider->query->andWhere([$filterUserColumn => Yii::$app->user->getId()]); |
||
| 105 | } |
||
| 106 | } |
||
| 107 | |||
| 108 | return $dataProvider; |
||
| 109 | }; |
||
| 110 | |||
| 111 | $dataProvider = parent::prepareDataProvider(); |
||
| 112 | /** @var ActiveQuery $query */ |
||
| 113 | $query = $dataProvider->query; |
||
| 114 | $query |
||
| 115 | ->limit(-1) |
||
| 116 | ->offset(-1) |
||
| 117 | ->orderBy([]); |
||
| 118 | |||
| 119 | $countUpdated = 0; |
||
| 120 | |||
| 121 | foreach ($query->each() as $model) { |
||
| 122 | /** @var $model ActiveRecord */ |
||
| 123 | $model->setAttributes($updatedAttributes); |
||
| 124 | |||
| 125 | if ($model->save()) { |
||
| 126 | $this->_updatedModels[] = $model; |
||
| 127 | $countUpdated++; |
||
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | Yii::$app->response->headers->set('X-Total-Updated', $countUpdated); |
||
| 132 | |||
| 133 | return; |
||
| 134 | } |
||
| 136 |