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 Template 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 Template, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class Template extends Mabilis |
||
| 14 | { |
||
| 15 | |||
| 16 | protected $main_layout = 'main'; |
||
| 17 | |||
| 18 | public $modules_template_dir; |
||
| 19 | |||
| 20 | public $template_dir; |
||
| 21 | |||
| 22 | public $template_vars = []; |
||
| 23 | |||
| 24 | private $_css_files = []; |
||
| 25 | |||
| 26 | private $_js_files = []; |
||
| 27 | |||
| 28 | private $_links = []; |
||
| 29 | |||
| 30 | private $_css_str = []; |
||
| 31 | |||
| 32 | private $_custom_strings = []; |
||
| 33 | |||
| 34 | private $_metas = []; |
||
| 35 | |||
| 36 | private $_canonicals = ''; |
||
| 37 | |||
| 38 | private static $arr = []; |
||
| 39 | |||
| 40 | private static $result_before = ''; |
||
| 41 | |||
| 42 | private static $result_after = ''; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * is tpl trimmed |
||
| 46 | * @var bool |
||
| 47 | */ |
||
| 48 | public $trimed = false; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * |
||
| 52 | * @var MY_Controller |
||
| 53 | */ |
||
| 54 | public $CI; |
||
| 55 | |||
| 56 | public function __construct() { |
||
| 62 | |||
| 63 | /** |
||
| 64 | * |
||
| 65 | * @param string $main_layout |
||
| 66 | * @throws Exception |
||
| 67 | */ |
||
| 68 | public function set_main_layout($main_layout) { |
||
| 75 | |||
| 76 | /** |
||
| 77 | * |
||
| 78 | * @param string $main_layout_full_path |
||
| 79 | * @throws Exception |
||
| 80 | */ |
||
| 81 | public function set_main_layout_by_full_path($main_layout_full_path) { |
||
| 86 | |||
| 87 | public function load() { |
||
| 123 | |||
| 124 | /** |
||
| 125 | * |
||
| 126 | * @param string $key |
||
| 127 | * @param string|array $value |
||
| 128 | */ |
||
| 129 | public function assign($key, $value) { |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Add array to template data |
||
| 135 | * |
||
| 136 | * @param $arr |
||
| 137 | * @return bool |
||
| 138 | */ |
||
| 139 | public function add_array($arr) { |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Display template file included in main.tpl if $load_main is TRUE |
||
| 150 | * |
||
| 151 | * @access public |
||
| 152 | * @param string|boolean $file |
||
| 153 | * @param boolean $load_main |
||
| 154 | * @param array $data |
||
| 155 | * @return boolean|null |
||
| 156 | */ |
||
| 157 | public function show($file = FALSE, $load_main = TRUE, $data = []) { |
||
| 187 | |||
| 188 | public function clear_all_assign() { |
||
| 191 | |||
| 192 | /** |
||
| 193 | * |
||
| 194 | * @param string $name |
||
| 195 | */ |
||
| 196 | public function clear_assign($name) { |
||
| 199 | |||
| 200 | /** |
||
| 201 | * |
||
| 202 | * @param string $var |
||
| 203 | * @return string|integer|float|array|boolean |
||
| 204 | */ |
||
| 205 | public function get_var($var) { |
||
| 208 | |||
| 209 | /** |
||
| 210 | * |
||
| 211 | * @return string|integer|float|array|boolean |
||
| 212 | */ |
||
| 213 | public function get_vars() { |
||
| 216 | |||
| 217 | public function run_info() { |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Fetch file |
||
| 233 | * |
||
| 234 | * @access public |
||
| 235 | * @param boolean|string $file |
||
| 236 | * @param array $data |
||
| 237 | * @return string |
||
| 238 | */ |
||
| 239 | public function read($file = FALSE, $data = []) { |
||
| 247 | |||
| 248 | /** |
||
| 249 | * |
||
| 250 | * @param boolean|string $file |
||
| 251 | * @param array $data |
||
| 252 | * @return string |
||
| 253 | */ |
||
| 254 | public function fetch($file = FALSE, $data = []) { |
||
| 257 | |||
| 258 | /** |
||
| 259 | * |
||
| 260 | * @param string $file |
||
| 261 | * @param array $data |
||
| 262 | * @param boolean $processOutput |
||
| 263 | */ |
||
| 264 | public function display($file, $data = [], $processOutput = true) { |
||
| 277 | |||
| 278 | /** |
||
| 279 | * |
||
| 280 | * @param string $file |
||
| 281 | * @param array $data |
||
| 282 | * @param boolean $return |
||
| 283 | * @return string |
||
| 284 | */ |
||
| 285 | public function view($file, $data = [], $return = FALSE) { |
||
| 288 | |||
| 289 | /** |
||
| 290 | * |
||
| 291 | * @param string $name |
||
| 292 | * @param string $path |
||
| 293 | * @param array $data |
||
| 294 | * @param boolean $processOutput |
||
| 295 | */ |
||
| 296 | public function include_tpl($name, $path, $data = [], $processOutput = true) { |
||
| 300 | |||
| 301 | /** |
||
| 302 | * |
||
| 303 | * @param string $name |
||
| 304 | * @param string $path |
||
| 305 | * @param array $data |
||
| 306 | * @param boolean $processOutput |
||
| 307 | */ |
||
| 308 | public function include_shop_tpl($name, $path, $data = [], $processOutput = true) { |
||
| 312 | |||
| 313 | /** |
||
| 314 | * |
||
| 315 | * @param string $url |
||
| 316 | * @param string $position |
||
| 317 | */ |
||
| 318 | public function registerCssFile($url, $position = 'before') { |
||
| 325 | |||
| 326 | /** |
||
| 327 | * |
||
| 328 | * @param string $css |
||
| 329 | * @param string $position |
||
| 330 | */ |
||
| 331 | public function registerCss($css, $position = 'before') { |
||
| 335 | |||
| 336 | /** |
||
| 337 | * |
||
| 338 | * @param string $url |
||
| 339 | * @param string $position |
||
| 340 | * @param boolean $fromThisSite |
||
| 341 | */ |
||
| 342 | public function registerJsFile($url, $position = 'before', $fromThisSite = TRUE) { |
||
| 353 | |||
| 354 | /** |
||
| 355 | * |
||
| 356 | * @param string $script |
||
| 357 | * @param string $position |
||
| 358 | */ |
||
| 359 | public function registerJsScript($script, $position = 'before') { |
||
| 363 | |||
| 364 | /** |
||
| 365 | * @param string $script |
||
| 366 | * @param string $position |
||
| 367 | */ |
||
| 368 | public function registerString($script, $position = 'before') { |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Place meta code before /head |
||
| 375 | * @param string $name meta name |
||
| 376 | * @param string $content meta content |
||
| 377 | */ |
||
| 378 | public function registerMeta($name, $content) { |
||
| 381 | |||
| 382 | /** |
||
| 383 | * |
||
| 384 | * @param string $url |
||
| 385 | * @param string $rel |
||
| 386 | */ |
||
| 387 | public function registerLink($url, $rel) { |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Place canonical code before /head |
||
| 393 | * @param string $url canonical url |
||
| 394 | */ |
||
| 395 | public function registerCanonical($url) { |
||
| 400 | |||
| 401 | /** |
||
| 402 | * |
||
| 403 | * @param string $position |
||
| 404 | * @return string |
||
| 405 | */ |
||
| 406 | private function _check_postion($position) { |
||
| 412 | |||
| 413 | /** |
||
| 414 | * |
||
| 415 | * @param string $tpl |
||
| 416 | * @return string |
||
| 417 | */ |
||
| 418 | public function splitTplFiles($tpl) { |
||
| 487 | |||
| 488 | /** |
||
| 489 | * |
||
| 490 | * @param array $data |
||
| 491 | */ |
||
| 492 | protected function split($data) { |
||
| 516 | |||
| 517 | } |
||
| 518 | |||
| 519 | /* End of template.php */ |
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: