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