Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like ImportLdifConnector often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ImportLdifConnector, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 32 | class ImportLdifConnector extends ImportConnector{ |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @brief separates elements from the input stream according to the entry_separator value in config |
||
| 36 | * ignoring the first line if mentionned in the config |
||
| 37 | * @param $input the input file to import |
||
| 38 | * @param $limit the number of elements to return (-1 = no limit) |
||
| 39 | * @return array of strings |
||
| 40 | */ |
||
| 41 | public function getElementsFromInput($file, $limit=-1) { |
||
| 42 | |||
| 43 | $parts = $this->getSourceElementsFromFile($file, $limit); |
||
| 44 | |||
| 45 | $elements = array(); |
||
| 46 | foreach($parts as $part) |
||
| 47 | { |
||
| 48 | $elements[] = $this->convertElementToVCard($part); |
||
| 49 | } |
||
| 50 | |||
| 51 | return array_values($elements); |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @brief parses the file in vcard format |
||
| 56 | * @param $file the input file to import |
||
| 57 | * @param $limit the number of elements to return (-1 = no limit) |
||
| 58 | * @return array() |
||
| 59 | */ |
||
| 60 | private function getSourceElementsFromFile($file, $limit=-1) { |
||
| 61 | $file = StringUtil::convertToUTF8(file_get_contents($file)); |
||
| 62 | |||
| 63 | $nl = "\n"; |
||
| 64 | $replaceFrom = array("\r","\n "); |
||
| 65 | $replaceTo = array("\n",""); |
||
| 66 | View Code Duplication | foreach ($this->configContent->import_core->replace as $replace) { |
|
| 67 | if (isset($replace['from']) && isset($replace['to'])) { |
||
| 68 | $replaceFrom[] = $replace['from']; |
||
| 69 | $replaceTo[] = $replace['to']; |
||
| 70 | } |
||
| 71 | } |
||
| 72 | |||
| 73 | $file = str_replace($replaceFrom, $replaceTo, $file); |
||
| 74 | |||
| 75 | $lines = explode($nl, $file); |
||
| 76 | $parts = array(); |
||
| 77 | $card = array(); |
||
| 78 | $numParts = 0; |
||
| 79 | foreach($lines as $line) { |
||
| 80 | if (!preg_match("/^#/", $line)) { // Ignore comment line |
||
| 81 | if(preg_match("/^\w+:: /",$line)) { |
||
| 82 | $kv = explode(':: ', $line, 2); |
||
| 83 | $key = $kv[0]; |
||
| 84 | $value = base64_decode($kv[1], true); |
||
| 85 | } else { |
||
| 86 | $kv = explode(': ', $line, 2); |
||
| 87 | $key = $kv[0]; |
||
| 88 | if(count($kv) == 2) { |
||
| 89 | $value = $kv[1]; |
||
| 90 | } else { |
||
| 91 | $value = ""; |
||
| 92 | } |
||
| 93 | } |
||
| 94 | if ($key == "dn") { |
||
| 95 | if (count($card) > 0) { |
||
| 96 | $numParts++; |
||
| 97 | if ($limit > -1 && count($card) == $limit) { |
||
| 98 | break; |
||
| 99 | } |
||
| 100 | $parts[] = $card; |
||
| 101 | } |
||
| 102 | $card = array(array($key, $value)); |
||
| 103 | } else if ($key != "" && $key != "version" && $value != "") { |
||
| 104 | $card[] = array($key, $value); |
||
| 105 | } |
||
| 106 | } |
||
| 107 | } |
||
| 108 | if ($numParts <= $limit && count($card) > 0) { |
||
| 109 | $parts[] = $card; |
||
| 110 | } |
||
| 111 | return $parts; |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @brief converts a ldif into a owncloud VCard |
||
| 116 | * @param $element the VCard element to convert |
||
| 117 | * @return VCard |
||
| 118 | */ |
||
| 119 | public function convertElementToVCard($element) { |
||
| 120 | $dest = new \OCA\Contacts\VObject\VCard(); |
||
| 121 | |||
| 122 | foreach ($element as $ldifProperty) { |
||
| 123 | $importEntry = $this->getImportEntry($ldifProperty[0]); |
||
| 124 | if ($importEntry) { |
||
| 125 | $value = $ldifProperty[1]; |
||
| 126 | if (isset($importEntry['remove'])) { |
||
| 127 | $value = str_replace($importEntry['remove'], '', $ldifProperty[1]); |
||
| 128 | } |
||
| 129 | $values = array($value); |
||
| 130 | if (isset($importEntry['separator'])) { |
||
| 131 | $values = explode($importEntry['separator'], $value); |
||
| 132 | } |
||
| 133 | |||
| 134 | foreach ($values as $oneValue) { |
||
| 135 | $this->convertElementToProperty($oneValue, $importEntry, $dest); |
||
| 136 | } |
||
| 137 | } else { |
||
| 138 | $property = $dest->createProperty("X-Unknown-Element", ''.StringUtil::convertToUTF8($ldifProperty[1])); |
||
| 139 | $property->add('TYPE', StringUtil::convertToUTF8($ldifProperty[0])); |
||
| 140 | $dest->add($property); |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | $dest->validate(\Sabre\VObject\Component\VCard::REPAIR); |
||
| 145 | return $dest; |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @brief converts an LDIF element into a VCard property |
||
| 150 | * and updates the VCard |
||
| 151 | * @param $value the LDIF value |
||
| 152 | * @param $importEntry the VCard entry to modify |
||
| 153 | * @param $dest the VCard to modify (for adding a X-FAVOURITE property) |
||
| 154 | */ |
||
| 155 | private function convertElementToProperty($value, $importEntry, &$dest) { |
||
| 156 | if (isset($importEntry->vcard_favourites)) { |
||
| 157 | foreach ($importEntry->vcard_favourites as $vcardFavourite) { |
||
| 158 | if (strcasecmp((string)$vcardFavourite, trim($value)) == 0) { |
||
| 159 | $property = $dest->createProperty("X-FAVOURITES", 'yes'); |
||
| 160 | $dest->add($property); |
||
| 161 | View Code Duplication | } else { |
|
| 162 | $property = $this->getOrCreateVCardProperty($dest, $importEntry->vcard_entry); |
||
| 163 | if (isset($importEntry['image']) && $importEntry['image'] == "true") { |
||
| 164 | $this->updateImageProperty($property, $value); |
||
| 165 | } else { |
||
| 166 | $this->updateProperty($property, $importEntry, $value); |
||
| 167 | } |
||
| 168 | } |
||
| 169 | } |
||
| 170 | View Code Duplication | } else { |
|
| 171 | $property = $this->getOrCreateVCardProperty($dest, $importEntry->vcard_entry); |
||
| 172 | if (isset($importEntry['image']) && $importEntry['image'] == "true") { |
||
| 173 | $this->updateImageProperty($property, $value); |
||
| 174 | } else { |
||
| 175 | $this->updateProperty($property, $importEntry, $value); |
||
| 176 | } |
||
| 177 | } |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @brief tests if the property has to be translated by looking for its signature in the xml configuration |
||
| 182 | * @param $property Sabre VObject Property too look |
||
| 183 | * @param $vcard the parent Sabre VCard object to look for a |
||
| 184 | */ |
||
| 185 | private function getImportEntry($property) { |
||
| 186 | foreach ($this->configContent->import_entry as $importEntry) { |
||
| 187 | if ($importEntry['name'] == $property) { |
||
| 188 | return $importEntry; |
||
| 189 | } |
||
| 190 | } |
||
| 191 | return false; |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @brief returns the probability that the first element is a match for this format |
||
| 196 | * @param $file the file to examine |
||
| 197 | * @return 0 if not a valid ldif file |
||
| 198 | * 1 - 0.5*(number of untranslated elements/total number of elements) |
||
| 199 | * The more the first element has untranslated elements, the more the result is close to 0.5 |
||
| 200 | */ |
||
| 201 | View Code Duplication | public function getFormatMatch($file) { |
|
| 218 | } |
||
| 219 | |||
| 220 | ?> |
||
| 221 |