Conditions | 11 |
Paths | 17 |
Total Lines | 42 |
Code Lines | 26 |
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 update(string $type, string $id): ?string |
||
33 | { |
||
34 | if (!Any::isInt($id) || $id < 1) { |
||
35 | throw new NotFoundException('Bad id format'); |
||
36 | } |
||
37 | // get active record based on type (post or answer for post) |
||
38 | $record = null; |
||
39 | $postId = $id; |
||
40 | switch ($type) { |
||
41 | case 'post': |
||
|
|||
42 | $record = FeedbackPost::find($id); |
||
43 | break; |
||
44 | case 'answer': |
||
45 | $record = FeedbackAnswer::find($id); |
||
46 | if ($record !== null && $record !== false) { |
||
47 | $postId = (int)$record->getFeedbackPost()->id; |
||
48 | } |
||
49 | break; |
||
50 | } |
||
51 | |||
52 | // try what we got |
||
53 | if ($record === null || $record === false) { |
||
54 | throw new NotFoundException(__('Feedback item is not founded')); |
||
55 | } |
||
56 | |||
57 | // initialize model |
||
58 | $model = new FormUpdate($record); |
||
59 | if ($model->send()) { |
||
60 | if ($model->validate()) { |
||
61 | $model->make(); |
||
62 | App::$Session->getFlashBag()->add('success', __('Feedback item are successful changed')); |
||
63 | $this->response->redirect('feedback/read/' . $postId); |
||
64 | } else { |
||
65 | App::$Session->getFlashBag()->add('danger', __('Updating is failed')); |
||
66 | } |
||
67 | } |
||
68 | |||
69 | // render output view |
||
70 | return $this->view->render('update', [ |
||
71 | 'model' => $model |
||
72 | ]); |
||
73 | } |
||
74 | } |
||
75 |
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.