Conditions | 23 |
Paths | 1729 |
Total Lines | 92 |
Lines | 24 |
Ratio | 26.09 % |
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:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | <?php |
||
52 | public function process($address, $text = null, $subject = null, $encode = null, $cc = null, $bcc = null, $newsgroups = null, $followupto = null, $extra = null) |
||
53 | { |
||
54 | if (empty($address)) { |
||
55 | return ''; |
||
56 | } |
||
57 | if (empty($text)) { |
||
58 | $text = $address; |
||
59 | } |
||
60 | |||
61 | // build address string |
||
62 | $address .= '?'; |
||
63 | |||
64 | if (!empty($subject)) { |
||
65 | $address .= 'subject=' . rawurlencode($subject) . '&'; |
||
66 | } |
||
67 | if (!empty($cc)) { |
||
68 | $address .= 'cc=' . rawurlencode($cc) . '&'; |
||
69 | } |
||
70 | if (!empty($bcc)) { |
||
71 | $address .= 'bcc=' . rawurlencode($bcc) . '&'; |
||
72 | } |
||
73 | if (!empty($newsgroups)) { |
||
74 | $address .= 'newsgroups=' . rawurlencode($newsgroups) . '&'; |
||
75 | } |
||
76 | if (!empty($followupto)) { |
||
77 | $address .= 'followupto=' . rawurlencode($followupto) . '&'; |
||
78 | } |
||
79 | |||
80 | $address = rtrim($address, '?&'); |
||
81 | |||
82 | // output |
||
83 | switch ($encode) { |
||
84 | |||
85 | case 'none': |
||
86 | case null: |
||
87 | return '<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>'; |
||
88 | |||
89 | case 'js': |
||
90 | View Code Duplication | case 'javascript': |
|
|
|||
91 | $str = 'document.write(\'<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>\');'; |
||
92 | $len = strlen($str); |
||
93 | |||
94 | $out = ''; |
||
95 | for ($i = 0; $i < $len; ++ $i) { |
||
96 | $out .= '%' . bin2hex($str[$i]); |
||
97 | } |
||
98 | |||
99 | return '<script type="text/javascript">eval(unescape(\'' . $out . '\'));</script>'; |
||
100 | |||
101 | break; |
||
102 | case 'javascript_charcode': |
||
103 | case 'js_charcode': |
||
104 | case 'jscharcode': |
||
105 | View Code Duplication | case 'jschar': |
|
106 | $str = '<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>'; |
||
107 | $len = strlen($str); |
||
108 | |||
109 | $out = '<script type="text/javascript">' . "\n<!--\ndocument.write(Str.fromCharCode("; |
||
110 | for ($i = 0; $i < $len; ++ $i) { |
||
111 | $out .= ord($str[$i]) . ','; |
||
112 | } |
||
113 | |||
114 | return rtrim($out, ',') . "));\n-->\n</script>\n"; |
||
115 | |||
116 | break; |
||
117 | |||
118 | case 'hex': |
||
119 | if (strpos($address, '?') !== false) { |
||
120 | $this->core->triggerError('Mailto: Hex encoding is not possible with extra attributes, use one of : <em>js, jscharcode or none</em>.', E_USER_WARNING); |
||
121 | } |
||
122 | |||
123 | $out = '<a href="mailto:'; |
||
124 | $len = strlen($address); |
||
125 | for ($i = 0; $i < $len; ++ $i) { |
||
126 | if (preg_match('#\w#', $address[$i])) { |
||
127 | $out .= '%' . bin2hex($address[$i]); |
||
128 | } else { |
||
129 | $out .= $address[$i]; |
||
130 | } |
||
131 | } |
||
132 | $out .= '" ' . $extra . '>'; |
||
133 | $len = strlen($text); |
||
134 | for ($i = 0; $i < $len; ++ $i) { |
||
135 | $out .= '&#x' . bin2hex($text[$i]); |
||
136 | } |
||
137 | |||
138 | return $out . '</a>'; |
||
139 | |||
140 | default: |
||
141 | $this->core->triggerError('Mailto: <em>encode</em> argument is invalid, it must be one of : <em>none (= no value), js, js_charcode or hex</em>', E_USER_WARNING); |
||
142 | } |
||
143 | } |
||
144 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.