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 Adapter 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 Adapter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
44 | class Adapter extends Core |
||
45 | { |
||
46 | /** |
||
47 | * Magic get/set/call functions that handle unsupported features |
||
48 | * |
||
49 | * @param string $p |
||
50 | * @param string $v |
||
51 | */ |
||
52 | public function __set($p, $v) |
||
75 | |||
76 | /** |
||
77 | * @param $p |
||
78 | * |
||
79 | * @return mixed |
||
80 | */ |
||
81 | public function __get($p) |
||
95 | |||
96 | /** |
||
97 | * @param string $m |
||
98 | * @param array $a |
||
99 | * |
||
100 | * @return mixed|void |
||
101 | */ |
||
102 | public function __call($m, $a) |
||
119 | |||
120 | /** |
||
121 | * List of unsupported properties and methods |
||
122 | */ |
||
123 | protected $compat = array( |
||
124 | 'methods' => array( |
||
125 | 'register_resource', |
||
126 | 'unregister_resource', |
||
127 | 'load_filter', |
||
128 | 'clear_compiled_tpl', |
||
129 | 'clear_config', |
||
130 | 'get_config_vars', |
||
131 | 'config_load', |
||
132 | ), |
||
133 | 'properties' => array( |
||
134 | 'cache_handler_func' => null, |
||
135 | 'debugging' => false, |
||
136 | 'error_reporting' => null, |
||
137 | 'debugging_ctrl' => 'NONE', |
||
138 | 'request_vars_order' => 'EGPCS', |
||
139 | 'request_use_auto_globals' => true, |
||
140 | 'use_sub_dirs' => false, |
||
141 | 'autoload_filters' => array(), |
||
142 | 'default_template_handler_func' => '', |
||
143 | 'debug_tpl' => '', |
||
144 | 'cache_modified_check' => false, |
||
145 | 'default_modifiers' => array(), |
||
146 | 'default_resource_type' => 'file', |
||
147 | 'config_overwrite' => true, |
||
148 | 'config_booleanize' => true, |
||
149 | 'config_read_hidden' => false, |
||
150 | 'config_fix_newlines' => true, |
||
151 | 'config_class' => 'Config_File', |
||
152 | ), |
||
153 | ); |
||
154 | |||
155 | /** |
||
156 | * Security vars |
||
157 | */ |
||
158 | public $security = false; |
||
159 | public $trusted_dir = array(); |
||
160 | public $secure_dir = array(); |
||
161 | public $php_handling = SMARTY_PHP_PASSTHRU; |
||
162 | public $security_settings = array( |
||
163 | 'PHP_HANDLING' => false, |
||
164 | 'IF_FUNCS' => array( |
||
165 | 'list', |
||
166 | 'empty', |
||
167 | 'count', |
||
168 | 'sizeof', |
||
169 | 'in_array', |
||
170 | 'is_array', |
||
171 | ), |
||
172 | 'INCLUDE_ANY' => false, |
||
173 | 'PHP_TAGS' => false, |
||
174 | 'MODIFIER_FUNCS' => array(), |
||
175 | 'ALLOW_CONSTANTS' => false, |
||
176 | ); |
||
177 | |||
178 | /** |
||
179 | * Paths |
||
180 | */ |
||
181 | public $template_dir = 'templates'; |
||
182 | public $compile_dir = 'templates_c'; |
||
183 | public $config_dir = 'configs'; |
||
184 | public $cache_dir = 'cache'; |
||
185 | public $plugins_dir = array(); |
||
186 | |||
187 | /** |
||
188 | * Misc options |
||
189 | */ |
||
190 | public $left_delimiter = '{'; |
||
191 | public $right_delimiter = '}'; |
||
192 | public $compile_check = true; |
||
193 | public $force_compile = false; |
||
194 | public $caching = 0; |
||
195 | public $cache_lifetime = 3600; |
||
196 | public $compile_id = null; |
||
197 | public $compiler_file = null; |
||
198 | public $compiler_class = null; |
||
199 | |||
200 | /** |
||
201 | * Dwoo/Smarty compat layer |
||
202 | */ |
||
203 | public $show_compat_errors = false; |
||
204 | protected $dataProvider; |
||
205 | protected $_filters = array( |
||
206 | 'pre' => array(), |
||
207 | 'post' => array(), |
||
208 | 'output' => array() |
||
209 | ); |
||
210 | protected static $tplCache = array(); |
||
211 | protected $compiler = null; |
||
212 | |||
213 | /** |
||
214 | * Adapter constructor. |
||
215 | */ |
||
216 | public function __construct() |
||
223 | |||
224 | /** |
||
225 | * @param $filename |
||
226 | * @param null $cacheId |
||
227 | * @param null $compileId |
||
228 | */ |
||
229 | public function display($filename, $cacheId = null, $compileId = null) |
||
233 | |||
234 | /** |
||
235 | * @param $filename |
||
236 | * @param null $cacheId |
||
237 | * @param null $compileId |
||
238 | * @param bool $display |
||
239 | * |
||
240 | * @return string|void |
||
241 | */ |
||
242 | public function fetch($filename, $cacheId = null, $compileId = null, $display = false) |
||
312 | |||
313 | /** |
||
314 | * @param mixed $_tpl |
||
315 | * @param array $data |
||
316 | * @param null $_compiler |
||
317 | * @param bool $_output |
||
318 | * |
||
319 | * @return string|void |
||
320 | */ |
||
321 | public function get($_tpl, $data = array(), $_compiler = null, $_output = false) |
||
329 | |||
330 | /** |
||
331 | * @param $name |
||
332 | * @param $callback |
||
333 | * @param bool $cacheable |
||
334 | * @param null $cache_attrs |
||
335 | * |
||
336 | * @throws Exception |
||
337 | */ |
||
338 | View Code Duplication | public function register_function($name, $callback, $cacheable = true, $cache_attrs = null) |
|
348 | |||
349 | /** |
||
350 | * @param $name |
||
351 | */ |
||
352 | public function unregister_function($name) |
||
356 | |||
357 | /** |
||
358 | * @param $name |
||
359 | * @param $callback |
||
360 | * @param bool $cacheable |
||
361 | * @param null $cache_attrs |
||
362 | * |
||
363 | * @throws Exception |
||
364 | */ |
||
365 | View Code Duplication | public function register_block($name, $callback, $cacheable = true, $cache_attrs = null) |
|
375 | |||
376 | /** |
||
377 | * @param $name |
||
378 | */ |
||
379 | public function unregister_block($name) |
||
383 | |||
384 | /** |
||
385 | * @param $name |
||
386 | * @param $callback |
||
387 | * |
||
388 | * @throws Exception |
||
389 | */ |
||
390 | View Code Duplication | public function register_modifier($name, $callback) |
|
400 | |||
401 | /** |
||
402 | * @param $name |
||
403 | */ |
||
404 | public function unregister_modifier($name) |
||
408 | |||
409 | /** |
||
410 | * @param $callback |
||
411 | */ |
||
412 | View Code Duplication | public function register_prefilter($callback) |
|
419 | |||
420 | /** |
||
421 | * @param $callback |
||
422 | */ |
||
423 | View Code Duplication | public function unregister_prefilter($callback) |
|
432 | |||
433 | /** |
||
434 | * @param $callback |
||
435 | */ |
||
436 | View Code Duplication | public function register_postfilter($callback) |
|
443 | |||
444 | /** |
||
445 | * @param $callback |
||
446 | */ |
||
447 | View Code Duplication | public function unregister_postfilter($callback) |
|
456 | |||
457 | /** |
||
458 | * @param $callback |
||
459 | */ |
||
460 | public function register_outputfilter($callback) |
||
467 | |||
468 | /** |
||
469 | * @param $callback |
||
470 | */ |
||
471 | public function unregister_outputfilter($callback) |
||
480 | |||
481 | /** |
||
482 | * @param $object |
||
483 | * @param $object_impl |
||
484 | * @param array $allowed |
||
485 | * @param bool $smarty_args |
||
486 | * @param array $block_methods |
||
487 | */ |
||
488 | public function register_object($object, $object_impl, $allowed = array(), $smarty_args = false, $block_methods = array()) |
||
508 | |||
509 | /** |
||
510 | * @param $object |
||
511 | */ |
||
512 | public function unregister_object($object) |
||
516 | |||
517 | /** |
||
518 | * @param $name |
||
519 | * |
||
520 | * @return mixed |
||
521 | */ |
||
522 | public function get_registered_object($name) |
||
531 | |||
532 | /** |
||
533 | * @param $filename |
||
534 | * |
||
535 | * @return bool |
||
536 | */ |
||
537 | public function template_exists($filename) |
||
551 | |||
552 | /** |
||
553 | * @param $tpl |
||
554 | * @param null $cacheId |
||
555 | * @param null $compileId |
||
556 | * |
||
557 | * @return bool |
||
558 | */ |
||
559 | public function is_cached($tpl, $cacheId = null, $compileId = null) |
||
563 | |||
564 | /** |
||
565 | * @param $var |
||
566 | * @param $value |
||
567 | * @param bool $merge |
||
568 | */ |
||
569 | public function append_by_ref($var, &$value, $merge = false) |
||
573 | |||
574 | /** |
||
575 | * @param $name |
||
576 | * @param $val |
||
577 | */ |
||
578 | public function assign_by_ref($name, &$val) |
||
582 | |||
583 | /** |
||
584 | * @param $var |
||
585 | */ |
||
586 | public function clear_assign($var) |
||
590 | |||
591 | /** |
||
592 | * |
||
593 | */ |
||
594 | public function clear_all_assign() |
||
598 | |||
599 | /** |
||
600 | * @param null $name |
||
601 | * |
||
602 | * @return array|null |
||
603 | */ |
||
604 | public function get_template_vars($name = null) |
||
619 | |||
620 | /** |
||
621 | * @param int $olderThan |
||
622 | */ |
||
623 | public function clear_all_cache($olderThan = 0) |
||
627 | |||
628 | /** |
||
629 | * @param $template |
||
630 | * @param null $cacheId |
||
631 | * @param null $compileId |
||
632 | * @param int $olderThan |
||
633 | */ |
||
634 | public function clear_cache($template, $cacheId = null, $compileId = null, $olderThan = 0) |
||
638 | |||
639 | /** |
||
640 | * @param $error_msg |
||
641 | * @param int $error_type |
||
642 | */ |
||
643 | public function trigger_error($error_msg, $error_type = E_USER_WARNING) |
||
647 | |||
648 | /** |
||
649 | * |
||
650 | */ |
||
651 | protected function initGlobals() |
||
657 | |||
658 | /** |
||
659 | * @param $file |
||
660 | * @param $cacheId |
||
661 | * @param $compileId |
||
662 | * |
||
663 | * @return mixed |
||
664 | * @throws Exception |
||
665 | */ |
||
666 | protected function makeTemplate($file, $cacheId, $compileId) |
||
686 | |||
687 | /** |
||
688 | * @param string $message |
||
689 | * @param int $level |
||
690 | */ |
||
691 | public function triggerError($message, $level = E_USER_NOTICE) |
||
698 | } |
||
699 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..