| Conditions | 24 |
| Paths | 937 |
| Total Lines | 108 |
| Code Lines | 72 |
| 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 |
||
| 50 | function smarty_function_mailto($params, &$smarty) |
||
| 51 | { |
||
| 52 | $extra = ''; |
||
| 53 | |||
| 54 | if (empty($params['address'])) { |
||
| 55 | $smarty->trigger_error("mailto: missing 'address' parameter"); |
||
| 56 | return; |
||
| 57 | } else { |
||
| 58 | $address = $params['address']; |
||
| 59 | } |
||
| 60 | |||
| 61 | $text = $address; |
||
| 62 | |||
| 63 | // netscape and mozilla do not decode %40 (@) in BCC field (bug?) |
||
| 64 | // so, don't encode it. |
||
| 65 | $search = array('%40', '%2C'); |
||
| 66 | $replace = array('@', ','); |
||
| 67 | $mail_parms = array(); |
||
| 68 | foreach ($params as $var=>$value) { |
||
| 69 | switch ($var) { |
||
| 70 | case 'cc': |
||
| 71 | case 'bcc': |
||
| 72 | case 'followupto': |
||
| 73 | if (!empty($value)) |
||
| 74 | $mail_parms[] = $var.'='.str_replace($search,$replace,rawurlencode($value)); |
||
| 75 | break; |
||
| 76 | |||
| 77 | case 'subject': |
||
| 78 | case 'newsgroups': |
||
| 79 | $mail_parms[] = $var.'='.rawurlencode($value); |
||
| 80 | break; |
||
| 81 | |||
| 82 | case 'extra': |
||
| 83 | case 'text': |
||
| 84 | $$var = $value; |
||
| 85 | |||
| 86 | default: |
||
| 87 | } |
||
| 88 | } |
||
| 89 | |||
| 90 | $mail_parm_vals = ''; |
||
| 91 | for ($i=0; $i<count($mail_parms); $i++) { |
||
|
|
|||
| 92 | $mail_parm_vals .= (0==$i) ? '?' : '&'; |
||
| 93 | $mail_parm_vals .= $mail_parms[$i]; |
||
| 94 | } |
||
| 95 | $address .= $mail_parm_vals; |
||
| 96 | |||
| 97 | $encode = (empty($params['encode'])) ? 'none' : $params['encode']; |
||
| 98 | if (!in_array($encode,array('javascript','javascript_charcode','hex','none')) ) { |
||
| 99 | $smarty->trigger_error("mailto: 'encode' parameter must be none, javascript or hex"); |
||
| 100 | return; |
||
| 101 | } |
||
| 102 | |||
| 103 | if ($encode == 'javascript' ) { |
||
| 104 | $string = 'document.write(\'<a href="mailto:'.$address.'" '.$extra.'>'.$text.'</a>\');'; |
||
| 105 | |||
| 106 | $js_encode = ''; |
||
| 107 | for ($x=0; $x < strlen($string); $x++) { |
||
| 108 | $js_encode .= '%' . bin2hex($string[$x]); |
||
| 109 | } |
||
| 110 | |||
| 111 | return '<script type="text/javascript">eval(unescape(\''.$js_encode.'\'))</script>'; |
||
| 112 | |||
| 113 | } elseif ($encode == 'javascript_charcode' ) { |
||
| 114 | $string = '<a href="mailto:'.$address.'" '.$extra.'>'.$text.'</a>'; |
||
| 115 | |||
| 116 | for($x = 0, $y = strlen($string); $x < $y; $x++ ) { |
||
| 117 | $ord[] = ord($string[$x]); |
||
| 118 | } |
||
| 119 | |||
| 120 | $_ret = "<script type=\"text/javascript\" language=\"javascript\">\n"; |
||
| 121 | $_ret .= "<!--\n"; |
||
| 122 | $_ret .= "{document.write(String.fromCharCode("; |
||
| 123 | $_ret .= implode(',',$ord); |
||
| 124 | $_ret .= "))"; |
||
| 125 | $_ret .= "}\n"; |
||
| 126 | $_ret .= "//-->\n"; |
||
| 127 | $_ret .= "</script>\n"; |
||
| 128 | |||
| 129 | return $_ret; |
||
| 130 | |||
| 131 | |||
| 132 | } elseif ($encode == 'hex') { |
||
| 133 | |||
| 134 | preg_match('!^(.*)(\?.*)$!',$address,$match); |
||
| 135 | if(!empty($match[2])) { |
||
| 136 | $smarty->trigger_error("mailto: hex encoding does not work with extra attributes. Try javascript."); |
||
| 137 | return; |
||
| 138 | } |
||
| 139 | $address_encode = ''; |
||
| 140 | for ($x=0; $x < strlen($address); $x++) { |
||
| 141 | if(preg_match('!\w!',$address[$x])) { |
||
| 142 | $address_encode .= '%' . bin2hex($address[$x]); |
||
| 143 | } else { |
||
| 144 | $address_encode .= $address[$x]; |
||
| 145 | } |
||
| 146 | } |
||
| 147 | $text_encode = ''; |
||
| 148 | for ($x=0; $x < strlen($text); $x++) { |
||
| 149 | $text_encode .= '&#x' . bin2hex($text[$x]).';'; |
||
| 150 | } |
||
| 151 | |||
| 152 | $mailto = "mailto:"; |
||
| 153 | return '<a href="'.$mailto.$address_encode.'" '.$extra.'>'.$text_encode.'</a>'; |
||
| 154 | |||
| 155 | } else { |
||
| 156 | // no encoding |
||
| 157 | return '<a href="mailto:'.$address.'" '.$extra.'>'.$text.'</a>'; |
||
| 158 | |||
| 166 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: