| Conditions | 4 |
| Paths | 5 |
| Total Lines | 74 |
| Code Lines | 47 |
| Lines | 23 |
| Ratio | 31.08 % |
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 |
||
| 156 | public function actionSearch() |
||
| 157 | { |
||
| 158 | /** |
||
| 159 | * @param $module \app\modules\page\PageModule |
||
| 160 | */ |
||
| 161 | if (!Yii::$app->request->isAjax) { |
||
| 162 | throw new ForbiddenHttpException(); |
||
| 163 | } |
||
| 164 | $model = new Search(); |
||
| 165 | $model->load(Yii::$app->request->get()); |
||
| 166 | $cacheKey = 'PageSearchIds: ' . $model->q; |
||
| 167 | $ids = Yii::$app->cache->get($cacheKey); |
||
| 168 | if ($ids === false) { |
||
| 169 | $ids = $model->searchPagesByDescription(); |
||
| 170 | Yii::$app->cache->set( |
||
| 171 | $cacheKey, |
||
| 172 | $ids, |
||
| 173 | 86400, |
||
| 174 | new TagDependency( |
||
| 175 | [ |
||
| 176 | 'tags' => ActiveRecordHelper::getCommonTag(Page::className()), |
||
| 177 | ] |
||
| 178 | ) |
||
| 179 | ); |
||
| 180 | } |
||
| 181 | $pages = new Pagination( |
||
| 182 | [ |
||
| 183 | 'defaultPageSize' => $this->module->searchResultsLimit, |
||
| 184 | 'forcePageParam' => false, |
||
| 185 | 'pageSizeLimit' => [ |
||
| 186 | $this->module->minPagesPerList, |
||
| 187 | $this->module->maxPagesPerList |
||
| 188 | ], |
||
| 189 | 'totalCount' => count($ids), |
||
| 190 | ] |
||
| 191 | ); |
||
| 192 | $cacheKey .= ' : ' . $pages->offset; |
||
| 193 | $pagelist = Yii::$app->cache->get($cacheKey); |
||
| 194 | View Code Duplication | if ($pagelist === false) { |
|
| 195 | $pagelist = Page::find()->where( |
||
| 196 | [ |
||
| 197 | 'in', |
||
| 198 | '`id`', |
||
| 199 | array_slice( |
||
| 200 | $ids, |
||
| 201 | $pages->offset, |
||
| 202 | $pages->limit |
||
| 203 | ) |
||
| 204 | ] |
||
| 205 | )->addOrderBy('sort_order')->with('images')->all(); |
||
| 206 | Yii::$app->cache->set( |
||
| 207 | $cacheKey, |
||
| 208 | $pagelist, |
||
| 209 | 86400, |
||
| 210 | new TagDependency( |
||
| 211 | [ |
||
| 212 | 'tags' => ActiveRecordHelper::getCommonTag(Page::className()), |
||
| 213 | ] |
||
| 214 | ) |
||
| 215 | ); |
||
| 216 | } |
||
| 217 | Yii::$app->response->format = Response::FORMAT_JSON; |
||
| 218 | return [ |
||
| 219 | 'view' => $this->renderPartial( |
||
| 220 | 'search', |
||
| 221 | [ |
||
| 222 | 'model' => $model, |
||
| 223 | 'pagelist' => $pagelist, |
||
| 224 | 'pages' => $pages, |
||
| 225 | ] |
||
| 226 | ), |
||
| 227 | 'totalCount' => count($ids), |
||
| 228 | ]; |
||
| 229 | } |
||
| 230 | |||
| 269 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.