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 XoopsBlock 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 XoopsBlock, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | class XoopsBlock extends XoopsObject |
||
| 31 | { |
||
| 32 | const CUSTOM_HTML = 'H'; // custom HTML block |
||
| 33 | const CUSTOM_PHP = 'P'; // custom PHP block |
||
| 34 | const CUSTOM_SMILIE = 'S'; // use text sanitizer (smilies enabled) |
||
| 35 | const CUSTOM_TEXT = 'T'; // use text sanitizer (smilies disabled) |
||
| 36 | |||
| 37 | const BLOCK_TYPE_SYSTEM = 'S'; // S - generated by system module |
||
| 38 | const BLOCK_TYPE_MODULE = 'M'; // M - generated by a non-system module |
||
| 39 | const BLOCK_TYPE_CUSTOM = 'C'; // C - Custom block |
||
| 40 | const BLOCK_TYPE_CLONED = 'D'; // D - cloned system/module block |
||
| 41 | // E - cloned custom block, DON'T use it |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Constructor |
||
| 45 | * |
||
| 46 | * @param int|array $id object id |
||
| 47 | */ |
||
| 48 | 34 | public function __construct($id = null) |
|
| 89 | |||
| 90 | |||
| 91 | /** |
||
| 92 | * getter |
||
| 93 | * |
||
| 94 | * @param string $format Dtype::FORMAT_xxxx constant |
||
| 95 | * |
||
| 96 | * @return mixed |
||
| 97 | */ |
||
| 98 | 1 | public function id($format = 'n') |
|
| 102 | |||
| 103 | /** |
||
| 104 | * getter |
||
| 105 | * |
||
| 106 | * @param string $format Dtype::FORMAT_xxxx constant |
||
| 107 | * |
||
| 108 | * @return mixed |
||
| 109 | */ |
||
| 110 | 2 | public function bid($format = '') |
|
| 114 | |||
| 115 | /** |
||
| 116 | * getter |
||
| 117 | * |
||
| 118 | * @param string $format Dtype::FORMAT_xxxx constant |
||
| 119 | * |
||
| 120 | * @return mixed |
||
| 121 | */ |
||
| 122 | 1 | public function mid($format = '') |
|
| 126 | |||
| 127 | /** |
||
| 128 | * getter |
||
| 129 | * |
||
| 130 | * @param string $format Dtype::FORMAT_xxxx constant |
||
| 131 | * |
||
| 132 | * @return mixed |
||
| 133 | */ |
||
| 134 | 1 | public function func_num($format = '') |
|
| 138 | |||
| 139 | /** |
||
| 140 | * getter |
||
| 141 | * |
||
| 142 | * @param string $format Dtype::FORMAT_xxxx constant |
||
| 143 | * |
||
| 144 | * @return mixed |
||
| 145 | */ |
||
| 146 | 1 | public function options($format = '') |
|
| 150 | |||
| 151 | /** |
||
| 152 | * getter |
||
| 153 | * |
||
| 154 | * @param string $format Dtype::FORMAT_xxxx constant |
||
| 155 | * |
||
| 156 | * @return mixed |
||
| 157 | */ |
||
| 158 | 1 | public function name($format = '') |
|
| 162 | |||
| 163 | /** |
||
| 164 | * getter |
||
| 165 | * |
||
| 166 | * @param string $format Dtype::FORMAT_xxxx constant |
||
| 167 | * |
||
| 168 | * @return mixed |
||
| 169 | */ |
||
| 170 | 1 | public function title($format = '') |
|
| 174 | |||
| 175 | /** |
||
| 176 | * getter |
||
| 177 | * |
||
| 178 | * @param string $format Dtype::FORMAT_xxxx constant |
||
| 179 | * |
||
| 180 | * @return mixed |
||
| 181 | */ |
||
| 182 | 1 | public function content($format = '') |
|
| 186 | |||
| 187 | /** |
||
| 188 | * getter |
||
| 189 | * |
||
| 190 | * @param string $format Dtype::FORMAT_xxxx constant |
||
| 191 | * |
||
| 192 | * @return mixed |
||
| 193 | */ |
||
| 194 | 1 | public function side($format = '') |
|
| 198 | |||
| 199 | /** |
||
| 200 | * getter |
||
| 201 | * |
||
| 202 | * @param string $format Dtype::FORMAT_xxxx constant |
||
| 203 | * |
||
| 204 | * @return mixed |
||
| 205 | */ |
||
| 206 | 1 | public function weight($format = '') |
|
| 210 | |||
| 211 | /** |
||
| 212 | * getter |
||
| 213 | * |
||
| 214 | * @param string $format Dtype::FORMAT_xxxx constant |
||
| 215 | * |
||
| 216 | * @return mixed |
||
| 217 | */ |
||
| 218 | 1 | public function visible($format = '') |
|
| 222 | |||
| 223 | /** |
||
| 224 | * getter |
||
| 225 | * |
||
| 226 | * @param string $format Dtype::FORMAT_xxxx constant |
||
| 227 | * |
||
| 228 | * @return mixed |
||
| 229 | */ |
||
| 230 | 1 | public function block_type($format = '') |
|
| 234 | |||
| 235 | /** |
||
| 236 | * getter custom block type XoopsBlock::CUSTOM_xxxx constant |
||
| 237 | * |
||
| 238 | * @param string $format Dtype::FORMAT_xxxx constant |
||
| 239 | * |
||
| 240 | * @return mixed |
||
| 241 | */ |
||
| 242 | 1 | public function c_type($format = '') |
|
| 246 | |||
| 247 | /** |
||
| 248 | * getter |
||
| 249 | * |
||
| 250 | * @param string $format Dtype::FORMAT_xxxx constant |
||
| 251 | * |
||
| 252 | * @return mixed |
||
| 253 | */ |
||
| 254 | 1 | public function isactive($format = '') |
|
| 258 | |||
| 259 | /** |
||
| 260 | * getter |
||
| 261 | * |
||
| 262 | * @param string $format Dtype::FORMAT_xxxx constant |
||
| 263 | * |
||
| 264 | * @return mixed |
||
| 265 | */ |
||
| 266 | 1 | public function dirname($format = '') |
|
| 270 | |||
| 271 | /** |
||
| 272 | * getter |
||
| 273 | * |
||
| 274 | * @param string $format Dtype::FORMAT_xxxx constant |
||
| 275 | * |
||
| 276 | * @return mixed |
||
| 277 | */ |
||
| 278 | 1 | public function func_file($format = '') |
|
| 282 | |||
| 283 | /** |
||
| 284 | * getter |
||
| 285 | * |
||
| 286 | * @param string $format Dtype::FORMAT_xxxx constant |
||
| 287 | * |
||
| 288 | * @return mixed |
||
| 289 | */ |
||
| 290 | 1 | public function show_func($format = '') |
|
| 294 | |||
| 295 | /** |
||
| 296 | * getter |
||
| 297 | * |
||
| 298 | * @param string $format Dtype::FORMAT_xxxx constant |
||
| 299 | * |
||
| 300 | * @return mixed |
||
| 301 | */ |
||
| 302 | 1 | public function edit_func($format = '') |
|
| 306 | |||
| 307 | /** |
||
| 308 | * getter |
||
| 309 | * |
||
| 310 | * @param string $format Dtype::FORMAT_xxxx constant |
||
| 311 | * |
||
| 312 | * @return mixed |
||
| 313 | */ |
||
| 314 | 1 | public function template($format = '') |
|
| 318 | |||
| 319 | /** |
||
| 320 | * getter |
||
| 321 | * |
||
| 322 | * @param string $format Dtype::FORMAT_xxxx constant |
||
| 323 | * |
||
| 324 | * @return mixed |
||
| 325 | */ |
||
| 326 | 1 | public function bcachetime($format = '') |
|
| 330 | |||
| 331 | /** |
||
| 332 | * getter |
||
| 333 | * |
||
| 334 | * @param string $format Dtype::FORMAT_xxxx constant |
||
| 335 | * |
||
| 336 | * @return mixed |
||
| 337 | */ |
||
| 338 | 1 | public function last_modified($format = '') |
|
| 342 | |||
| 343 | /** |
||
| 344 | * return the content of the block for output |
||
| 345 | * |
||
| 346 | * @param string $format Dtype::FORMAT_xxxx constant |
||
| 347 | * @param string $c_type type of custom content, a XoopsBlock::CUSTOM_xxxx constant |
||
| 348 | * H : custom HTML block |
||
| 349 | * P : custom PHP block |
||
| 350 | * S : use text sanitizer (smilies enabled) |
||
| 351 | * T : use text sanitizer (smilies disabled) |
||
| 352 | * |
||
| 353 | * @return string content for output |
||
| 354 | */ |
||
| 355 | public function getContent($format = 's', $c_type = 'T') |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Convert {X_SITEURL} to actual site URL in content |
||
| 394 | * |
||
| 395 | * @param string $content content to convert |
||
| 396 | * |
||
| 397 | * @return string |
||
| 398 | */ |
||
| 399 | protected function convertSiteURL($content) |
||
| 404 | |||
| 405 | /** |
||
| 406 | * (HTML-) form for setting the options of the block |
||
| 407 | * |
||
| 408 | * @return string|false HTML for the form, FALSE if not defined for this block |
||
| 409 | */ |
||
| 410 | 1 | public function getOptions() |
|
| 443 | |||
| 444 | /** |
||
| 445 | * Determine if this is a custom block |
||
| 446 | * |
||
| 447 | * @return bool true if this is a custom block |
||
| 448 | */ |
||
| 449 | 5 | public function isCustom() |
|
| 453 | |||
| 454 | /************ADDED**************/ |
||
| 455 | |||
| 456 | |||
| 457 | /** |
||
| 458 | * Build Block |
||
| 459 | * |
||
| 460 | * @return array|bool |
||
| 461 | */ |
||
| 462 | 2 | public function buildBlock() |
|
| 504 | } |
||
| 505 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: