Conditions | 33 |
Paths | 193 |
Total Lines | 117 |
Code Lines | 91 |
Lines | 21 |
Ratio | 17.95 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
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 |
||
98 | protected function parse() |
||
99 | { |
||
100 | // Normalize new lines. |
||
101 | $this->content = str_replace(array("\r\n", "\r"), "\n", $this->content); |
||
102 | |||
103 | // RFC2425 5.8.1. Line delimiting and folding |
||
104 | // Unfolding is accomplished by regarding CRLF immediately followed by |
||
105 | // a white space character (namely HTAB ASCII decimal 9 or. SPACE ASCII |
||
106 | // decimal 32) as equivalent to no characters at all (i.e., the CRLF |
||
107 | // and single white space character are removed). |
||
108 | $this->content = preg_replace("/\n(?:[ \t])/", "", $this->content); |
||
109 | $lines = explode("\n", $this->content); |
||
110 | |||
111 | // Parse the VCard, line by line. |
||
112 | foreach ($lines as $line) { |
||
113 | $line = trim($line); |
||
114 | |||
115 | if (strtoupper($line) == "BEGIN:VCARD") { |
||
116 | $cardData = new \stdClass(); |
||
117 | } elseif (strtoupper($line) == "END:VCARD") { |
||
118 | $this->vcardObjects[] = $cardData; |
||
|
|||
119 | } elseif (!empty($line)) { |
||
120 | $type = ''; |
||
121 | $value = ''; |
||
122 | @list($type, $value) = explode(':', $line, 2); |
||
123 | |||
124 | $types = explode(';', $type); |
||
125 | $element = strtoupper($types[0]); |
||
126 | |||
127 | array_shift($types); |
||
128 | $i = 0; |
||
129 | $rawValue = false; |
||
130 | foreach ($types as $type) { |
||
131 | if (preg_match('/base64/', strtolower($type))) { |
||
132 | $value = base64_decode($value); |
||
133 | unset($types[$i]); |
||
134 | $rawValue = true; |
||
135 | } elseif (preg_match('/encoding=b/', strtolower($type))) { |
||
136 | $value = base64_decode($value); |
||
137 | unset($types[$i]); |
||
138 | $rawValue = true; |
||
139 | } elseif (preg_match('/quoted-printable/', strtolower($type))) { |
||
140 | $value = quoted_printable_decode($value); |
||
141 | unset($types[$i]); |
||
142 | $rawValue = true; |
||
143 | } elseif (strpos(strtolower($type), 'charset=') === 0) { |
||
144 | try { |
||
145 | $value = mb_convert_encoding($value, "UTF-8", substr($type, 8)); |
||
146 | } catch (\Exception $e) { } |
||
147 | unset($types[$i]); |
||
148 | } |
||
149 | $i++; |
||
150 | } |
||
151 | |||
152 | switch (strtoupper($element)) { |
||
153 | case 'FN': |
||
154 | $cardData->fullname = $value; |
||
155 | break; |
||
156 | case 'N': |
||
157 | foreach($this->parseName($value) as $key => $val) { |
||
158 | $cardData->{$key} = $val; |
||
159 | } |
||
160 | break; |
||
161 | case 'BDAY': |
||
162 | $cardData->birthday = $this->parseBirthday($value); |
||
163 | break; |
||
164 | case 'ADR': |
||
165 | if (!isset($cardData->address)) { |
||
166 | $cardData->address = array(); |
||
167 | } |
||
168 | $key = !empty($types) ? implode(';', $types) : 'WORK;POSTAL'; |
||
169 | $cardData->address[$key][] = $this->parseAddress($value); |
||
170 | break; |
||
171 | View Code Duplication | case 'TEL': |
|
172 | if (!isset($cardData->phone)) { |
||
173 | $cardData->phone = array(); |
||
174 | } |
||
175 | $key = !empty($types) ? implode(';', $types) : 'default'; |
||
176 | $cardData->phone[$key][] = $value; |
||
177 | break; |
||
178 | View Code Duplication | case 'EMAIL': |
|
179 | if (!isset($cardData->email)) { |
||
180 | $cardData->email = array(); |
||
181 | } |
||
182 | $key = !empty($types) ? implode(';', $types) : 'default'; |
||
183 | $cardData->email[$key][] = $value; |
||
184 | break; |
||
185 | case 'REV': |
||
186 | $cardData->revision = $value; |
||
187 | break; |
||
188 | case 'VERSION': |
||
189 | $cardData->version = $value; |
||
190 | break; |
||
191 | case 'ORG': |
||
192 | $cardData->organization = $value; |
||
193 | break; |
||
194 | View Code Duplication | case 'URL': |
|
195 | if (!isset($cardData->url)) { |
||
196 | $cardData->url = array(); |
||
197 | } |
||
198 | $key = !empty($types) ? implode(';', $types) : 'default'; |
||
199 | $cardData->url[$key][] = $value; |
||
200 | break; |
||
201 | case 'TITLE': |
||
202 | $cardData->title = $value; |
||
203 | break; |
||
204 | case 'PHOTO': |
||
205 | if ($rawValue) { |
||
206 | $cardData->rawPhoto = $value; |
||
207 | } else { |
||
208 | $cardData->photo = $value; |
||
209 | } |
||
210 | break; |
||
211 | } |
||
212 | } |
||
213 | } |
||
214 | } |
||
215 | |||
262 |
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: