| Conditions | 31 |
| Paths | 196 |
| Total Lines | 101 |
| Lines | 4 |
| Ratio | 3.96 % |
| 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 |
||
| 84 | public function update(Request $request, $project_key, $issue_id, $id) |
||
| 85 | { |
||
| 86 | $comments = DB::collection('comments_' . $project_key)->find($id); |
||
| 87 | if (!$comments) { |
||
| 88 | throw new \UnexpectedValueException('the comments does not exist or is not in the project.', -11201); |
||
| 89 | } |
||
| 90 | |||
| 91 | $contents = $request->input('contents'); |
||
| 92 | if (isset($contents)) { |
||
| 93 | if (!$contents) { |
||
| 94 | throw new \UnexpectedValueException('the contents can not be empty.', -11200); |
||
| 95 | } |
||
| 96 | } |
||
| 97 | // record the changed comments |
||
| 98 | $changedComments = []; |
||
| 99 | |||
| 100 | $table = 'comments_' . $project_key; |
||
| 101 | $user = [ 'id' => $this->user->id, 'name' => $this->user->first_name, 'email' => $this->user->email ]; |
||
| 102 | $operation = $request->input('operation'); |
||
| 103 | if (isset($operation)) { |
||
| 104 | if (!in_array($operation, [ 'addReply', 'editReply', 'delReply' ])) { |
||
| 105 | throw new \UnexpectedValueException('the operation is incorrect value.', -11204); |
||
| 106 | } |
||
| 107 | if (!isset($comments['reply']) || !$comments['reply']) { |
||
| 108 | $comments['reply'] = []; |
||
| 109 | } |
||
| 110 | |||
| 111 | if ($operation == 'addReply') { |
||
| 112 | if (!$this->isPermissionAllowed($project_key, 'add_comments')) { |
||
| 113 | return response()->json(['ecode' => -10002, 'emsg' => 'permission denied.']); |
||
| 114 | } |
||
| 115 | |||
| 116 | $reply_id = md5(microtime() . $this->user->id); |
||
| 117 | array_push($comments['reply'], array_only($request->all(), [ 'contents', 'atWho' ]) + [ 'id' => $reply_id , 'creator' => $user, 'created_at' => time() ]); |
||
| 118 | $changedComments = array_only($request->all(), [ 'contents', 'atWho' ]) + [ 'to' => $comments['creator'] ]; |
||
| 119 | } |
||
| 120 | else if ($operation == 'editReply') { |
||
| 121 | $reply_id = $request->input('reply_id'); |
||
| 122 | if (!isset($reply_id) || !$reply_id) { |
||
| 123 | throw new \UnexpectedValueException('the reply id can not be empty.', -11202); |
||
| 124 | } |
||
| 125 | $index = $this->array_find([ 'id' => $reply_id ], $comments['reply']); |
||
| 126 | if ($index !== false) { |
||
| 127 | if (!$this->isPermissionAllowed($project_key, 'edit_comments') && !($comments['reply'][$index]['creator']['id'] == $this->user->id && $this->isPermissionAllowed($project_key, 'edit_self_comments'))) { |
||
| 128 | return response()->json(['ecode' => -10002, 'emsg' => 'permission denied.']); |
||
| 129 | } |
||
| 130 | |||
| 131 | $comments['reply'][$index] = array_merge($comments['reply'][$index], [ 'updated_at' => time(), 'edited_flag' => 1 ] + array_only($request->all(), [ 'contents', 'atWho' ])); |
||
| 132 | $changedComments = array_only($comments['reply'][$index], [ 'contents', 'atWho' ]) + [ 'to' => $comments['creator'] ]; |
||
| 133 | } |
||
| 134 | else |
||
| 135 | { |
||
| 136 | throw new \UnexpectedValueException('the reply does not exist', -11203); |
||
| 137 | } |
||
| 138 | } |
||
| 139 | else if ($operation == 'delReply') { |
||
| 140 | $reply_id = $request->input('reply_id'); |
||
| 141 | if (!isset($reply_id) || !$reply_id) { |
||
| 142 | throw new \UnexpectedValueException('the reply id can not be empty.', -11202); |
||
| 143 | } |
||
| 144 | $index = $this->array_find([ 'id' => $reply_id ], $comments['reply']); |
||
| 145 | if ($index !== false) { |
||
| 146 | if (!$this->isPermissionAllowed($project_key, 'delete_comments') && !($comments['reply'][$index]['creator']['id'] == $this->user->id && $this->isPermissionAllowed($project_key, 'delete_self_comments'))) { |
||
| 147 | return response()->json(['ecode' => -10002, 'emsg' => 'permission denied.']); |
||
| 148 | } |
||
| 149 | |||
| 150 | $changedComments = array_only($comments['reply'][$index], [ 'contents', 'atWho' ]) + [ 'to' => $comments['creator'] ]; |
||
| 151 | array_splice($comments['reply'], $index, 1); |
||
| 152 | } |
||
| 153 | else |
||
| 154 | { |
||
| 155 | throw new \UnexpectedValueException('the reply does not exist', -11203); |
||
| 156 | } |
||
| 157 | } |
||
| 158 | DB::collection($table)->where('_id', $id)->update([ 'reply' => $comments['reply'] ]); |
||
| 159 | } |
||
| 160 | else |
||
| 161 | { |
||
| 162 | View Code Duplication | if (!$this->isPermissionAllowed($project_key, 'edit_comments') && !($comments['creator']['id'] == $this->user->id && $this->isPermissionAllowed($project_key, 'edit_self_comments'))) { |
|
| 163 | return response()->json(['ecode' => -10002, 'emsg' => 'permission denied.']); |
||
| 164 | } |
||
| 165 | |||
| 166 | DB::collection($table)->where('_id', $id)->update([ 'updated_at' => time(), 'edited_flag' => 1 ] + array_only($request->all(), [ 'contents', 'atWho' ])); |
||
| 167 | $changedComments = array_only($request->all(), [ 'contents', 'atWho' ]); |
||
| 168 | } |
||
| 169 | |||
| 170 | // trigger event of comments |
||
| 171 | $event_key = ''; |
||
| 172 | if (isset($operation)) { |
||
| 173 | $operation === 'addReply' && $event_key = 'add_comments'; |
||
| 174 | $operation === 'editReply' && $event_key = 'edit_comments'; |
||
| 175 | $operation === 'delReply' && $event_key = 'del_comments'; |
||
| 176 | } |
||
| 177 | else |
||
| 178 | { |
||
| 179 | $event_key = 'edit_comments'; |
||
| 180 | } |
||
| 181 | Event::fire(new IssueEvent($project_key, $issue_id, $user, [ 'event_key' => $event_key, 'data' => $changedComments ])); |
||
| 182 | |||
| 183 | return response()->json([ 'ecode' => 0, 'data' => parent::arrange(DB::collection($table)->find($id)) ]); |
||
| 184 | } |
||
| 185 | |||
| 231 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.