Conditions | 38 |
Paths | 438 |
Total Lines | 147 |
Code Lines | 108 |
Lines | 21 |
Ratio | 14.29 % |
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 |
||
145 | protected function parse() |
||
146 | { |
||
147 | // Normalize new lines. |
||
148 | $this->content = str_replace(array("\r\n", "\r"), "\n", $this->content); |
||
149 | |||
150 | // RFC2425 5.8.1. Line delimiting and folding |
||
151 | // Unfolding is accomplished by regarding CRLF immediately followed by |
||
152 | // a white space character (namely HTAB ASCII decimal 9 or. SPACE ASCII |
||
153 | // decimal 32) as equivalent to no characters at all (i.e., the CRLF |
||
154 | // and single white space character are removed). |
||
155 | $this->content = preg_replace("/\n(?:[ \t])/", "", $this->content); |
||
156 | $lines = explode("\n", $this->content); |
||
157 | |||
158 | // Parse the VCard, line by line. |
||
159 | foreach ($lines as $line) { |
||
160 | $line = trim($line); |
||
161 | |||
162 | if (strtoupper($line) == "BEGIN:VCARD") { |
||
163 | $cardData = new \stdClass(); |
||
164 | } elseif (strtoupper($line) == "END:VCARD") { |
||
165 | $this->vcardObjects[] = $cardData; |
||
|
|||
166 | } elseif (!empty($line)) { |
||
167 | // Strip grouping information. We don't use the group names. We |
||
168 | // simply use a list for entries that have multiple values. |
||
169 | // As per RFC, group names are alphanumerical, and end with a |
||
170 | // period (.). |
||
171 | $line = preg_replace('/^\w+\./', '', $line); |
||
172 | |||
173 | $type = ''; |
||
174 | $value = ''; |
||
175 | @list($type, $value) = explode(':', $line, 2); |
||
176 | |||
177 | $types = explode(';', $type); |
||
178 | $element = strtoupper($types[0]); |
||
179 | |||
180 | array_shift($types); |
||
181 | |||
182 | // Normalize types. A type can either be a type-param directly, |
||
183 | // or can be prefixed with "type=". E.g.: "INTERNET" or |
||
184 | // "type=INTERNET". |
||
185 | if (!empty($types)) { |
||
186 | $types = array_map(function($type) { |
||
187 | return preg_replace('/^type=/i', '', $type); |
||
188 | }, $types); |
||
189 | } |
||
190 | |||
191 | $i = 0; |
||
192 | $rawValue = false; |
||
193 | foreach ($types as $type) { |
||
194 | if (preg_match('/base64/', strtolower($type))) { |
||
195 | $value = base64_decode($value); |
||
196 | unset($types[$i]); |
||
197 | $rawValue = true; |
||
198 | } elseif (preg_match('/encoding=b/', strtolower($type))) { |
||
199 | $value = base64_decode($value); |
||
200 | unset($types[$i]); |
||
201 | $rawValue = true; |
||
202 | } elseif (preg_match('/quoted-printable/', strtolower($type))) { |
||
203 | $value = quoted_printable_decode($value); |
||
204 | unset($types[$i]); |
||
205 | $rawValue = true; |
||
206 | } elseif (strpos(strtolower($type), 'charset=') === 0) { |
||
207 | try { |
||
208 | $value = mb_convert_encoding($value, "UTF-8", substr($type, 8)); |
||
209 | } catch (\Exception $e) { |
||
210 | } |
||
211 | unset($types[$i]); |
||
212 | } |
||
213 | $i++; |
||
214 | } |
||
215 | |||
216 | switch (strtoupper($element)) { |
||
217 | case 'FN': |
||
218 | $cardData->fullname = $value; |
||
219 | break; |
||
220 | case 'N': |
||
221 | foreach ($this->parseName($value) as $key => $val) { |
||
222 | $cardData->{$key} = $val; |
||
223 | } |
||
224 | break; |
||
225 | case 'BDAY': |
||
226 | $cardData->birthday = $this->parseBirthday($value); |
||
227 | break; |
||
228 | case 'ADR': |
||
229 | if (!isset($cardData->address)) { |
||
230 | $cardData->address = array(); |
||
231 | } |
||
232 | $key = !empty($types) ? implode(';', $types) : 'WORK;POSTAL'; |
||
233 | $cardData->address[$key][] = $this->parseAddress($value); |
||
234 | break; |
||
235 | View Code Duplication | case 'TEL': |
|
236 | if (!isset($cardData->phone)) { |
||
237 | $cardData->phone = array(); |
||
238 | } |
||
239 | $key = !empty($types) ? implode(';', $types) : 'default'; |
||
240 | $cardData->phone[$key][] = $value; |
||
241 | break; |
||
242 | View Code Duplication | case 'EMAIL': |
|
243 | if (!isset($cardData->email)) { |
||
244 | $cardData->email = array(); |
||
245 | } |
||
246 | $key = !empty($types) ? implode(';', $types) : 'default'; |
||
247 | $cardData->email[$key][] = $value; |
||
248 | break; |
||
249 | case 'REV': |
||
250 | $cardData->revision = $value; |
||
251 | break; |
||
252 | case 'VERSION': |
||
253 | $cardData->version = $value; |
||
254 | break; |
||
255 | case 'ORG': |
||
256 | $cardData->organization = $value; |
||
257 | break; |
||
258 | View Code Duplication | case 'URL': |
|
259 | if (!isset($cardData->url)) { |
||
260 | $cardData->url = array(); |
||
261 | } |
||
262 | $key = !empty($types) ? implode(';', $types) : 'default'; |
||
263 | $cardData->url[$key][] = $value; |
||
264 | break; |
||
265 | case 'TITLE': |
||
266 | $cardData->title = $value; |
||
267 | break; |
||
268 | case 'PHOTO': |
||
269 | if ($rawValue) { |
||
270 | $cardData->rawPhoto = $value; |
||
271 | } else { |
||
272 | $cardData->photo = $value; |
||
273 | } |
||
274 | break; |
||
275 | case 'LOGO': |
||
276 | if ($rawValue) { |
||
277 | $cardData->rawLogo = $value; |
||
278 | } else { |
||
279 | $cardData->logo = $value; |
||
280 | } |
||
281 | break; |
||
282 | case 'NOTE': |
||
283 | $cardData->note = $this->unescape($value); |
||
284 | break; |
||
285 | case 'CATEGORIES': |
||
286 | $cardData->categories = array_map('trim', explode(',', $value)); |
||
287 | break; |
||
288 | } |
||
289 | } |
||
290 | } |
||
291 | } |
||
292 | |||
351 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: