| Conditions | 18 |
| Paths | 1800 |
| Total Lines | 102 |
| Code Lines | 59 |
| 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 |
||
| 176 | public function doPostComment($data, $form) |
||
| 177 | { |
||
| 178 | // Load class and parent from data |
||
| 179 | if (isset($data['ParentClassName'])) { |
||
| 180 | $this->controller->setParentClass($data['ParentClassName']); |
||
| 181 | } |
||
| 182 | if (isset($data['ParentID']) && ($class = $this->controller->getParentClass())) { |
||
| 183 | $this->controller->setOwnerRecord($class::get()->byID($data['ParentID'])); |
||
| 184 | } |
||
| 185 | if (!$this->controller->getOwnerRecord()) { |
||
| 186 | return $this->httpError(404); |
||
| 187 | } |
||
| 188 | |||
| 189 | // cache users data |
||
| 190 | Cookie::set('CommentsForm_UserData', Convert::raw2json($data)); |
||
| 191 | Cookie::set('CommentsForm_Comment', $data['Comment']); |
||
| 192 | |||
| 193 | // extend hook to allow extensions. Also see onAfterPostComment |
||
| 194 | $this->controller->extend('onBeforePostComment', $form); |
||
| 195 | |||
| 196 | // If commenting can only be done by logged in users, make sure the user is logged in |
||
| 197 | if (!$this->controller->getOwnerRecord()->canPostComment()) { |
||
| 198 | return Security::permissionFailure( |
||
| 199 | $this, |
||
| 200 | _t( |
||
| 201 | 'SilverStripe\\Comments\\Controllers\\CommentingController.PERMISSIONFAILURE', |
||
| 202 | "You're not able to post comments to this page. Please ensure you are logged in and have an " |
||
| 203 | . 'appropriate permission level.' |
||
| 204 | ) |
||
| 205 | ); |
||
| 206 | } |
||
| 207 | |||
| 208 | if ($member = Security::getCurrentUser()) { |
||
| 209 | $form->Fields()->push(new HiddenField('AuthorID', 'Author ID', $member->ID)); |
||
| 210 | } |
||
| 211 | |||
| 212 | // What kind of moderation is required? |
||
| 213 | switch ($this->controller->getOwnerRecord()->ModerationRequired) { |
||
| 214 | case 'Required': |
||
| 215 | $requireModeration = true; |
||
| 216 | break; |
||
| 217 | case 'NonMembersOnly': |
||
| 218 | $requireModeration = empty($member); |
||
| 219 | break; |
||
| 220 | case 'None': |
||
| 221 | default: |
||
| 222 | $requireModeration = false; |
||
| 223 | break; |
||
| 224 | } |
||
| 225 | |||
| 226 | $comment = Comment::create(); |
||
| 227 | $form->saveInto($comment); |
||
| 228 | |||
| 229 | $comment->ParentID = $data['ParentID']; |
||
| 230 | $comment->ParentClass = $data['ParentClassName']; |
||
| 231 | |||
| 232 | $comment->AllowHtml = $this->controller->getOption('html_allowed'); |
||
| 233 | $comment->Moderated = !$requireModeration; |
||
| 234 | |||
| 235 | // Save into DB, or call pre-save hooks to give accurate preview |
||
| 236 | $usePreview = $this->controller->getOption('use_preview'); |
||
| 237 | $isPreview = $usePreview && !empty($data['IsPreview']); |
||
| 238 | if ($isPreview) { |
||
| 239 | $comment->extend('onBeforeWrite'); |
||
| 240 | } else { |
||
| 241 | $comment->write(); |
||
| 242 | |||
| 243 | // extend hook to allow extensions. Also see onBeforePostComment |
||
| 244 | $this->controller->extend('onAfterPostComment', $comment); |
||
| 245 | } |
||
| 246 | |||
| 247 | // we want to show a notification if comments are moderated |
||
| 248 | if ($requireModeration && !$comment->IsSpam) { |
||
| 249 | Session::set('CommentsModerated', 1); |
||
| 250 | } |
||
| 251 | |||
| 252 | // clear the users comment since it passed validation |
||
| 253 | Cookie::set('CommentsForm_Comment', false); |
||
| 254 | |||
| 255 | // Find parent link |
||
| 256 | if (!empty($data['ReturnURL'])) { |
||
| 257 | $url = $data['ReturnURL']; |
||
| 258 | } elseif ($parent = $comment->Parent()) { |
||
| 259 | $url = $parent->Link(); |
||
| 260 | } else { |
||
| 261 | return $this->controller->redirectBack(); |
||
| 262 | } |
||
| 263 | |||
| 264 | // Given a redirect page exists, attempt to link to the correct anchor |
||
| 265 | if ($comment->IsSpam) { |
||
| 266 | // Link to the form with the error message contained |
||
| 267 | $hash = $form->FormName(); |
||
| 268 | } elseif (!$comment->Moderated) { |
||
| 269 | // Display the "awaiting moderation" text |
||
| 270 | $hash = 'moderated'; |
||
| 271 | } else { |
||
| 272 | // Link to the moderated, non-spam comment |
||
| 273 | $hash = $comment->Permalink(); |
||
| 274 | } |
||
| 275 | |||
| 276 | return $this->controller->redirect(Controller::join_links($url, "#{$hash}")); |
||
| 277 | } |
||
| 278 | } |
||
| 279 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: