Conditions | 12 |
Paths | 19 |
Total Lines | 44 |
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 |
||
33 | public function delete(string $type, ?string $id = null): ?string |
||
34 | { |
||
35 | // sounds like a multiply delete definition |
||
36 | if (!$id || (int)$id < 1) { |
||
37 | $ids = $this->request->query->get('selected'); |
||
38 | if (!Any::isArray($ids) || !Arr::onlyNumericValues($ids)) { |
||
39 | throw new NotFoundException('Bad conditions'); |
||
40 | } |
||
41 | |||
42 | $id = $ids; |
||
43 | } else { |
||
44 | $id = [$id]; |
||
45 | } |
||
46 | |||
47 | // prepare query to db |
||
48 | /** @var CommentPost|CommentAnswer $query */ |
||
49 | $query = null; |
||
50 | switch ($type) { |
||
51 | case self::TYPE_COMMENT: |
||
|
|||
52 | $query = CommentPost::whereIn('id', $id); |
||
53 | break; |
||
54 | case self::TYPE_ANSWER: |
||
55 | $query = CommentAnswer::whereIn('id', $id); |
||
56 | break; |
||
57 | } |
||
58 | |||
59 | // check if result is not empty |
||
60 | if (!$query || $query->count() < 1) { |
||
61 | throw new NotFoundException(__('No comments found for this condition')); |
||
62 | } |
||
63 | |||
64 | // initialize model |
||
65 | $model = new FormCommentDelete($query, $type); |
||
66 | |||
67 | // check if delete is submited |
||
68 | if ($model->send() && $model->validate()) { |
||
69 | $model->make(); |
||
70 | App::$Session->getFlashBag()->add('success', __('Comments or answers are successful deleted!')); |
||
71 | $this->response->redirect('comments/' . ($type === self::TYPE_ANSWER ? 'answerlist' : 'index')); |
||
72 | } |
||
73 | |||
74 | // render view |
||
75 | return $this->view->render('comments/delete', [ |
||
76 | 'model' => $model |
||
77 | ]); |
||
80 |