Conditions | 5 |
Paths | 7 |
Total Lines | 58 |
Code Lines | 35 |
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 |
||
56 | public function handleDelete( |
||
57 | Request $request, |
||
58 | Response $response, |
||
59 | array $args |
||
60 | ): Response { |
||
61 | $id = $args['id']; |
||
62 | $data = $request->getParsedBody() ?? []; |
||
63 | |||
64 | $results = $this->model->validate( |
||
65 | $data, |
||
66 | ['id' => $id, 'context' => 'delete'] |
||
67 | ); |
||
68 | |||
69 | if (!$results['valid']) { |
||
70 | $this->flashManager->addErrors($results['errors']); |
||
71 | return $this->redirectResponse( |
||
72 | $request, |
||
73 | $response, |
||
74 | "/{$this->adminDirName}/index" |
||
75 | ); |
||
76 | } |
||
77 | |||
78 | // validate csrf token |
||
79 | $redirectTo = "/{$this->adminDirName}/delete/{$id}"; |
||
80 | $redirectResponse = $this->validateCsrfToken($data, $request, $response, $redirectTo); |
||
81 | if ($redirectResponse) { |
||
82 | return $redirectResponse; |
||
83 | } |
||
84 | |||
85 | // delete data |
||
86 | try { |
||
87 | if ($this->model->delete($id)) { |
||
88 | $this->flashManager->addMessage( |
||
89 | 'success', |
||
90 | __( |
||
91 | "x_delete_success", |
||
92 | ":name deleted successfully.", |
||
93 | ['name' => __($this->label)] |
||
94 | ) |
||
95 | ); |
||
96 | return $this->redirectResponse( |
||
97 | $request, |
||
98 | $response, |
||
99 | "/{$this->adminDirName}/index" |
||
100 | ); |
||
101 | } |
||
102 | } catch (\Exception $e) { |
||
103 | $this->flashManager->addErrors([ |
||
104 | __( |
||
105 | "x_delete_failed", |
||
106 | "Failed to delete :name", |
||
107 | ['name' => __($this->label)] |
||
108 | ) |
||
109 | ]); |
||
110 | } |
||
111 | |||
112 | $redirectTo = "/{$this->adminDirName}/edit/{$id}"; |
||
113 | return $this->redirectResponse($request, $response, $redirectTo); |
||
114 | } |
||
116 |