| Conditions | 5 |
| Paths | 9 |
| Total Lines | 77 |
| Code Lines | 45 |
| 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 |
||
| 61 | public function actionList($id) |
||
| 62 | { |
||
| 63 | if (null === $model = Page::findById($id)) { |
||
| 64 | throw new NotFoundHttpException; |
||
| 65 | } |
||
| 66 | |||
| 67 | $this->registerMetaDescription($model); |
||
| 68 | |||
| 69 | $cacheKey = 'PagesList:'.$model->id; |
||
| 70 | |||
| 71 | // query that needed for pages retrieve and pagination |
||
| 72 | $children = Page::find() |
||
| 73 | ->where(['parent_id' => $model->id, 'published' => 1]) |
||
| 74 | ->orderBy('date_added DESC, sort_order') |
||
| 75 | ->with('images'); |
||
| 76 | |||
| 77 | // count all pages |
||
| 78 | $count = Yii::$app->cache->get($cacheKey.';count'); |
||
| 79 | if ($count === false) { |
||
| 80 | $countQuery = clone $children; |
||
| 81 | $count = $countQuery->count(); |
||
| 82 | Yii::$app->cache->set($cacheKey.';count', $count, 86400, new TagDependency([ |
||
| 83 | 'tags' => [ |
||
| 84 | ActiveRecordHelper::getCommonTag(Page::className()) |
||
| 85 | ] |
||
| 86 | ])); |
||
| 87 | } |
||
| 88 | |||
| 89 | $pages = new Pagination( |
||
| 90 | [ |
||
| 91 | 'defaultPageSize' => $this->module->pagesPerList, |
||
| 92 | 'forcePageParam' => false, |
||
| 93 | 'pageSizeLimit' => [ |
||
| 94 | $this->module->minPagesPerList, |
||
| 95 | $this->module->maxPagesPerList |
||
| 96 | ], |
||
| 97 | 'totalCount' => $count, |
||
| 98 | ] |
||
| 99 | ); |
||
| 100 | |||
| 101 | // append current page number to cache key |
||
| 102 | $cacheKey .= ';page:' . $pages->page; |
||
| 103 | |||
| 104 | $childrenModels = Yii::$app->cache->get($cacheKey); |
||
| 105 | if ($childrenModels === false) { |
||
| 106 | |||
| 107 | /** @var ActiveQuery $children */ |
||
| 108 | |||
| 109 | $children = $children->offset($pages->offset) |
||
| 110 | ->limit($pages->limit) |
||
| 111 | ->all(); |
||
| 112 | Yii::$app->cache->set($cacheKey, $children, 86400, new TagDependency([ |
||
| 113 | 'tags' => [ |
||
| 114 | ActiveRecordHelper::getCommonTag(Page::className()) |
||
| 115 | ] |
||
| 116 | ])); |
||
| 117 | } else { |
||
| 118 | $children = $childrenModels; |
||
| 119 | } |
||
| 120 | |||
| 121 | $this->view->title = $model->title; |
||
| 122 | if (!empty($model->h1)) { |
||
| 123 | $this->view->blocks['h1'] = $model->h1; |
||
| 124 | } |
||
| 125 | $this->view->blocks['content'] = $model->content; |
||
| 126 | $this->view->blocks['announce'] = $model->announce; |
||
| 127 | |||
| 128 | return $this->render( |
||
| 129 | $this->computeViewFile($model, 'list'), |
||
| 130 | [ |
||
| 131 | 'model' => $model, |
||
| 132 | 'children' => $children, |
||
| 133 | 'pages' => $pages, |
||
| 134 | 'breadcrumbs' => $this->buildBreadcrumbsArray($model), |
||
| 135 | ] |
||
| 136 | ); |
||
| 137 | } |
||
| 138 | |||
| 283 |
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.