| Conditions | 14 |
| Paths | 60 |
| Total Lines | 126 |
| Code Lines | 75 |
| 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 |
||
| 30 | public function __construct($name, CommentingController $controller) |
||
| 31 | { |
||
| 32 | $usePreview = $controller->getOption('use_preview'); |
||
| 33 | $nameRequired = _t('CommentInterface.YOURNAME_MESSAGE_REQUIRED', 'Please enter your name'); |
||
| 34 | $emailRequired = _t('CommentInterface.EMAILADDRESS_MESSAGE_REQUIRED', 'Please enter your email address'); |
||
| 35 | $emailInvalid = _t('CommentInterface.EMAILADDRESS_MESSAGE_EMAIL', 'Please enter a valid email address'); |
||
| 36 | $urlInvalid = _t('CommentInterface.COMMENT_MESSAGE_URL', 'Please enter a valid URL'); |
||
| 37 | $commentRequired = _t('CommentInterface.COMMENT_MESSAGE_REQUIRED', 'Please enter your comment'); |
||
| 38 | |||
| 39 | $fields = FieldList::create( |
||
| 40 | $dataFields = CompositeField::create( |
||
| 41 | // Name |
||
| 42 | $a = TextField::create('Name', _t('CommentInterface.YOURNAME', 'Your name')) |
||
| 43 | ->setCustomValidationMessage($nameRequired) |
||
| 44 | ->setAttribute('data-msg-required', $nameRequired), |
||
| 45 | |||
| 46 | EmailField::create( |
||
| 47 | 'Email', |
||
| 48 | _t('SilverStripe\\Comments\\Controllers\\CommentingController.EMAILADDRESS', 'Your email address (will not be published)') |
||
| 49 | ) |
||
| 50 | ->setCustomValidationMessage($emailRequired) |
||
| 51 | ->setAttribute('data-msg-required', $emailRequired) |
||
| 52 | ->setAttribute('data-msg-email', $emailInvalid) |
||
| 53 | ->setAttribute('data-rule-email', true), |
||
|
|
|||
| 54 | // Url |
||
| 55 | TextField::create('URL', _t('SilverStripe\\Comments\\Controllers\\CommentingController.WEBSITEURL', 'Your website URL')) |
||
| 56 | ->setAttribute('data-msg-url', $urlInvalid) |
||
| 57 | ->setAttribute('data-rule-url', true), |
||
| 58 | // Comment |
||
| 59 | TextareaField::create('Comment', _t('SilverStripe\\Comments\\Controllers\\CommentingController.COMMENTS', 'Comments')) |
||
| 60 | ->setCustomValidationMessage($commentRequired) |
||
| 61 | ->setAttribute('data-msg-required', $commentRequired) |
||
| 62 | ), |
||
| 63 | HiddenField::create('ParentID'), |
||
| 64 | HiddenField::create('ParentClassName'), |
||
| 65 | HiddenField::create('ReturnURL'), |
||
| 66 | HiddenField::create('ParentCommentID') |
||
| 67 | ); |
||
| 68 | |||
| 69 | // Preview formatted comment. Makes most sense when shortcodes or |
||
| 70 | // limited HTML is allowed. Populated by JS/Ajax. |
||
| 71 | if ($usePreview) { |
||
| 72 | $fields->insertAfter( |
||
| 73 | ReadonlyField::create('PreviewComment', _t('CommentInterface.PREVIEWLABEL', 'Preview')) |
||
| 74 | ->setAttribute('style', 'display: none'), // enable through JS |
||
| 75 | 'Comment' |
||
| 76 | ); |
||
| 77 | } |
||
| 78 | |||
| 79 | $dataFields->addExtraClass('data-fields'); |
||
| 80 | |||
| 81 | // save actions |
||
| 82 | $actions = FieldList::create( |
||
| 83 | $postAction = new FormAction('doPostComment', _t('CommentInterface.POST', 'Post')) |
||
| 84 | ); |
||
| 85 | |||
| 86 | if ($usePreview) { |
||
| 87 | $actions->push( |
||
| 88 | FormAction::create('doPreviewComment', _t('CommentInterface.PREVIEW', 'Preview')) |
||
| 89 | ->addExtraClass('action-minor') |
||
| 90 | ->setAttribute('style', 'display: none') // enable through JS |
||
| 91 | ); |
||
| 92 | } |
||
| 93 | |||
| 94 | $required = new RequiredFields( |
||
| 95 | $controller->config()->required_fields |
||
| 96 | ); |
||
| 97 | |||
| 98 | parent::__construct($controller, $name, $fields, $actions, $required); |
||
| 99 | |||
| 100 | |||
| 101 | // if the record exists load the extra required data |
||
| 102 | if ($record = $controller->getOwnerRecord()) { |
||
| 103 | // Load member data |
||
| 104 | $member = Member::currentUser(); |
||
| 105 | if (($record->CommentsRequireLogin || $record->PostingRequiredPermission) && $member) { |
||
| 106 | $fields = $this->Fields(); |
||
| 107 | |||
| 108 | $fields->removeByName('Name'); |
||
| 109 | $fields->removeByName('Email'); |
||
| 110 | $fields->insertBefore( |
||
| 111 | new ReadonlyField( |
||
| 112 | 'NameView', |
||
| 113 | _t('CommentInterface.YOURNAME', 'Your name'), |
||
| 114 | $member->getName() |
||
| 115 | ), |
||
| 116 | 'URL' |
||
| 117 | ); |
||
| 118 | $fields->push(new HiddenField('Name', '', $member->getName())); |
||
| 119 | $fields->push(new HiddenField('Email', '', $member->Email)); |
||
| 120 | } |
||
| 121 | |||
| 122 | // we do not want to read a new URL when the form has already been submitted |
||
| 123 | // which in here, it hasn't been. |
||
| 124 | $this->loadDataFrom(array( |
||
| 125 | 'ParentID' => $record->ID, |
||
| 126 | 'ReturnURL' => $controller->getRequest()->getURL(), |
||
| 127 | 'ParentClassName' => $controller->getParentClass() |
||
| 128 | )); |
||
| 129 | |||
| 130 | if ($holder = $record->getCommentHolderID()) { |
||
| 131 | $this->setHTMLID($holder); |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | // Set it so the user gets redirected back down to the form upon form fail |
||
| 136 | $this->setRedirectToFormOnValidationError(true); |
||
| 137 | |||
| 138 | // load any data from the cookies |
||
| 139 | if ($data = Cookie::get('CommentsForm_UserData')) { |
||
| 140 | $data = Convert::json2array($data); |
||
| 141 | |||
| 142 | $this->loadDataFrom(array( |
||
| 143 | 'Name' => isset($data['Name']) ? $data['Name'] : '', |
||
| 144 | 'URL' => isset($data['URL']) ? $data['URL'] : '', |
||
| 145 | 'Email' => isset($data['Email']) ? $data['Email'] : '' |
||
| 146 | )); |
||
| 147 | |||
| 148 | // allow previous value to fill if comment not stored in cookie (i.e. validation error) |
||
| 149 | $prevComment = Cookie::get('CommentsForm_Comment'); |
||
| 150 | |||
| 151 | if ($prevComment && $prevComment != '') { |
||
| 152 | $this->loadDataFrom(array('Comment' => $prevComment)); |
||
| 153 | } |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 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: