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:
| 1 | <?php |
||
| 30 | abstract class ImportConnector { |
||
| 31 | |||
| 32 | // XML Configuration, class SimpleXml format |
||
| 33 | protected $configContent; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @param \SimpleXMLElement $xml_config |
||
| 37 | */ |
||
| 38 | public function __construct($xml_config = null) { |
||
| 39 | if ($xml_config != null) { |
||
| 40 | $this->setConfig($xml_config); |
||
| 41 | } |
||
| 42 | } |
||
| 43 | |||
| 44 | // returns a table containing converted elements from the input file |
||
| 45 | abstract function getElementsFromInput($input, $limit=-1); |
||
| 46 | |||
| 47 | // returns a single converted element |
||
| 48 | abstract function convertElementToVCard($element); |
||
| 49 | |||
| 50 | // returns the probability that the file matchs the current format |
||
| 51 | abstract function getFormatMatch($file); |
||
| 52 | |||
| 53 | public function setConfig($xml_config) { |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @brief updates a property given in parameter with the value and using the importEntry to set the different parameters |
||
| 59 | * @param $property the property to update |
||
| 60 | * @param $importEntry the entry configuration to update in SimpleXml format |
||
| 61 | * @value the value to update |
||
| 62 | */ |
||
| 63 | protected function updateProperty(&$property, $importEntry, $value) { |
||
| 64 | if (isset($property) && isset($importEntry) && isset($value)) { |
||
| 65 | if (isset($importEntry->vcard_entry)) { |
||
| 66 | if (isset($importEntry->vcard_entry['type'])) { |
||
| 67 | $property->add('TYPE', StringUtil::convertToUTF8($importEntry->vcard_entry['type'])); |
||
| 68 | } |
||
| 69 | if (isset($importEntry->vcard_entry->additional_property)) { |
||
| 70 | foreach ($importEntry->vcard_entry->additional_property as $additionalProperty) { |
||
| 71 | $property->add($additionalProperty['name'], $additionalProperty['value']); |
||
| 72 | } |
||
| 73 | } |
||
| 74 | if (isset($importEntry->vcard_entry['prefix'])) { |
||
| 75 | $value = $importEntry->vcard_entry['prefix'].$value; |
||
| 76 | } |
||
| 77 | if (isset($importEntry->vcard_entry['group'])) { |
||
| 78 | $property->group = $importEntry->vcard_entry['group']; |
||
| 79 | } |
||
| 80 | if (isset($importEntry->vcard_entry['position'])) { |
||
| 81 | $separator=";"; |
||
| 82 | if (isset($importEntry->vcard_entry['separator'])) { |
||
| 83 | $separator=$importEntry->vcard_entry['separator']; |
||
| 84 | } |
||
| 85 | $position = $importEntry->vcard_entry['position']; |
||
| 86 | $vArray = $property->getParts(); |
||
| 87 | $vArray[intval($position)] = StringUtil::convertToUTF8($value); |
||
| 88 | $property->setParts($vArray); |
||
| 89 | } else { |
||
| 90 | if (isset($importEntry->vcard_entry['value'])) { |
||
| 91 | $property->add('TYPE', StringUtil::convertToUTF8($value)); |
||
| 92 | } else { |
||
| 93 | $curVal = $property->getParts(); |
||
| 94 | $curVal[] = StringUtil::convertToUTF8($value); |
||
| 95 | $property->setValue($curVal); |
||
| 96 | } |
||
| 97 | } |
||
| 98 | } |
||
| 99 | if (isset($importEntry->vcard_parameter)) { |
||
| 100 | $property->add($importEntry->vcard_parameter['parameter'], StringUtil::convertToUTF8($value)); |
||
| 101 | } |
||
| 102 | } |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @brief modifies a vcard property array with the image |
||
| 107 | */ |
||
| 108 | public function updateImageProperty(&$property, $entry, $version=null) { |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @brief returns the vcard property corresponding to the parameter |
||
| 124 | * creates the property if it doesn't exists yet |
||
| 125 | * @param $vcard the vcard to get or create the properties with |
||
| 126 | * @param $importEntry the parameter to find |
||
| 127 | * @return the property|false |
||
| 128 | */ |
||
| 129 | protected function getOrCreateVCardProperty(&$vcard, $importEntry) { |
||
| 184 | } |
||
| 185 | |||
| 187 |