Conditions | 10 |
Paths | 7 |
Total Lines | 52 |
Code Lines | 26 |
Lines | 3 |
Ratio | 5.77 % |
Changes | 6 | ||
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 |
||
78 | public function actionRead() |
||
79 | { |
||
80 | // get raw path without controller-action |
||
81 | $rawPath = App::$Request->getPathWithoutControllerAction(); |
||
82 | $arrayPath = explode('/', $rawPath); |
||
83 | // get category and content item path as string |
||
84 | $contentPath = array_pop($arrayPath); |
||
85 | $categoryPath = implode('/', $arrayPath); |
||
86 | |||
87 | // try to find category object by string pathway |
||
88 | $categoryRecord = ContentCategory::getByPath($categoryPath); |
||
89 | |||
90 | // if no categories are available for this path - throw exception |
||
91 | if ($categoryRecord === null || $categoryRecord->count() < 1) { |
||
92 | throw new NotFoundException(); |
||
93 | } |
||
94 | |||
95 | // try to find content entity record |
||
96 | $contentRecord = ContentEntity::where('path', '=', $contentPath)->where('category_id', '=', $categoryRecord->id); |
||
97 | $trash = false; |
||
98 | |||
99 | // if no entity is founded for this path lets try to find on trashed |
||
100 | if ($contentRecord === null || $contentRecord->count() !== 1) { |
||
101 | // check if user can access to content list on admin panel |
||
102 | View Code Duplication | if (!App::$User->isAuth() || !App::$User->identity()->getRole()->can('Admin/Content/Index')) { |
|
|
|||
103 | throw new NotFoundException(); |
||
104 | } |
||
105 | // lets try to find in trashed |
||
106 | $contentRecord->withTrashed(); |
||
107 | // no way, lets throw exception |
||
108 | if ($contentRecord->count() !== 1) { |
||
109 | throw new NotFoundException(); |
||
110 | } |
||
111 | // set trashed marker for this item |
||
112 | $trash = true; |
||
113 | } |
||
114 | |||
115 | // lets init entity model for content transfer to view |
||
116 | $model = new EntityContentRead($categoryRecord, $contentRecord->first()); |
||
117 | $search = null; |
||
118 | // check if similar search is enabled for item category |
||
119 | if ((int)$model->getCategory()->getProperty('showSimilar') === 1 && $trash === false) { |
||
120 | $search = new EntityContentSearch($model->title, $model->id); |
||
121 | } |
||
122 | |||
123 | return App::$View->render('read', [ |
||
124 | 'model' => $model, |
||
125 | 'search' => $search, |
||
126 | 'trash' => $trash, |
||
127 | 'configs' => $this->getConfigs() |
||
128 | ]); |
||
129 | } |
||
130 | |||
220 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.