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 Request 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 Request, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 87 | class Request |
||
|
|
|||
| 88 | { |
||
| 89 | /** |
||
| 90 | * here is the request data stored |
||
| 91 | * |
||
| 92 | * @var array |
||
| 93 | */ |
||
| 94 | protected $data=array(); |
||
| 95 | /** |
||
| 96 | * Flag if data has been modified and therefor need to be stored again in the session |
||
| 97 | * |
||
| 98 | * @var boolean |
||
| 99 | */ |
||
| 100 | protected $data_modified=false; |
||
| 101 | /** |
||
| 102 | * Flag that stored data should be removed by destructor, if not modified. |
||
| 103 | * |
||
| 104 | * @var boolean |
||
| 105 | */ |
||
| 106 | protected $remove_if_not_modified=false; |
||
| 107 | /** |
||
| 108 | * mcrypt resource |
||
| 109 | * |
||
| 110 | * @var resource |
||
| 111 | */ |
||
| 112 | static protected $mcrypt; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * See gzcompress, set it to 0 to not compress |
||
| 116 | * |
||
| 117 | * @var int |
||
| 118 | */ |
||
| 119 | static public $compression_level = 6; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Name of request class used |
||
| 123 | * |
||
| 124 | * Can be set here to force a certain class, otherwise the factory method chooses one |
||
| 125 | * |
||
| 126 | * @var string |
||
| 127 | */ |
||
| 128 | static public $request_class; // = 'etemplate_request_session'; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Factory method to get a new request object or the one for an existing request |
||
| 132 | * |
||
| 133 | * New default is to use egw_cache to store requests and no longer request or |
||
| 134 | * session documented below: |
||
| 135 | * |
||
| 136 | * If mcrypt AND gzcompress is available this factory method chooses etemplate_request, |
||
| 137 | * which stores the request data encrypted in a hidden var directly in the form, |
||
| 138 | * over etemplate_request_session, which stores the data in the session (and causing |
||
| 139 | * the sesison to constantly grow). |
||
| 140 | * |
||
| 141 | * @param string $id =null |
||
| 142 | * @return Request |
||
| 143 | */ |
||
| 144 | public static function read($id=null) |
||
| 145 | { |
||
| 146 | if (is_null(self::$request_class)) |
||
| 147 | { |
||
| 148 | // new default to use egw_cache to store requests |
||
| 149 | self::$request_class = __CLASS__.'\\Cache'; |
||
| 150 | /* old default to use request if mcrypt and gzcompress are available and session if not |
||
| 151 | self::$request_class = check_load_extension('mcrypt') && function_exists('gzcompress') && |
||
| 152 | self::init_crypt() ? __CLASS__ : 'etemplate_request_session'; |
||
| 153 | */ |
||
| 154 | } |
||
| 155 | if (self::$request_class != __CLASS__) |
||
| 156 | { |
||
| 157 | $request = call_user_func(self::$request_class.'::read', $id); |
||
| 158 | } |
||
| 159 | else |
||
| 160 | { |
||
| 161 | $request = new Request(); |
||
| 162 | |||
| 163 | if (!is_null($id)) |
||
| 164 | { |
||
| 165 | $id = base64_decode($id); |
||
| 166 | |||
| 167 | // decrypt the data if available |
||
| 168 | if (self::init_crypt()) |
||
| 169 | { |
||
| 170 | $id = mdecrypt_generic(self::$mcrypt,$id); |
||
| 171 | } |
||
| 172 | // uncompress the data if available |
||
| 173 | if (self::$compression_level && function_exists('gzcompress')) |
||
| 174 | { |
||
| 175 | //$len_compressed = bytes($id); |
||
| 176 | //$time = microtime(true); |
||
| 177 | $id = gzuncompress($id); |
||
| 178 | //$time = number_format(1000.0 * (microtime(true) - $time),1); |
||
| 179 | //$len_uncompressed = bytes($id); |
||
| 180 | //error_log(__METHOD__."() uncompressed from $len_compressed to $len_uncompressed bytes $time ms"); |
||
| 181 | } |
||
| 182 | $request->data = unserialize($id); |
||
| 183 | |||
| 184 | if (!$request->data) |
||
| 185 | { |
||
| 186 | error_log(__METHOD__."() id not valid!"); |
||
| 187 | $request = false; |
||
| 188 | } |
||
| 189 | //error_log(__METHOD__."() size of request = ".bytes($id)); |
||
| 190 | } |
||
| 191 | } |
||
| 192 | if (!$request) // eT2 request/session expired |
||
| 193 | { |
||
| 194 | list($app) = explode('.', $_GET['menuaction']); |
||
| 195 | $global = false; |
||
| 196 | if(isset($GLOBALS['egw_info']['apps'][$app])) |
||
| 197 | { |
||
| 198 | $index_url = isset($GLOBALS['egw_info']['apps'][$app]['index']) ? |
||
| 199 | '/index.php?menuaction='.$GLOBALS['egw_info']['apps'][$app]['index'] : '/'.$app.'/index.php'; |
||
| 200 | } |
||
| 201 | else |
||
| 202 | { |
||
| 203 | $index_url = Api\Framework::link('/index.php'); |
||
| 204 | $global = true; |
||
| 205 | $app = null; |
||
| 206 | } |
||
| 207 | // add a unique token to redirect to avoid client-side framework tries refreshing via nextmatch |
||
| 208 | $index_url .= (strpos($index_url, '?') ? '&' : '?').'redirect='.microtime(true); |
||
| 209 | error_log(__METHOD__."('$id', ...) eT2 request not found / expired --> redirecting app $app to $index_url (_GET[menuaction]=$_GET[menuaction], isJSONRequest()=".array2string(Api\Json\Request::isJSONRequest()).')'); |
||
| 210 | if (Api\Json\Request::isJSONRequest()) |
||
| 211 | { |
||
| 212 | // we must not redirect ajax_destroy_session calls, as they might originate from our own redirect! |
||
| 213 | if (strpos($_GET['menuaction'], '.ajax_destroy_session.etemplate') === false) |
||
| 214 | { |
||
| 215 | $response = Api\Json\Response::get(); |
||
| 216 | $response->redirect($index_url, $global, $app); |
||
| 217 | exit; |
||
| 218 | } |
||
| 219 | } |
||
| 220 | else |
||
| 221 | { |
||
| 222 | Api\Framework::redirect_link($index_url); |
||
| 223 | } |
||
| 224 | } |
||
| 225 | return $request; |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Private constructor to force the instancation of this class only via it's static factory method read |
||
| 230 | * |
||
| 231 | * @param string $id =null |
||
| 232 | */ |
||
| 233 | private function __construct($id=null) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * return the id of this request |
||
| 240 | * |
||
| 241 | * @return string |
||
| 242 | */ |
||
| 243 | public function &id() |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Clean up data before storing it: currently only removes "real" nextmatch rows |
||
| 272 | */ |
||
| 273 | protected function cleanup() |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Register a form-variable to be processed |
||
| 290 | * |
||
| 291 | * @param string $_form_name form-name |
||
| 292 | * @param string $type etemplate type |
||
| 293 | * @param array $data =array() optional extra data |
||
| 294 | */ |
||
| 295 | public function set_to_process($_form_name, $type, $data=array()) |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Set an attribute of a to-process record |
||
| 311 | * |
||
| 312 | * @param string $_form_name form-name |
||
| 313 | * @param string $attribute etemplate type |
||
| 314 | * @param array $value |
||
| 315 | * @param boolean $add_to_array =false should $value be added to the attribute array |
||
| 316 | */ |
||
| 317 | public function set_to_process_attribute($_form_name, $attribute, $value, $add_to_array=false) |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Unregister a form-variable to be no longer processed |
||
| 338 | * |
||
| 339 | * @param string $form_name form-name |
||
| 340 | */ |
||
| 341 | public function unset_to_process($form_name) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * return the data of a form-var to process or the whole array |
||
| 350 | * |
||
| 351 | * @param string $form_name =null |
||
| 352 | * @return array |
||
| 353 | */ |
||
| 354 | public function get_to_process($form_name=null) |
||
| 359 | |||
| 360 | /** |
||
| 361 | * check if something set for a given $form_name |
||
| 362 | * |
||
| 363 | * @param string $form_name |
||
| 364 | * @return boolean |
||
| 365 | */ |
||
| 366 | public function isset_to_process($form_name) |
||
| 371 | |||
| 372 | /** |
||
| 373 | * creates a new unique request-id |
||
| 374 | * |
||
| 375 | * @return string |
||
| 376 | */ |
||
| 377 | static function request_id() |
||
| 390 | |||
| 391 | /** |
||
| 392 | * magic function to set all request-vars, used eg. as $request->method = 'app.class.method'; |
||
| 393 | * |
||
| 394 | * @param string $var |
||
| 395 | * @param mixed $val |
||
| 396 | */ |
||
| 397 | public function __set($var,$val) |
||
| 406 | |||
| 407 | /** |
||
| 408 | * magic function to access the request-vars, used eg. as $method = $request->method; |
||
| 409 | * |
||
| 410 | * @param string $var |
||
| 411 | * @return mixed |
||
| 412 | */ |
||
| 413 | public function &__get($var) |
||
| 419 | |||
| 420 | |||
| 421 | /** |
||
| 422 | * magic function to see if a request-var has been set |
||
| 423 | * |
||
| 424 | * @param string $var |
||
| 425 | * @return boolean |
||
| 426 | */ |
||
| 427 | public function __isset($var) |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Get the names / keys of existing variables |
||
| 434 | * |
||
| 435 | * @return array |
||
| 436 | */ |
||
| 437 | public function names() |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Output the size-wise important parts of a request |
||
| 444 | * |
||
| 445 | * @param double $min_share minimum share to be reported (in percent of the whole request) |
||
| 446 | * @param double $dump_share minimum share from which on a variable get output |
||
| 447 | */ |
||
| 448 | public function debug($min_share=1.0,$dump_share=25.0) |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Check if session encryption is configured, possible and initialise it |
||
| 474 | * |
||
| 475 | * @param string $algo ='tripledes' |
||
| 476 | * @param string $mode ='ecb' |
||
| 477 | * @return boolean true if encryption is used, false otherwise |
||
| 478 | */ |
||
| 479 | static public function init_crypt($algo='tripledes',$mode='ecb') |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Destructor |
||
| 519 | */ |
||
| 520 | function __destruct() |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Mark request as to destroy, if it does not get modified before destructor is called |
||
| 531 | * |
||
| 532 | * If that function is called, request is removed from storage, further modification will work! |
||
| 533 | */ |
||
| 534 | public function remove_if_not_modified() |
||
| 538 | } |
||
| 539 |