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