| Conditions | 11 |
| Paths | 19 |
| Total Lines | 41 |
| Code Lines | 24 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 32 | public function publish(string $type, ?string $id = null): ?string |
||
| 33 | { |
||
| 34 | // check if it multiple accept ids |
||
| 35 | if (!$id || (int)$id < 1) { |
||
| 36 | $ids = $this->request->query->get('selected'); |
||
| 37 | if (!Any::isArray($ids) || !Arr::onlyNumericValues($ids)) { |
||
| 38 | throw new NotFoundException('Bad conditions'); |
||
| 39 | } |
||
| 40 | $id = $ids; |
||
| 41 | } else { |
||
| 42 | $id = [$id]; |
||
| 43 | } |
||
| 44 | |||
| 45 | /** @var CommentPost|CommentAnswer $query */ |
||
| 46 | $query = null; |
||
| 47 | switch ($type) { |
||
| 48 | case static::TYPE_COMMENT: |
||
|
|
|||
| 49 | $query = CommentPost::whereIn('id', $id)->where('moderate', true); |
||
| 50 | break; |
||
| 51 | case static::TYPE_ANSWER: |
||
| 52 | $query = CommentAnswer::whereIn('id', $id)->where('moderate', true); |
||
| 53 | break; |
||
| 54 | } |
||
| 55 | |||
| 56 | // check if result is not empty |
||
| 57 | if (!$query || $query->count() < 1) { |
||
| 58 | throw new NotFoundException(__('No comments found for this condition')); |
||
| 59 | } |
||
| 60 | |||
| 61 | // initialize moderation model |
||
| 62 | $model = new FormCommentModerate($query, $type); |
||
| 63 | |||
| 64 | // check if form is submited |
||
| 65 | if ($model->send()) { |
||
| 66 | $model->make(); |
||
| 67 | App::$Session->getFlashBag()->add('success', __('Comments or answers are successful published')); |
||
| 68 | $this->response->redirect('comments/' . ($type === 'answer' ? 'answerlist' : 'index')); |
||
| 69 | } |
||
| 70 | |||
| 71 | return $this->view->render('comments/publish', [ |
||
| 72 | 'model' => $model |
||
| 73 | ]); |
||
| 76 |