Conditions | 30 |
Paths | > 20000 |
Total Lines | 152 |
Code Lines | 112 |
Lines | 67 |
Ratio | 44.08 % |
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 |
||
65 | function imapInfo($header,$ARhosts,$dpl=false, $learn=false) { |
||
66 | /* Get relevant Info from header's mail */ |
||
67 | /* Each line must end with /r/n */ |
||
68 | |||
69 | $dateC = NULL; |
||
70 | $mid = NULL; |
||
71 | $result = array( |
||
72 | 'date' => NULL, |
||
73 | 'from' => NULL, |
||
74 | 'messageid' => NULL, |
||
75 | 'dmarc' => array( |
||
76 | 'result' => NULL, |
||
77 | 'dom' => NULL |
||
78 | ), |
||
79 | 'spf' => array( |
||
80 | 'result' => NULL, |
||
81 | 'dom' => NULL |
||
82 | ), |
||
83 | 'dkim' => array( |
||
84 | 'result' => NULL, |
||
85 | 'dom' => NULL |
||
86 | ), |
||
87 | 'spam' => array( |
||
88 | 'status' => NULL, |
||
89 | 'score' => NULL, |
||
90 | 'th' => NULL, |
||
91 | ), |
||
92 | 'dspam' => array( |
||
93 | 'type' => NULL, |
||
94 | 'level' => NULL, |
||
95 | 'learn' => NULL |
||
96 | ), |
||
97 | 'warn' => NULL |
||
98 | ); |
||
99 | |||
100 | |||
101 | View Code Duplication | if ( preg_match_all ('/^Authentication\-Results:\s+(?<host>[\w\.]+);(?:\s+|\r\n\s+)dmarc=(?<dmarc>\w+)\s+\(p=\w+\s+dis=\w+\)\s+header\.from=(?<DMARCfrom>[\w\.]+)/m',$header,$received) ) { |
|
102 | $k=0; |
||
103 | for ($i = count($received[0])-1;$i>=0;$i--) { |
||
104 | foreach ($ARhosts as $mx) { |
||
105 | if ($mx == $received['host'][$i]) { |
||
106 | /* This is a trusted AR result */ |
||
107 | $result['dmarc']['result']=$received['dmarc'][$i]; |
||
108 | $result['dmarc']['dom'] = $received['DMARCfrom'][$i]; |
||
109 | $k++; |
||
110 | } |
||
111 | } |
||
112 | } |
||
113 | } |
||
114 | $received=NULL; |
||
115 | if ($k>1) $result['warn'][] = 'The trusted DMARC AR Headers are present more than once. Something wrong.'; |
||
116 | |||
117 | View Code Duplication | if ( preg_match_all('/^Authentication\-Results:\s+(?<host>[\w\.]+);(?:\s+|\r\n\s+)spf=(?<spf>\w+)\s+smtp\.(?:mailfrom|helo)=(?<SPFfrom>[\w\.]+)/m',$header,$received) ) { |
|
118 | $k=0; |
||
119 | for ($i = count($received[0])-1;$i>=0;$i--) { |
||
120 | foreach ($ARhosts as $mx) { |
||
121 | if ($mx == $received['host'][$i]) { |
||
122 | /* This is a trusted AR result */ |
||
123 | $result['spf']['result']=$received['spf'][$i]; |
||
124 | $result['spf']['dom'] = $received['SPFfrom'][$i]; |
||
125 | $k++; |
||
126 | } |
||
127 | } |
||
128 | } |
||
129 | } |
||
130 | $received=NULL; |
||
131 | if ($k>1) $result['warn'][] = 'The trusted SPF AR Headers are present more than once. Something wrong.'; |
||
132 | |||
133 | View Code Duplication | if ( preg_match_all('/^Authentication\-Results:\s+(?<host>[\w\.]+);(?:\s+|\r\n\s+)dkim=(?<dkim>\w+)\s+[\w\s\(\)\-]+header\.d=(?<DKIMdom>[\w\.]+)/m',$header,$received) ) { |
|
134 | $k=0; |
||
135 | for ($i = count($received[0])-1;$i>=0;$i--) { |
||
136 | foreach ($ARhosts as $mx) { |
||
137 | if ($mx == $received['host'][$i]) { |
||
138 | /* This is a trusted AR result */ |
||
139 | $result['dkim']['result']=$received['dkim'][$i]; |
||
140 | $result['dkim']['dom'] = $received['DKIMdom'][$i]; |
||
141 | $k++; |
||
142 | } |
||
143 | } |
||
144 | } |
||
145 | } |
||
146 | $received=NULL; |
||
147 | if ($k>1) $result['warn'][] = 'The trusted DKIM AR Headers are present more than once. Something wrong.'; |
||
148 | |||
149 | if ($dpl) { /* Use Spamassassin Plugin */ |
||
150 | if ( preg_match_all('/^X\-Spam\-Status:\s(?P<spamstatus>\w+)\,(?:\s+|\r\n\s+)score=(?P<score>[\-\.\d]+)(?:\s+|\r\n\s+)tagged_above=\-{0,1}\d+(?:\s+|\r\n\s+)required=(?P<th>[\-\.\d]+)(?:\s+|\r\n\s+)tests=\[(?:.|\r\n\s+)*DSPAM_(?P<dtype>SPAM|HAM)_(?P<dlevel>\d\d)(?:.|\r\n\s+)*\]/m',$header,$received) ) { |
||
151 | $result['spam']['status']=$received['spamstatus'][0]; |
||
152 | $result['spam']['score'] = $received['score'][0]; |
||
153 | $result['spam']['th'] = $received['th'][0]; |
||
154 | $result['dspam']['type'] = $received['dtype'][0]; |
||
155 | $result['dspam']['level'] =$received['dlevel'][0]; |
||
156 | } |
||
157 | if (count($received[0])>1) $result['warn'][] = 'The Spamassassin Headers are present more than once. I consider only the last one.'; |
||
158 | } |
||
159 | else { /* Parse apart all DSPAM Header and calculate a level */ |
||
160 | if ( preg_match_all('/^X\-Spam\-Status:\s(?P<spamstatus>\w+)\,(?:\s+|\r\n\s+)score=(?P<score>[\-\.\d]+)(?:\s+|\r\n\s+)tagged_above=\-{0,1}\d+(?:\s+|\r\n\s+)required=(?P<th>[\-\.\d]+)(?:\s+|\r\n\s+)tests=\[(?:.|\r\n\s+)*\]/m',$header,$received) ) { |
||
161 | $result['spam']['status']=$received['spamstatus'][0]; |
||
162 | $result['spam']['score'] = $received['score'][0]; |
||
163 | $result['spam']['th'] = $received['th'][0]; |
||
164 | if (count($received[0])>1) |
||
165 | $result['warn'][]= 'The Spamassassin Headers are present more than once. I consider only the last one.'; |
||
166 | } |
||
167 | View Code Duplication | if ( preg_match ('/\r\nX\-DSPAM\-Result:\s(?P<result>.*)\r\n/',$header,$received) != 1) |
|
168 | $result['warn'] = 'DSPAM Result invalid, not present or present more than once.'; |
||
169 | else |
||
170 | $result['dspam']['type']=$received['result']; |
||
171 | $prob = NULL; |
||
172 | $conf = NULL; |
||
173 | View Code Duplication | if ( preg_match ('/\r\nX\-DSPAM\-Probability:\s(?P<prob>.*)\r\n/',$header,$received) != 1) |
|
174 | $result['warn'][] = 'DSPAM Probability invalid, not present or present more than once.'; |
||
175 | else |
||
176 | $prob = $received['prob']; |
||
177 | View Code Duplication | if ( preg_match ('/\r\nX\-DSPAM\-Confidence:\s(?P<conf>.*)\r\n/',$header,$received) != 1) |
|
178 | $result['warn'][] = 'DSPAM Confidence invalid, not present or present more than once.'; |
||
179 | else |
||
180 | $conf = $received['conf']; |
||
181 | $result['dspam']['level'] = dspamLevel($prob,$conf); |
||
182 | } |
||
183 | $received=NULL; |
||
184 | View Code Duplication | if ( preg_match ('/\r\nFrom:\s(?P<from>.*)\r\n/',$header,$received) != 1) |
|
185 | $result['warn'][] = 'From header invalid or not present'; |
||
186 | else |
||
187 | $result['from'] = $received['from']; |
||
188 | |||
189 | View Code Duplication | if ( preg_match ('/\r\nDate:\s(?P<date>.*)\r\n/',$header,$received) != 1) |
|
190 | $result['warn'][] = 'Date header invalid or not present'; |
||
191 | else |
||
192 | $result['date'] = $received['date']; |
||
193 | |||
194 | $received=NULL; |
||
195 | View Code Duplication | if ( preg_match ('/\r\nMessage\-I(?:D|d):\s(?P<mid>.*)\r\n/',$header,$received) != 1) |
|
196 | $result['warn'][] = 'Message-ID invalid, not present or present more than once.'; |
||
197 | else |
||
198 | $result['messageid']=$received['mid']; |
||
199 | |||
200 | $received=NULL; |
||
201 | |||
202 | switch ($learn) { |
||
203 | case 'dspamc': |
||
204 | View Code Duplication | if ( preg_match ('/\r\nX\-DSPAM\-Signature:\s(?P<sig>.*)\r\n/',$header,$received) != 1) |
|
205 | $result['warn'] = 'DSPAM Signature invalid, not present or present more than once.'; |
||
206 | else |
||
207 | $result['dspam']['learn']=$received['sig']; |
||
208 | break; |
||
209 | case false: |
||
210 | break; |
||
211 | default: |
||
212 | syslog (LOG_INFO,$user.': Error in "learn" imap configuration value. Please, set "dspamc" or "false".'); |
||
213 | } |
||
214 | |||
215 | return $result; |
||
216 | } |
||
217 | |||
345 |
An exit expression should only be used in rare cases. For example, if you write a short command line script.
In most cases however, using an
exit
expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.