Conditions | 12 |
Paths | 19 |
Total Lines | 45 |
Code Lines | 25 |
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 === null || (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 | $query = null; |
||
49 | switch ($type) { |
||
50 | case self::TYPE_COMMENT: |
||
|
|||
51 | $query = CommentPost::whereIn('id', $id); |
||
52 | break; |
||
53 | case self::TYPE_ANSWER: |
||
54 | $query = CommentAnswer::whereIn('id', $id); |
||
55 | break; |
||
56 | } |
||
57 | |||
58 | // check if result is not empty |
||
59 | if ($query === null || $query->count() < 1) { |
||
60 | throw new NotFoundException(__('No comments found for this condition')); |
||
61 | } |
||
62 | |||
63 | // initialize model |
||
64 | $model = new FormCommentDelete($query, $type); |
||
65 | |||
66 | // check if delete is submited |
||
67 | if ($model->send() && $model->validate()) { |
||
68 | $model->make(); |
||
69 | App::$Session->getFlashBag()->add('success', __('Comments or answers are successful deleted!')); |
||
70 | $this->response->redirect('comments/' . ($type === 'answer' ? 'answerlist' : 'index')); |
||
71 | } |
||
72 | |||
73 | // render view |
||
74 | return $this->view->render('delete', [ |
||
75 | 'model' => $model |
||
76 | ]); |
||
77 | } |
||
78 | } |
||
79 |
As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next
break
.There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.