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 importexport_definition 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 importexport_definition, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class importexport_definition implements importexport_iface_egw_record { |
||
| 24 | |||
| 25 | const _appname = 'importexport'; |
||
|
|
|||
| 26 | const _defintion_talbe = 'egw_importexport_definitions'; |
||
| 27 | |||
| 28 | private $attributes = array( |
||
| 29 | 'definition_id' => 'string', |
||
| 30 | 'name' => 'string', |
||
| 31 | 'application' => 'string', |
||
| 32 | 'plugin' => 'string', |
||
| 33 | 'type' => 'string', |
||
| 34 | 'allowed_users' => 'array', |
||
| 35 | 'plugin_options' => 'array', |
||
| 36 | 'filter' => 'array', |
||
| 37 | 'owner' => 'int', |
||
| 38 | 'description' => 'string', |
||
| 39 | 'modified' => 'timestamp' |
||
| 40 | ); |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var so_sql holds Api\Storage\Base object |
||
| 44 | */ |
||
| 45 | private $so_sql; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var array internal representation of definition |
||
| 49 | */ |
||
| 50 | private $definition = array(); |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var int holds current user |
||
| 54 | */ |
||
| 55 | private $user; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var bool is current user an admin? |
||
| 59 | */ |
||
| 60 | private $is_admin; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * constructor |
||
| 64 | * reads record from backend if identifier is given. |
||
| 65 | * |
||
| 66 | * @param string $_identifier |
||
| 67 | */ |
||
| 68 | public function __construct( $_identifier='' ) { |
||
| 69 | $this->so_sql = new Api\Storage\Base(self::_appname ,self::_defintion_talbe); |
||
| 70 | $this->user = $GLOBALS['egw_info']['user']['user_id']; |
||
| 71 | $this->is_admin = $GLOBALS['egw_info']['user']['apps']['admin'] || $GLOBALS['egw_setup'] ? true : false; |
||
| 72 | // compability to string identifiers |
||
| 73 | if (!is_numeric($_identifier) && strlen($_identifier) > 3) $_identifier = $this->name2identifier($_identifier); |
||
| 74 | |||
| 75 | if ((int)$_identifier != 0) { |
||
| 76 | $this->definition = $this->so_sql->read(array('definition_id' => $_identifier)); |
||
| 77 | if ( empty( $this->definition ) ) { |
||
| 78 | throw new Exception('Error: No such definition with identifier :"'.$_identifier.'"!'); |
||
| 79 | } |
||
| 80 | if ( !( importexport_definitions_bo::is_permitted($this->get_record_array()) || $this->is_admin)) { |
||
| 81 | throw new Exception('Error: User "'.$this->user.'" is not permitted to get definition with identifier "'.$_identifier.'"!'); |
||
| 82 | } |
||
| 83 | try |
||
| 84 | { |
||
| 85 | $options_data = importexport_arrayxml::xml2array( $this->definition['plugin_options'] ); |
||
| 86 | $this->definition['plugin_options'] = (array)$options_data['root']; |
||
| 87 | if($this->definition['filter']) $filter = importexport_arrayxml::xml2array( $this->definition['filter'] ); |
||
| 88 | $this->definition['filter'] = $filter['root']; |
||
| 89 | } |
||
| 90 | catch (Exception $e) |
||
| 91 | { |
||
| 92 | error_log($e->getMessage()); |
||
| 93 | } |
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * compability function for string identifiers e.g. used by cli |
||
| 99 | * |
||
| 100 | * @param string $_name |
||
| 101 | * @return int |
||
| 102 | */ |
||
| 103 | private function name2identifier( $_name ) { |
||
| 115 | |||
| 116 | public function __get($_attribute_name) { |
||
| 131 | |||
| 132 | public function __set($_attribute_name,$_data) { |
||
| 148 | |||
| 149 | /** |
||
| 150 | * gets users who are allowd to use this definition |
||
| 151 | * |
||
| 152 | * @return array |
||
| 153 | */ |
||
| 154 | private function get_allowed_users() { |
||
| 157 | |||
| 158 | /** |
||
| 159 | * sets allowed users |
||
| 160 | * |
||
| 161 | * @param array $_allowed_users |
||
| 162 | */ |
||
| 163 | private function set_allowed_users( $_allowed_users ) { |
||
| 166 | |||
| 167 | /** |
||
| 168 | * gets options |
||
| 169 | * |
||
| 170 | * @return array |
||
| 171 | */ |
||
| 172 | private function get_options() { |
||
| 175 | |||
| 176 | /** |
||
| 177 | * sets options |
||
| 178 | * |
||
| 179 | * @param array $options |
||
| 180 | */ |
||
| 181 | private function set_options(array $_plugin_options) { |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Get stored data filter |
||
| 194 | * |
||
| 195 | * @return array |
||
| 196 | */ |
||
| 197 | private function get_filter() { |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Set stored data filter |
||
| 203 | * |
||
| 204 | * @param filter array of field => settings |
||
| 205 | */ |
||
| 206 | private function set_filter(Array $filter) { |
||
| 209 | |||
| 210 | /** |
||
| 211 | * converts this object to array. |
||
| 212 | * @abstract We need such a function cause PHP5 |
||
| 213 | * dosn't allow objects do define it's own casts :-( |
||
| 214 | * once PHP can deal with object casts we will change to them! |
||
| 215 | * |
||
| 216 | * @return array complete record as associative array |
||
| 217 | */ |
||
| 218 | public function get_record_array() { |
||
| 225 | |||
| 226 | /** |
||
| 227 | * gets title of record |
||
| 228 | * |
||
| 229 | *@return string tiltle |
||
| 230 | */ |
||
| 231 | public function get_title() { |
||
| 234 | |||
| 235 | /** |
||
| 236 | * sets complete record from associative array |
||
| 237 | * |
||
| 238 | * @return void |
||
| 239 | */ |
||
| 240 | public function set_record( array $_record ) { |
||
| 258 | |||
| 259 | /** |
||
| 260 | * gets identifier of this record |
||
| 261 | * |
||
| 262 | * @return int identifier of this record |
||
| 263 | */ |
||
| 264 | public function get_identifier() { |
||
| 267 | |||
| 268 | |||
| 269 | /** |
||
| 270 | * Gets the URL icon representitive of the record |
||
| 271 | * This could be as general as the application icon, or as specific as a contact photo |
||
| 272 | * |
||
| 273 | * @return string Full URL of an icon, or appname/icon_name |
||
| 274 | */ |
||
| 275 | public function get_icon() { |
||
| 278 | |||
| 279 | /** |
||
| 280 | * saves record into backend |
||
| 281 | * |
||
| 282 | * @return string identifier |
||
| 283 | */ |
||
| 284 | public function save ( $_dst_identifier ) { |
||
| 299 | |||
| 300 | /** |
||
| 301 | * copys current record to record identified by $_dst_identifier |
||
| 302 | * |
||
| 303 | * @param string $_dst_identifier |
||
| 304 | * @return string dst_identifier |
||
| 305 | */ |
||
| 306 | public function copy ( $_dst_identifier ) { |
||
| 319 | |||
| 320 | /** |
||
| 321 | * moves current record to record identified by $_dst_identifier |
||
| 322 | * $this will become moved record |
||
| 323 | * |
||
| 324 | * @param string $_dst_identifier |
||
| 325 | * @return string dst_identifier |
||
| 326 | */ |
||
| 327 | public function move ( $_dst_identifier ) { |
||
| 343 | |||
| 344 | /** |
||
| 345 | * delets current record from backend |
||
| 346 | * @return void |
||
| 347 | * |
||
| 348 | */ |
||
| 349 | public function delete () { |
||
| 357 | |||
| 358 | /** |
||
| 359 | * destructor |
||
| 360 | * |
||
| 361 | */ |
||
| 362 | public function __destruct() { |
||
| 365 | |||
| 366 | } |
||
| 367 |