| Conditions | 21 |
| Paths | 1440 |
| Total Lines | 100 |
| Code Lines | 77 |
| Lines | 6 |
| Ratio | 6 % |
| 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 |
||
| 72 | public function sendemailafriend($RAW_data, $form) |
||
| 73 | { |
||
| 74 | $adminEmail = Config::inst()->get("Email", "admin_email"); |
||
| 75 | $data = Convert::raw2sql($RAW_data); |
||
| 76 | if ($page = Page::get()->byID(intval($data['PageID']))) { |
||
| 77 | $pageLink = $page->AbsoluteLink(); |
||
| 78 | } |
||
| 79 | $toList = array(); |
||
| 80 | if ($this->Config()->get("mail_to_site_owner_only") != 'yes') { |
||
| 81 | $tos = explode(',', $data['To']); |
||
| 82 | foreach ($tos as $to) { |
||
| 83 | $toList = array_merge($toList, explode(';', $to)); |
||
| 84 | } |
||
| 85 | } else { |
||
| 86 | $toList[] = $adminEmail; |
||
| 87 | } |
||
| 88 | if ($data['YourMailAddress']) { |
||
| 89 | $toList[] = $data['YourMailAddress']; |
||
| 90 | } |
||
| 91 | $ip = EmailAFriendExtension::get_ip_user(); |
||
| 92 | $count = 0; |
||
| 93 | if ($this->Config()->get("EmailAFriendExtension", "max_message_phour_pip")) { |
||
| 94 | $anHourAgo = date('Y-m-d H:i:s', mktime(date('G') - 1, date('i'), date('s'), date('n'), date('j'), date('Y'))); |
||
| 95 | $count = FriendEmail::get()->filter( |
||
| 96 | array( |
||
| 97 | "IPAddress" => $ip, |
||
| 98 | "Created:GreaterThan" => $anHourAgo |
||
| 99 | ) |
||
| 100 | )->count(); |
||
| 101 | } |
||
| 102 | if ($this->Config()->get("mail_to_site_owner_only") != 'yes') { |
||
| 103 | $mailFrom = $data['YourMailAddress']; |
||
| 104 | } else { |
||
| 105 | if ($this->Config()->get("EmailAFriendExtension", "sender_name")) { |
||
| 106 | $mailFrom = $this->Config()->get("EmailAFriendExtension", "sender_name"); |
||
| 107 | if ($this->Config()->get("EmailAFriendRole", "sender_email_address")) { |
||
| 108 | $mailFrom .= ' <' . $this->Config()->get("EmailAFriendExtension", "sender_email_address") . '>'; |
||
| 109 | } |
||
| 110 | } elseif ($this->Config()->get("EmailAFriendExtension", "sender_email_address")) { |
||
| 111 | $mailFrom = $this->Config()->get("EmailAFriendExtension", "sender_email_address"); |
||
| 112 | } else { |
||
| 113 | $mailFrom = $adminEmail; |
||
| 114 | } |
||
| 115 | } |
||
| 116 | foreach ($toList as $index => $to) { |
||
| 117 | $messagesPerHour = $this->Config()->get("EmailAFriendExtension", "max_message_phour_pip"); |
||
| 118 | if ($messagesPerHour && $count > $messagesPerHour) { |
||
| 119 | $stopIndex = $index; |
||
| 120 | break; |
||
| 121 | } else { |
||
| 122 | $friendEmail = new FriendEmail(); |
||
| 123 | $friendEmail->To = $to; |
||
| 124 | $friendEmail->Message = $data['Message']; |
||
| 125 | $friendEmail->From = $data['YourMailAddress']; |
||
| 126 | $friendEmail->IPAddress = $ip; |
||
| 127 | $friendEmail->PageID = $data['PageID']; |
||
| 128 | $friendEmail->write(); |
||
| 129 | $subject = $this->Config()->get("EmailAFriendExtension", "mail_subject"); |
||
| 130 | $subject .= ' | sent by '.$data['YourMailAddress']; |
||
| 131 | $email = new Email( |
||
| 132 | $mailFrom, |
||
| 133 | $to, |
||
| 134 | $subject, |
||
| 135 | Convert::raw2xml($data['Message']) . '<br/><br/>Page Link : ' . $pageLink. '<br /><br />Sent by: '.$data['YourMailAddress'] |
||
| 136 | ); |
||
| 137 | $outcome = $email->send(); |
||
| 138 | if ($outcome) { |
||
| 139 | $count++; |
||
| 140 | } else { |
||
| 141 | unset($toList[$index]); |
||
| 142 | } |
||
| 143 | } |
||
| 144 | } |
||
| 145 | |||
| 146 | if (count($toList) > 0) { |
||
| 147 | $content = ''; |
||
| 148 | $endIndex = isset($stopIndex) ? $stopIndex : count($toList); |
||
| 149 | if (! isset($stopIndex) || $stopIndex > 0) { |
||
| 150 | $content .= '<p class="message good">This page has been successfully e-mailed to the following addresses :</p><ul>'; |
||
| 151 | View Code Duplication | for ($i = 0; $i < $endIndex; $i++) { |
|
| 152 | $content .= '<li>' . $toList[$i] . '</li>'; |
||
| 153 | } |
||
| 154 | $content .= '</ul>'; |
||
| 155 | } |
||
| 156 | if ($endIndex < count($toList)) { |
||
| 157 | $content .= '<p class="message required">This page could not be e-mailed to the following addresses :</p><ul>'; |
||
| 158 | View Code Duplication | for ($i = $endIndex; $i < count($toList); $i++) { |
|
| 159 | $content .= '<li>' . $toList[$i] . '</li>'; |
||
| 160 | } |
||
| 161 | $content .= '</ul>'; |
||
| 162 | } |
||
| 163 | } else { |
||
| 164 | $content = '<p class="message required bad">This page has not been e-mailed to anyone.</p>'; |
||
| 165 | } |
||
| 166 | |||
| 167 | $content .= '<br/><p><a href="' . $this->controller->Link() . '">Send more?</a>.</p>'; |
||
| 168 | |||
| 169 | $templateData = array("EmailAFriendForm" => null, "EmailAFriendThankYouContent" => $content); |
||
| 170 | return $this->customise($templateData)->renderWith('EmailAFriendHolder'); |
||
| 171 | } |
||
| 172 | } |
||
| 173 |
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.