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 Dwoo_Smarty__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 Dwoo_Smarty__Adapter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 32 | if (!defined('SMARTY_PHP_PASSTHRU')) { |
||
| 33 | define('SMARTY_PHP_PASSTHRU', 0); |
||
| 34 | define('SMARTY_PHP_QUOTE', 1); |
||
| 35 | define('SMARTY_PHP_REMOVE', 2); |
||
| 36 | define('SMARTY_PHP_ALLOW', 3); |
||
| 37 | } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * A Smarty compatibility layer for Dwoo. |
||
| 41 | * This software is provided 'as-is', without any express or implied warranty. |
||
| 42 | * In no event will the authors be held liable for any damages arising from the use of this software. |
||
| 43 | */ |
||
| 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) |
||
| 472 | { |
||
| 473 | foreach ($this->_filters['output'] as $index => $filter) { |
||
| 474 | if ($filter->callback === $callback) { |
||
| 475 | $this->removeOutputFilter($filter); |
||
| 476 | unset($this->_filters['output'][$index]); |
||
| 477 | } |
||
| 478 | } |
||
| 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..