| Conditions | 40 |
| Paths | 3550 |
| Total Lines | 225 |
| Code Lines | 97 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 6 | ||
| Bugs | 1 | Features | 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 |
||
| 72 | public function __construct($Path = false, $RawData = false, array $Options = null) |
||
| 73 | { |
||
| 74 | // Checking preconditions for the parser. |
||
| 75 | // If path is given, the file should be accessible. |
||
| 76 | // If raw data is given, it is taken as it is. |
||
| 77 | // In both cases the real content is put in $this -> RawData |
||
| 78 | if ($Path) |
||
| 79 | { |
||
| 80 | if (!is_readable($Path)) |
||
| 81 | { |
||
| 82 | throw new Exception('vCard: Path not accessible ('.$Path.')'); |
||
| 83 | } |
||
| 84 | |||
| 85 | $this -> Path = $Path; |
||
|
|
|||
| 86 | $this -> RawData = file_get_contents($this -> Path); |
||
| 87 | } |
||
| 88 | elseif ($RawData) |
||
| 89 | { |
||
| 90 | $this -> RawData = $RawData; |
||
| 91 | } |
||
| 92 | else |
||
| 93 | { |
||
| 94 | //throw new Exception('vCard: No content provided'); |
||
| 95 | // Not necessary anymore as possibility to create vCards is added |
||
| 96 | } |
||
| 97 | |||
| 98 | if (!$this -> Path && !$this -> RawData) |
||
| 99 | { |
||
| 100 | return true; |
||
| 101 | } |
||
| 102 | |||
| 103 | if ($Options) |
||
| 104 | { |
||
| 105 | $this -> Options = array_merge($this -> Options, $Options); |
||
| 106 | } |
||
| 107 | |||
| 108 | // Counting the begin/end separators. If there aren't any or the count doesn't match, there is a problem with the file. |
||
| 109 | // If there is only one, this is a single vCard, if more, multiple vCards are combined. |
||
| 110 | $Matches = array(); |
||
| 111 | $vCardBeginCount = preg_match_all('{^BEGIN\:VCARD}miS', $this -> RawData, $Matches); |
||
| 112 | $vCardEndCount = preg_match_all('{^END\:VCARD}miS', $this -> RawData, $Matches); |
||
| 113 | |||
| 114 | if (($vCardBeginCount != $vCardEndCount) || !$vCardBeginCount) |
||
| 115 | { |
||
| 116 | $this -> Mode = vCard::MODE_ERROR; |
||
| 117 | throw new Exception('vCard: invalid vCard'); |
||
| 118 | } |
||
| 119 | |||
| 120 | $this -> Mode = $vCardBeginCount == 1 ? vCard::MODE_SINGLE : vCard::MODE_MULTIPLE; |
||
| 121 | |||
| 122 | // Removing/changing inappropriate newlines, i.e., all CRs or multiple newlines are changed to a single newline |
||
| 123 | $this -> RawData = str_replace("\r", "\n", $this -> RawData); |
||
| 124 | $this -> RawData = preg_replace('{(\n+)}', "\n", $this -> RawData); |
||
| 125 | |||
| 126 | // In multiple card mode the raw text is split at card beginning markers and each |
||
| 127 | // fragment is parsed in a separate vCard object. |
||
| 128 | if ($this -> Mode == self::MODE_MULTIPLE) |
||
| 129 | { |
||
| 130 | $this -> RawData = explode('BEGIN:VCARD', $this -> RawData); |
||
| 131 | $this -> RawData = array_filter($this -> RawData); |
||
| 132 | |||
| 133 | foreach ($this -> RawData as $SinglevCardRawData) |
||
| 134 | { |
||
| 135 | // Prepending "BEGIN:VCARD" to the raw string because we exploded on that one. |
||
| 136 | // If there won't be the BEGIN marker in the new object, it will fail. |
||
| 137 | $SinglevCardRawData = 'BEGIN:VCARD'."\n".$SinglevCardRawData; |
||
| 138 | |||
| 139 | $ClassName = get_class($this); |
||
| 140 | $this -> Data[] = new $ClassName(false, $SinglevCardRawData); |
||
| 141 | } |
||
| 142 | } |
||
| 143 | else |
||
| 144 | { |
||
| 145 | // Protect the BASE64 final = sign (detected by the line beginning with whitespace), otherwise the next replace will get rid of it |
||
| 146 | $this -> RawData = preg_replace('{(\n\s.+)=(\n)}', '$1-base64=-$2', $this -> RawData); |
||
| 147 | |||
| 148 | // Joining multiple lines that are split with a hard wrap and indicated by an equals sign at the end of line |
||
| 149 | // (quoted-printable-encoded values in v2.1 vCards) |
||
| 150 | $this -> RawData = str_replace("=\n", '', $this -> RawData); |
||
| 151 | |||
| 152 | // Joining multiple lines that are split with a soft wrap (space or tab on the beginning of the next line |
||
| 153 | $this -> RawData = str_replace(array("\n ", "\n\t"), '-wrap-', $this -> RawData); |
||
| 154 | |||
| 155 | // Restoring the BASE64 final equals sign (see a few lines above) |
||
| 156 | $this -> RawData = str_replace("-base64=-\n", "=\n", $this -> RawData); |
||
| 157 | |||
| 158 | $Lines = explode("\n", $this -> RawData); |
||
| 159 | |||
| 160 | foreach ($Lines as $Line) |
||
| 161 | { |
||
| 162 | // Lines without colons are skipped because, most likely, they contain no data. |
||
| 163 | if (strpos($Line, ':') === false) |
||
| 164 | { |
||
| 165 | continue; |
||
| 166 | } |
||
| 167 | |||
| 168 | // Each line is split into two parts. The key contains the element name and additional parameters, if present, |
||
| 169 | // value is just the value |
||
| 170 | list($Key, $Value) = explode(':', $Line, 2); |
||
| 171 | |||
| 172 | // Key is transformed to lowercase because, even though the element and parameter names are written in uppercase, |
||
| 173 | // it is quite possible that they will be in lower- or mixed case. |
||
| 174 | // The key is trimmed to allow for non-significant WSP characters as allowed by v2.1 |
||
| 175 | $Key = strtolower(trim(self::Unescape($Key))); |
||
| 176 | |||
| 177 | // These two lines can be skipped as they aren't necessary at all. |
||
| 178 | if ($Key == 'begin' || $Key == 'end') |
||
| 179 | { |
||
| 180 | continue; |
||
| 181 | } |
||
| 182 | |||
| 183 | if ((strpos($Key, 'agent') === 0) && (stripos($Value, 'begin:vcard') !== false)) |
||
| 184 | { |
||
| 185 | $ClassName = get_class($this); |
||
| 186 | $Value = new $ClassName(false, str_replace('-wrap-', "\n", $Value)); |
||
| 187 | if (!isset($this -> Data[$Key])) |
||
| 188 | { |
||
| 189 | $this -> Data[$Key] = array(); |
||
| 190 | } |
||
| 191 | $this -> Data[$Key][] = $Value; |
||
| 192 | continue; |
||
| 193 | } |
||
| 194 | else |
||
| 195 | { |
||
| 196 | $Value = str_replace('-wrap-', '', $Value); |
||
| 197 | } |
||
| 198 | |||
| 199 | $Value = trim(self::Unescape($Value)); |
||
| 200 | $Type = array(); |
||
| 201 | |||
| 202 | // Here additional parameters are parsed |
||
| 203 | $KeyParts = explode(';', $Key); |
||
| 204 | $Key = $KeyParts[0]; |
||
| 205 | $Encoding = false; |
||
| 206 | |||
| 207 | if (strpos($Key, 'item') === 0) |
||
| 208 | { |
||
| 209 | $TmpKey = explode('.', $Key, 2); |
||
| 210 | $Key = $TmpKey[1]; |
||
| 211 | $ItemIndex = (int)str_ireplace('item', '', $TmpKey[0]); |
||
| 212 | } |
||
| 213 | |||
| 214 | if (count($KeyParts) > 1) |
||
| 215 | { |
||
| 216 | $Parameters = self::ParseParameters($Key, array_slice($KeyParts, 1)); |
||
| 217 | |||
| 218 | foreach ($Parameters as $ParamKey => $ParamValue) |
||
| 219 | { |
||
| 220 | switch ($ParamKey) |
||
| 221 | { |
||
| 222 | case 'encoding': |
||
| 223 | $Encoding = $ParamValue; |
||
| 224 | if (in_array($ParamValue, array('b', 'base64'))) |
||
| 225 | { |
||
| 226 | //$Value = base64_decode($Value); |
||
| 227 | } |
||
| 228 | elseif ($ParamValue == 'quoted-printable') // v2.1 |
||
| 229 | { |
||
| 230 | $Value = quoted_printable_decode($Value); |
||
| 231 | } |
||
| 232 | break; |
||
| 233 | case 'charset': // v2.1 |
||
| 234 | if ($ParamValue != 'utf-8' && $ParamValue != 'utf8') |
||
| 235 | { |
||
| 236 | $Value = mb_convert_encoding($Value, 'UTF-8', $ParamValue); |
||
| 237 | } |
||
| 238 | break; |
||
| 239 | case 'type': |
||
| 240 | $Type = $ParamValue; |
||
| 241 | break; |
||
| 242 | } |
||
| 243 | } |
||
| 244 | } |
||
| 245 | |||
| 246 | // Checking files for colon-separated additional parameters (Apple's Address Book does this), for example, "X-ABCROP-RECTANGLE" for photos |
||
| 247 | if (in_array($Key, self::$Spec_FileElements) && isset($Parameters['encoding']) && in_array($Parameters['encoding'], array('b', 'base64'))) |
||
| 248 | { |
||
| 249 | // If colon is present in the value, it must contain Address Book parameters |
||
| 250 | // (colon is an invalid character for base64 so it shouldn't appear in valid files) |
||
| 251 | if (strpos($Value, ':') !== false) |
||
| 252 | { |
||
| 253 | $Value = explode(':', $Value); |
||
| 254 | $Value = array_pop($Value); |
||
| 255 | } |
||
| 256 | } |
||
| 257 | |||
| 258 | // Values are parsed according to their type |
||
| 259 | if (isset(self::$Spec_StructuredElements[$Key])) |
||
| 260 | { |
||
| 261 | $Value = self::ParseStructuredValue($Value, $Key); |
||
| 262 | if ($Type) |
||
| 263 | { |
||
| 264 | $Value['type'] = $Type; |
||
| 265 | } |
||
| 266 | } |
||
| 267 | else |
||
| 268 | { |
||
| 269 | if (in_array($Key, self::$Spec_MultipleValueElements)) |
||
| 270 | { |
||
| 271 | $Value = self::ParseMultipleTextValue($Value, $Key); |
||
| 272 | } |
||
| 273 | |||
| 274 | if ($Type) |
||
| 275 | { |
||
| 276 | $Value = array( |
||
| 277 | 'value' => $Value, |
||
| 278 | 'type' => $Type |
||
| 279 | ); |
||
| 280 | } |
||
| 281 | } |
||
| 282 | |||
| 283 | if (is_array($Value) && $Encoding) |
||
| 284 | { |
||
| 285 | $Value['encoding'] = $Encoding; |
||
| 286 | } |
||
| 287 | |||
| 288 | if (!isset($this -> Data[$Key])) |
||
| 289 | { |
||
| 290 | $this -> Data[$Key] = array(); |
||
| 291 | } |
||
| 292 | |||
| 293 | $this -> Data[$Key][] = $Value; |
||
| 294 | } |
||
| 295 | } |
||
| 296 | } |
||
| 297 | |||
| 693 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.