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:
| 1 | <?php |
||
| 40 | trait SmartyBCTrait |
||
| 41 | { |
||
| 42 | /** |
||
| 43 | * Smarty 2 BC |
||
| 44 | * |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | public $_version = self::SMARTY_VERSION; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * This is an array of directories where trusted php scripts reside. |
||
| 51 | * |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | public $trusted_dir = array(); |
||
| 55 | |||
| 56 | /** |
||
| 57 | * wrapper for assign_by_ref |
||
| 58 | * |
||
| 59 | * @param string $tpl_var the template variable name |
||
| 60 | * @param mixed &$value the referenced value to assign |
||
| 61 | */ |
||
| 62 | public function assign_by_ref($tpl_var, &$value) |
||
| 67 | |||
| 68 | /** |
||
| 69 | * wrapper for append_by_ref |
||
| 70 | * |
||
| 71 | * @param string $tpl_var the template variable name |
||
| 72 | * @param mixed &$value the referenced value to append |
||
| 73 | * @param boolean $merge flag if array elements shall be merged |
||
| 74 | */ |
||
| 75 | public function append_by_ref($tpl_var, &$value, $merge = false) |
||
| 80 | |||
| 81 | /** |
||
| 82 | * clear the given assigned template variable. |
||
| 83 | * |
||
| 84 | * @param string $tpl_var the template variable to clear |
||
| 85 | */ |
||
| 86 | public function clear_assign($tpl_var) |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Registers custom function to be used in templates |
||
| 94 | * |
||
| 95 | * @param string $function the name of the template function |
||
| 96 | * @param string $function_impl the name of the PHP function to register |
||
| 97 | * @param bool $cacheable |
||
| 98 | * @param mixed $cache_attrs |
||
| 99 | */ |
||
| 100 | public function register_function($function, $function_impl, $cacheable = true, $cache_attrs = null) |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Unregisters custom function |
||
| 108 | * |
||
| 109 | * @param string $function name of template function |
||
| 110 | */ |
||
| 111 | public function unregister_function($function) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Registers object to be used in templates |
||
| 119 | * |
||
| 120 | * @param string $object name of template object |
||
| 121 | * @param object $object_impl the referenced PHP object to register |
||
| 122 | * @param array $allowed list of allowed methods (empty = all) |
||
| 123 | * @param boolean $smarty_args smarty argument format, else traditional |
||
| 124 | * @param array $block_methods list of methods that are block format |
||
| 125 | * |
||
| 126 | * @throws \SmartyException |
||
| 127 | * @internal param array $block_functs list of methods that are block format |
||
| 128 | */ |
||
| 129 | public function register_object($object, $object_impl, $allowed = array(), $smarty_args = true, $block_methods = array()) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Unregisters object |
||
| 139 | * |
||
| 140 | * @param string $object name of template object |
||
| 141 | */ |
||
| 142 | public function unregister_object($object) |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Registers block function to be used in templates |
||
| 150 | * |
||
| 151 | * @param string $block name of template block |
||
| 152 | * @param string $block_impl PHP function to register |
||
| 153 | * @param bool $cacheable |
||
| 154 | * @param mixed $cache_attrs |
||
| 155 | */ |
||
| 156 | public function register_block($block, $block_impl, $cacheable = true, $cache_attrs = null) |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Unregisters block function |
||
| 164 | * |
||
| 165 | * @param string $block name of template function |
||
| 166 | */ |
||
| 167 | public function unregister_block($block) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Registers compiler function |
||
| 175 | * |
||
| 176 | * @param string $function name of template function |
||
| 177 | * @param string $function_impl name of PHP function to register |
||
| 178 | * @param bool $cacheable |
||
| 179 | */ |
||
| 180 | public function register_compiler_function($function, $function_impl, $cacheable = true) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Unregisters compiler function |
||
| 188 | * |
||
| 189 | * @param string $function name of template function |
||
| 190 | */ |
||
| 191 | public function unregister_compiler_function($function) |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Registers modifier to be used in templates |
||
| 199 | * |
||
| 200 | * @param string $modifier name of template modifier |
||
| 201 | * @param string $modifier_impl name of PHP function to register |
||
| 202 | */ |
||
| 203 | public function register_modifier($modifier, $modifier_impl) |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Unregisters modifier |
||
| 211 | * |
||
| 212 | * @param string $modifier name of template modifier |
||
| 213 | */ |
||
| 214 | public function unregister_modifier($modifier) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Registers a resource to fetch a template |
||
| 222 | * |
||
| 223 | * @param string $type name of resource |
||
| 224 | * @param array $functions array of functions to handle resource |
||
| 225 | */ |
||
| 226 | public function register_resource($type, $functions) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Unregisters a resource |
||
| 234 | * |
||
| 235 | * @param string $type name of resource |
||
| 236 | */ |
||
| 237 | public function unregister_resource($type) |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Registers a prefilter function to apply |
||
| 245 | * to a template before compiling |
||
| 246 | * |
||
| 247 | * @param callable $function |
||
| 248 | */ |
||
| 249 | public function register_prefilter($function) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Unregisters a prefilter function |
||
| 257 | * |
||
| 258 | * @param callable $function |
||
| 259 | */ |
||
| 260 | public function unregister_prefilter($function) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Registers a postfilter function to apply |
||
| 268 | * to a compiled template after compilation |
||
| 269 | * |
||
| 270 | * @param callable $function |
||
| 271 | */ |
||
| 272 | public function register_postfilter($function) |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Unregisters a postfilter function |
||
| 280 | * |
||
| 281 | * @param callable $function |
||
| 282 | */ |
||
| 283 | public function unregister_postfilter($function) |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Registers an output filter function to apply |
||
| 291 | * to a template output |
||
| 292 | * |
||
| 293 | * @param callable $function |
||
| 294 | */ |
||
| 295 | public function register_outputfilter($function) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Unregisters an outputfilter function |
||
| 303 | * |
||
| 304 | * @param callable $function |
||
| 305 | */ |
||
| 306 | public function unregister_outputfilter($function) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * load a filter of specified type and name |
||
| 314 | * |
||
| 315 | * @param string $type filter type |
||
| 316 | * @param string $name filter name |
||
| 317 | */ |
||
| 318 | public function load_filter($type, $name) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * clear cached content for the given template and cache id |
||
| 326 | * |
||
| 327 | * @param string $tpl_file name of template file |
||
| 328 | * @param string $cache_id name of cache_id |
||
| 329 | * @param string $compile_id name of compile_id |
||
| 330 | * @param string $exp_time expiration time |
||
| 331 | * |
||
| 332 | * @return boolean |
||
| 333 | */ |
||
| 334 | public function clear_cache($tpl_file = null, $cache_id = null, $compile_id = null, $exp_time = null) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * clear the entire contents of cache (all templates) |
||
| 342 | * |
||
| 343 | * @param string $exp_time expire time |
||
| 344 | * |
||
| 345 | * @return boolean |
||
| 346 | */ |
||
| 347 | public function clear_all_cache($exp_time = null) |
||
| 352 | |||
| 353 | /** |
||
| 354 | * test to see if valid cache exists for this template |
||
| 355 | * |
||
| 356 | * @param string $tpl_file name of template file |
||
| 357 | * @param string $cache_id |
||
| 358 | * @param string $compile_id |
||
| 359 | * |
||
| 360 | * @return boolean |
||
| 361 | */ |
||
| 362 | public function is_cached($tpl_file, $cache_id = null, $compile_id = null) |
||
| 367 | |||
| 368 | /** |
||
| 369 | * clear all the assigned template variables. |
||
| 370 | */ |
||
| 371 | public function clear_all_assign() |
||
| 376 | |||
| 377 | /** |
||
| 378 | * clears compiled version of specified template resource, |
||
| 379 | * or all compiled template files if one is not specified. |
||
| 380 | * This function is for advanced use only, not normally needed. |
||
| 381 | * |
||
| 382 | * @param string $tpl_file |
||
| 383 | * @param string $compile_id |
||
| 384 | * @param string $exp_time |
||
| 385 | * |
||
| 386 | * @return boolean results of {@link smarty_core_rm_auto()} |
||
| 387 | */ |
||
| 388 | public function clear_compiled_tpl($tpl_file = null, $compile_id = null, $exp_time = null) |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Checks whether requested template exists. |
||
| 396 | * |
||
| 397 | * @param string $tpl_file |
||
| 398 | * |
||
| 399 | * @return boolean |
||
| 400 | */ |
||
| 401 | public function template_exists($tpl_file) |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Returns an array containing template variables |
||
| 409 | * |
||
| 410 | * @param string $name |
||
| 411 | * |
||
| 412 | * @return array |
||
| 413 | */ |
||
| 414 | public function get_template_vars($name = null) |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Returns an array containing config variables |
||
| 422 | * |
||
| 423 | * @param string $name |
||
| 424 | * |
||
| 425 | * @return array |
||
| 426 | */ |
||
| 427 | public function get_config_vars($name = null) |
||
| 432 | |||
| 433 | /** |
||
| 434 | * load configuration values |
||
| 435 | * |
||
| 436 | * @param string $file |
||
| 437 | * @param string $section |
||
| 438 | * @param string $scope |
||
| 439 | */ |
||
| 440 | public function config_load($file, $section = null, $scope = 'global') |
||
| 445 | |||
| 446 | /** |
||
| 447 | * return a reference to a registered object |
||
| 448 | * |
||
| 449 | * @param string $name |
||
| 450 | * |
||
| 451 | * @return object |
||
| 452 | */ |
||
| 453 | public function get_registered_object($name) |
||
| 458 | |||
| 459 | /** |
||
| 460 | * clear configuration values |
||
| 461 | * |
||
| 462 | * @param string $var |
||
| 463 | */ |
||
| 464 | public function clear_config($var = null) |
||
| 469 | |||
| 470 | /** |
||
| 471 | * trigger Smarty error |
||
| 472 | * |
||
| 473 | * @param string $error_msg |
||
| 474 | * @param integer $error_type |
||
| 475 | */ |
||
| 476 | public function trigger_error($error_msg, $error_type = E_USER_WARNING) |
||
| 481 | |||
| 482 | View Code Duplication | protected function deprecated($function, $file, $line) |
|
| 492 | } |
||
| 493 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.