| Conditions | 9 |
| Paths | 22 |
| Total Lines | 149 |
| Code Lines | 70 |
| 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 |
||
| 146 | public function create($COMMENT, $reportdata) { |
||
| 147 | // For when a user posts a report on a comment. |
||
| 148 | // $reportdata is an array like: |
||
| 149 | // array ( |
||
| 150 | // 'body' => 'some text', |
||
| 151 | // 'firstname' => 'Billy', |
||
| 152 | // 'lastname' => 'Nomates', |
||
| 153 | // 'email' => '[email protected]' |
||
| 154 | // ) |
||
| 155 | // But if the report was made by a logged-in user, only the |
||
| 156 | // 'body' element should really contain anything, because |
||
| 157 | // we use $THEUSER's id to get the rest. |
||
| 158 | |||
| 159 | // $COMMENT is an existing COMMENT object, needed for setting |
||
| 160 | // its modflag and comment_id. |
||
| 161 | |||
| 162 | global $THEUSER, $PAGE; |
||
| 163 | |||
| 164 | if (!$THEUSER->is_able_to('reportcomment')) { |
||
| 165 | $PAGE->error_message ("Sorry, you are not allowed to post reports."); |
||
| 166 | return false; |
||
| 167 | } |
||
| 168 | |||
| 169 | if (is_numeric($THEUSER->user_id()) && $THEUSER->user_id() > 0) { |
||
| 170 | // Flood check - make sure the user hasn't just posted a report recently. |
||
| 171 | // To help prevent accidental duplicates, among other nasty things. |
||
| 172 | // (Non-logged in users are all id == 0.) |
||
| 173 | |||
| 174 | $flood_time_limit = 20; // How many seconds until a user can post again? |
||
| 175 | |||
| 176 | $q = $this->db->query("SELECT report_id |
||
| 177 | FROM commentreports |
||
| 178 | WHERE user_id = '" . $THEUSER->user_id() . "' |
||
| 179 | AND reported + 0 > NOW() - $flood_time_limit"); |
||
| 180 | |||
| 181 | if ($q->rows() > 0) { |
||
| 182 | $PAGE->error_message("Sorry, we limit people to posting one report per $flood_time_limit seconds to help prevent duplicate reports. Please go back and try again, thanks."); |
||
| 183 | return false; |
||
| 184 | } |
||
| 185 | } |
||
| 186 | |||
| 187 | |||
| 188 | // Tidy up body. |
||
| 189 | $body = filter_user_input($reportdata['body'], 'comment'); // In utility.php |
||
| 190 | |||
| 191 | $time = gmdate("Y-m-d H:i:s"); |
||
| 192 | |||
| 193 | if ($THEUSER->isloggedin()) { |
||
| 194 | $sql = "INSERT INTO commentreports |
||
| 195 | (comment_id, body, reported, user_id) |
||
| 196 | VALUES (:comment_id, |
||
| 197 | :body, |
||
| 198 | :time, |
||
| 199 | :user_id |
||
| 200 | ) |
||
| 201 | "; |
||
| 202 | $params = array( |
||
| 203 | ':comment_id' => $COMMENT->comment_id(), |
||
| 204 | ':body' => $body, |
||
| 205 | ':time' => $time, |
||
| 206 | ':user_id' => $THEUSER->user_id() |
||
| 207 | ); |
||
| 208 | } else { |
||
| 209 | $sql = "INSERT INTO commentreports |
||
| 210 | (comment_id, body, reported, firstname, lastname, email) |
||
| 211 | VALUES (:comment_id, |
||
| 212 | :body, |
||
| 213 | :time, |
||
| 214 | :firstname, |
||
| 215 | :lastname, |
||
| 216 | |||
| 217 | ) |
||
| 218 | "; |
||
| 219 | $params = array( |
||
| 220 | ':comment_id' => $COMMENT->comment_id(), |
||
| 221 | ':body' => $body, |
||
| 222 | ':time' => $time, |
||
| 223 | ':firstname' => $reportdata['firstname'], |
||
| 224 | ':lastname' => $reportdata['lastname'], |
||
| 225 | ':email' => $reportdata['email'], |
||
| 226 | ); |
||
| 227 | } |
||
| 228 | |||
| 229 | $q = $this->db->query($sql, $params); |
||
| 230 | |||
| 231 | if ($q->success()) { |
||
| 232 | // Inserted OK, so set up this object's variables. |
||
| 233 | $this->report_id = $q->insert_id(); |
||
| 234 | $this->comment_id = $COMMENT->comment_id(); |
||
| 235 | $this->body = $body; |
||
| 236 | $this->reported = $time; |
||
| 237 | |||
| 238 | if ($THEUSER->isloggedin()) { |
||
| 239 | $this->user_id = $THEUSER->user_id(); |
||
| 240 | $this->firstname = $THEUSER->firstname(); |
||
| 241 | $this->lastname = $THEUSER->lastname(); |
||
| 242 | } else { |
||
| 243 | $this->email = $reportdata['email']; |
||
| 244 | $this->firstname = $reportdata['firstname']; |
||
| 245 | $this->lastname = $reportdata['lastname']; |
||
| 246 | } |
||
| 247 | |||
| 248 | |||
| 249 | // Set the comment's modflag to on. |
||
| 250 | $COMMENT->set_modflag('on'); |
||
| 251 | |||
| 252 | |||
| 253 | // Notify those who need to know that there's a new report. |
||
| 254 | |||
| 255 | $URL = new \MySociety\TheyWorkForYou\Url('admin_commentreport'); |
||
| 256 | $URL->insert(array( |
||
| 257 | 'rid'=>$this->report_id, |
||
| 258 | 'cid'=>$this->comment_id |
||
| 259 | )); |
||
| 260 | |||
| 261 | $emailbody = "A new comment report has been filed by " . $this->user_name() . ".\n\n"; |
||
| 262 | $emailbody .= "COMMENT:\n" . $COMMENT->body() . "\n\n"; |
||
| 263 | $emailbody .= "REPORT:\n" . $this->body . "\n\n"; |
||
| 264 | $emailbody .= "To manage this report follow this link: https://" . DOMAIN . $URL->generate('none') . "\n"; |
||
|
1 ignored issue
–
show
|
|||
| 265 | |||
| 266 | send_email(REPORTLIST, 'New comment report', $emailbody); |
||
|
1 ignored issue
–
show
|
|||
| 267 | |||
| 268 | |||
| 269 | // Send an email to the user to thank them. |
||
| 270 | |||
| 271 | if ($THEUSER->isloggedin()) { |
||
| 272 | $email = $THEUSER->email(); |
||
| 273 | } else { |
||
| 274 | $email = $this->email(); |
||
| 275 | } |
||
| 276 | |||
| 277 | $data = array ( |
||
| 278 | 'to' => $email, |
||
| 279 | 'template' => 'report_acknowledge' |
||
| 280 | ); |
||
| 281 | $merge = array ( |
||
| 282 | 'FIRSTNAME' => $this->firstname(), |
||
| 283 | 'LASTNAME' => $this->lastname(), |
||
| 284 | 'COMMENTURL' => "https://" . DOMAIN . $COMMENT->url(), |
||
| 285 | 'REPORTBODY' => strip_tags($this->body()) |
||
| 286 | ); |
||
| 287 | |||
| 288 | |||
| 289 | // send_template_email in utility.php. |
||
| 290 | send_template_email($data, $merge); |
||
| 291 | |||
| 292 | return true; |
||
| 293 | } else { |
||
| 294 | return false; |
||
| 295 | } |
||
| 457 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.