Conditions | 8 |
Paths | 23 |
Total Lines | 52 |
Code Lines | 30 |
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 |
||
130 | public function meta(?string $id = null): void |
||
131 | { |
||
132 | $id = (int)$id; |
||
133 | $isEdit = !empty($id); |
||
134 | $pid = $this->getRequest()->getQuery('pid', null); |
||
135 | $isAnswer = !empty($pid); |
||
136 | |||
137 | if ($isAnswer) { |
||
138 | /** @var PostingInterface */ |
||
139 | $parent = $this->Entries->get($pid)->toPosting()->withCurrentUser($this->CurrentUser); |
||
140 | |||
141 | // Don't leak content of forbidden categories |
||
142 | if ($parent->isAnsweringForbidden()) { |
||
143 | throw new SaitoForbiddenException( |
||
144 | 'Access to parent in PostingsController:meta() forbidden.', |
||
145 | ['CurrentUser' => $this->CurrentUser] |
||
146 | ); |
||
147 | } |
||
148 | |||
149 | $this->set('parent', $parent); |
||
150 | } |
||
151 | |||
152 | if ($isEdit) { |
||
153 | /** @var PostingInterface */ |
||
154 | $posting = $this->Entries->get($id)->toPosting()->withCurrentUser($this->CurrentUser); |
||
155 | if (!$posting->isEditingAllowed()) { |
||
156 | throw new SaitoForbiddenException( |
||
157 | 'Access to posting in PostingsController:meta() forbidden.', |
||
158 | ['CurrentUser' => $this->CurrentUser] |
||
159 | ); |
||
160 | } |
||
161 | $this->set('posting', $posting); |
||
162 | } else { |
||
163 | /// We currently don't save drafts for edits |
||
164 | $where = ['user_id' => $this->CurrentUser->getId()]; |
||
165 | if (is_numeric($pid)) { |
||
166 | $where['pid'] = $pid; |
||
167 | } |
||
168 | $draft = $this->Entries->Drafts->find()->where($where)->first(); |
||
169 | |||
170 | if ($draft) { |
||
171 | $this->set('draft', $draft); |
||
172 | } |
||
173 | } |
||
174 | |||
175 | $settings = Configure::read('Saito.Settings'); |
||
176 | |||
177 | $this->set(compact('isAnswer', 'isEdit', 'settings')); |
||
178 | |||
179 | $action = $isAnswer ? 'answer' : 'thread'; |
||
180 | $categories = $this->CurrentUser->getCategories()->getAll($action, 'list'); |
||
181 | $this->set('categories', $categories); |
||
182 | } |
||
184 |