Complex classes like TDMCreateFile 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 TDMCreateFile, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class TDMCreateFile extends TDMCreateTableFields |
||
| 29 | { |
||
| 30 | /* |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | private $xoopsFile = null; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * "fileName" attribute of the files. |
||
| 37 | * |
||
| 38 | * @var mixed |
||
| 39 | */ |
||
| 40 | private $fileName = null; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * "subdir" attribute of the directories. |
||
| 44 | * |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | private $subdir = null; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * "uploadPath" attribute of the files. |
||
| 51 | * |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | private $uploadPath = null; |
||
| 55 | |||
| 56 | /* |
||
| 57 | * @var string |
||
| 58 | */ |
||
| 59 | private $content = null; |
||
| 60 | |||
| 61 | /* |
||
| 62 | * @var mixed |
||
| 63 | */ |
||
| 64 | private $created = null; |
||
| 65 | |||
| 66 | /* |
||
| 67 | * @var mixed |
||
| 68 | */ |
||
| 69 | private $notCreated = null; |
||
| 70 | |||
| 71 | /* |
||
| 72 | * @var string |
||
| 73 | */ |
||
| 74 | private $mode = null; |
||
| 75 | |||
| 76 | /* |
||
| 77 | * @var string |
||
| 78 | */ |
||
| 79 | protected $phpcode = null; |
||
| 80 | |||
| 81 | /* |
||
| 82 | * @var string |
||
| 83 | */ |
||
| 84 | private $tdmcreate = null; |
||
| 85 | |||
| 86 | /* |
||
| 87 | * @public function constructor |
||
| 88 | * @param null |
||
| 89 | */ |
||
| 90 | /** |
||
| 91 | * |
||
| 92 | */ |
||
| 93 | public function __construct() |
||
| 99 | |||
| 100 | /* |
||
| 101 | * @public static function &getInstance |
||
| 102 | * @param null |
||
| 103 | */ |
||
| 104 | /** |
||
| 105 | * @return TDMCreateFile |
||
| 106 | */ |
||
| 107 | public static function &getInstance() |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @public function create |
||
| 119 | * |
||
| 120 | * @param $moduleDirname |
||
| 121 | * @param $subdir |
||
| 122 | * @param $fileName |
||
| 123 | * @param $content |
||
| 124 | * @param $created |
||
| 125 | * @param $notCreated |
||
| 126 | * @param $mode |
||
| 127 | */ |
||
| 128 | public function create($moduleDirname, $subdir = null, $fileName = null, $content = '', $created = null, $notCreated = null, $mode = 'w+') |
||
| 142 | |||
| 143 | /* |
||
| 144 | * @public function write |
||
| 145 | * @param string $module |
||
| 146 | * @param string $fileName |
||
| 147 | */ |
||
| 148 | /** |
||
| 149 | * @param $module |
||
| 150 | * @param $fileName |
||
| 151 | */ |
||
| 152 | public function write($module, $fileName) |
||
| 157 | |||
| 158 | /* |
||
| 159 | * @private function setRepositoryPath |
||
| 160 | * @param string $moduleDirname |
||
| 161 | */ |
||
| 162 | /** |
||
| 163 | * @param $moduleDirname |
||
| 164 | */ |
||
| 165 | private function setRepositoryPath($moduleDirname) |
||
| 169 | |||
| 170 | /* |
||
| 171 | * @private function getRepositoryPath |
||
| 172 | * @param null |
||
| 173 | */ |
||
| 174 | /** |
||
| 175 | * @return string |
||
| 176 | */ |
||
| 177 | private function getRepositoryPath() |
||
| 181 | |||
| 182 | /* |
||
| 183 | * @private function setSubDir |
||
| 184 | * @param string $subdir |
||
| 185 | */ |
||
| 186 | /** |
||
| 187 | * @param $subdir |
||
| 188 | */ |
||
| 189 | private function setSubDir($subdir) |
||
| 193 | |||
| 194 | /* |
||
| 195 | * @private function getSubDir |
||
| 196 | * @param null |
||
| 197 | */ |
||
| 198 | private function getSubDir() |
||
| 202 | |||
| 203 | /** |
||
| 204 | * public function setFileName. |
||
| 205 | * |
||
| 206 | * @param $fileName |
||
| 207 | * |
||
| 208 | * @internal param mixed $file_name |
||
| 209 | */ |
||
| 210 | public function setFileName($fileName) |
||
| 214 | |||
| 215 | /* |
||
| 216 | * @public function getFileName |
||
| 217 | * @param null |
||
| 218 | */ |
||
| 219 | /** |
||
| 220 | * @return mixed |
||
| 221 | */ |
||
| 222 | public function getFileName() |
||
| 226 | |||
| 227 | /* |
||
| 228 | * @private function setContent |
||
| 229 | * @param string $content |
||
| 230 | */ |
||
| 231 | /** |
||
| 232 | * @param $content |
||
| 233 | */ |
||
| 234 | private function setContent($content) |
||
| 238 | |||
| 239 | /* |
||
| 240 | * @private function setContent |
||
| 241 | * @param null |
||
| 242 | */ |
||
| 243 | /** |
||
| 244 | */ |
||
| 245 | private function getContent() |
||
| 249 | |||
| 250 | /* |
||
| 251 | * @private function getFolderName |
||
| 252 | * @param null |
||
| 253 | */ |
||
| 254 | /** |
||
| 255 | * @return string |
||
| 256 | */ |
||
| 257 | private function getFolderName() |
||
| 271 | |||
| 272 | /* |
||
| 273 | * @public function getUploadPath |
||
| 274 | * @param null |
||
| 275 | */ |
||
| 276 | /** |
||
| 277 | * @return string |
||
| 278 | */ |
||
| 279 | private function getUploadPath() |
||
| 289 | |||
| 290 | /* |
||
| 291 | * @private function getCreated |
||
| 292 | * @param null |
||
| 293 | */ |
||
| 294 | /** |
||
| 295 | * @return bool |
||
| 296 | */ |
||
| 297 | private function getCreated() |
||
| 301 | |||
| 302 | /* |
||
| 303 | * @private function getNotCreated |
||
| 304 | * @param null |
||
| 305 | */ |
||
| 306 | /** |
||
| 307 | * @return bool |
||
| 308 | */ |
||
| 309 | private function getNotCreated() |
||
| 313 | |||
| 314 | /* |
||
| 315 | * @private function setMode |
||
| 316 | * @param string $mode |
||
| 317 | */ |
||
| 318 | /** |
||
| 319 | * @param $mode |
||
| 320 | */ |
||
| 321 | private function setMode($mode) |
||
| 325 | |||
| 326 | /* |
||
| 327 | * @private function getMode |
||
| 328 | * @param null |
||
| 329 | */ |
||
| 330 | /** |
||
| 331 | */ |
||
| 332 | private function getMode() |
||
| 336 | |||
| 337 | /* |
||
| 338 | * @public function getLanguage |
||
| 339 | * @param string $moduleDirname |
||
| 340 | * @param string $prefix |
||
| 341 | * @param string $suffix |
||
| 342 | */ |
||
| 343 | /** |
||
| 344 | * @param $moduleDirname |
||
| 345 | * @param string $prefix |
||
| 346 | * @param string $suffix |
||
| 347 | * |
||
| 348 | * @return string |
||
| 349 | */ |
||
| 350 | public function getLanguage($moduleDirname, $prefix = '', $suffix = '') |
||
| 362 | |||
| 363 | /* |
||
| 364 | * @public function getLeftString |
||
| 365 | * @param string $string |
||
| 366 | */ |
||
| 367 | /** |
||
| 368 | * @param $string |
||
| 369 | * |
||
| 370 | * @return string |
||
| 371 | */ |
||
| 372 | public function getLeftString($string) |
||
| 376 | |||
| 377 | /* |
||
| 378 | * @public function getRightString |
||
| 379 | * @param string $string |
||
| 380 | */ |
||
| 381 | /** |
||
| 382 | * @param $string |
||
| 383 | * |
||
| 384 | * @return string |
||
| 385 | */ |
||
| 386 | public function getRightString($string) |
||
| 399 | |||
| 400 | /* |
||
| 401 | * @public function getCamelCase |
||
| 402 | * @param $string |
||
| 403 | */ |
||
| 404 | /** |
||
| 405 | * @param $string |
||
| 406 | * @param $ucfirst |
||
| 407 | * @param $lcfirst |
||
| 408 | * |
||
| 409 | * @return string |
||
| 410 | */ |
||
| 411 | public function getCamelCase($string, $ucfirst = false, $lcfirst = false) |
||
| 424 | |||
| 425 | /* |
||
| 426 | * @public function getUcfirst |
||
| 427 | * @param $string |
||
| 428 | */ |
||
| 429 | /** |
||
| 430 | * @param $string |
||
| 431 | * |
||
| 432 | * @return string |
||
| 433 | */ |
||
| 434 | public function getUcfirst($string) |
||
| 438 | |||
| 439 | /* |
||
| 440 | * @public function getLcfirst |
||
| 441 | * @param $string |
||
| 442 | */ |
||
| 443 | /** |
||
| 444 | * @param $string |
||
| 445 | * |
||
| 446 | * @return string |
||
| 447 | */ |
||
| 448 | public function getLcfirst($string) |
||
| 452 | |||
| 453 | /* |
||
| 454 | * @public function getStrToUpper |
||
| 455 | * @param $string |
||
| 456 | */ |
||
| 457 | /** |
||
| 458 | * @param $string |
||
| 459 | * |
||
| 460 | * @return string |
||
| 461 | */ |
||
| 462 | public function getStrToUpper($string) |
||
| 466 | |||
| 467 | /* |
||
| 468 | * @public function getStrToLower |
||
| 469 | * @param $string |
||
| 470 | */ |
||
| 471 | /** |
||
| 472 | * @param $string |
||
| 473 | * |
||
| 474 | * @return string |
||
| 475 | */ |
||
| 476 | public function getStrToLower($string) |
||
| 480 | |||
| 481 | /* |
||
| 482 | * @public function getInclude |
||
| 483 | * @param $filename |
||
| 484 | */ |
||
| 485 | /** |
||
| 486 | * @return string |
||
| 487 | */ |
||
| 488 | public function getInclude($filename = 'header') |
||
| 492 | |||
| 493 | /* |
||
| 494 | * @public function getIncludeOnce |
||
| 495 | * @param $filename |
||
| 496 | */ |
||
| 497 | /** |
||
| 498 | * @return string |
||
| 499 | */ |
||
| 500 | public function getIncludeOnce($filename = 'header') |
||
| 504 | |||
| 505 | /* |
||
| 506 | * @public function getCommentLine |
||
| 507 | * @param $string |
||
| 508 | * @param $var |
||
| 509 | * |
||
| 510 | * @return string |
||
| 511 | */ |
||
| 512 | public function getCommentLine($string = '', $var = '') |
||
| 519 | |||
| 520 | /** |
||
| 521 | * @private function getHeaderComment |
||
| 522 | * |
||
| 523 | * @param $comment |
||
| 524 | * |
||
| 525 | * @return string |
||
| 526 | */ |
||
| 527 | public function getHeaderComment($comment) |
||
| 531 | |||
| 532 | /* |
||
| 533 | * @public function getSimpleString |
||
| 534 | * @param $string |
||
| 535 | * |
||
| 536 | * @return string |
||
| 537 | */ |
||
| 538 | public function getSimpleString($string) |
||
| 542 | |||
| 543 | /* |
||
| 544 | * @public function getHeaderFilesComments |
||
| 545 | * @param string $module |
||
| 546 | * @param string $fileName |
||
| 547 | */ |
||
| 548 | /** |
||
| 549 | * @param $module |
||
| 550 | * @param $fileName |
||
| 551 | * @param $noPhpFile |
||
| 552 | * |
||
| 553 | * @return string |
||
| 554 | */ |
||
| 555 | public function getHeaderFilesComments($module, $fileName, $noPhpFile = null) |
||
| 591 | |||
| 592 | /* |
||
| 593 | * @public function renderFile |
||
| 594 | * @param null |
||
| 595 | */ |
||
| 596 | /** |
||
| 597 | * @return string |
||
| 598 | */ |
||
| 599 | public function renderFile() |
||
| 634 | } |
||
| 635 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.