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