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 |
||
| 10 | class HTMLPurifier_LanguageFactory |
||
|
|
|||
| 11 | { |
||
| 12 | |||
| 13 | /** |
||
| 14 | * Cache of language code information used to load HTMLPurifier_Language objects. |
||
| 15 | * Structure is: $factory->cache[$language_code][$key] = $value |
||
| 16 | * @type array |
||
| 17 | */ |
||
| 18 | public $cache; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Valid keys in the HTMLPurifier_Language object. Designates which |
||
| 22 | * variables to slurp out of a message file. |
||
| 23 | * @type array |
||
| 24 | */ |
||
| 25 | public $keys = array('fallback', 'messages', 'errorNames'); |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Instance to validate language codes. |
||
| 29 | * @type HTMLPurifier_AttrDef_Lang |
||
| 30 | * |
||
| 31 | */ |
||
| 32 | protected $validator; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Cached copy of dirname(__FILE__), directory of current file without |
||
| 36 | * trailing slash. |
||
| 37 | * @type string |
||
| 38 | */ |
||
| 39 | protected $dir; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Keys whose contents are a hash map and can be merged. |
||
| 43 | * @type array |
||
| 44 | */ |
||
| 45 | protected $mergeable_keys_map = array('messages' => true, 'errorNames' => true); |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Keys whose contents are a list and can be merged. |
||
| 49 | * @value array lookup |
||
| 50 | */ |
||
| 51 | protected $mergeable_keys_list = array(); |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Retrieve sole instance of the factory. |
||
| 55 | * @param HTMLPurifier_LanguageFactory $prototype Optional prototype to overload sole instance with, |
||
| 56 | * or bool true to reset to default factory. |
||
| 57 | * @return HTMLPurifier_LanguageFactory |
||
| 58 | */ |
||
| 59 | View Code Duplication | public static function instance($prototype = null) |
|
| 70 | |||
| 71 | /** |
||
| 72 | * Sets up the singleton, much like a constructor |
||
| 73 | * @note Prevents people from getting this outside of the singleton |
||
| 74 | */ |
||
| 75 | public function setup() |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Creates a language object, handles class fallbacks |
||
| 83 | * @param HTMLPurifier_Config $config |
||
| 84 | * @param HTMLPurifier_Context $context |
||
| 85 | * @param bool|string $code Code to override configuration with. Private parameter. |
||
| 86 | * @return HTMLPurifier_Language |
||
| 87 | */ |
||
| 88 | public function create($config, $context, $code = false) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Returns the fallback language for language |
||
| 132 | * @note Loads the original language into cache |
||
| 133 | * @param string $code language code |
||
| 134 | * @return string|bool |
||
| 135 | */ |
||
| 136 | public function getFallbackFor($code) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Loads language into the cache, handles message file and fallbacks |
||
| 144 | * @param string $code language code |
||
| 145 | */ |
||
| 146 | public function loadLanguage($code) |
||
| 207 | } |
||
| 208 | |||
| 210 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.