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 HTMLPurifier_Config 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 HTMLPurifier_Config, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class HTMLPurifier_Config |
||
|
|
|||
| 18 | { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * HTML Purifier's version |
||
| 22 | * @type string |
||
| 23 | */ |
||
| 24 | public $version = '4.7.0'; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Whether or not to automatically finalize |
||
| 28 | * the object if a read operation is done. |
||
| 29 | * @type bool |
||
| 30 | */ |
||
| 31 | public $autoFinalize = true; |
||
| 32 | |||
| 33 | // protected member variables |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Namespace indexed array of serials for specific namespaces. |
||
| 37 | * @see getSerial() for more info. |
||
| 38 | * @type string[] |
||
| 39 | */ |
||
| 40 | protected $serials = array(); |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Serial for entire configuration object. |
||
| 44 | * @type string |
||
| 45 | */ |
||
| 46 | protected $serial; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Parser for variables. |
||
| 50 | * @type HTMLPurifier_VarParser_Flexible |
||
| 51 | */ |
||
| 52 | protected $parser = null; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Reference HTMLPurifier_ConfigSchema for value checking. |
||
| 56 | * @type HTMLPurifier_ConfigSchema |
||
| 57 | * @note This is public for introspective purposes. Please don't |
||
| 58 | * abuse! |
||
| 59 | */ |
||
| 60 | public $def; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Indexed array of definitions. |
||
| 64 | * @type HTMLPurifier_Definition[] |
||
| 65 | */ |
||
| 66 | protected $definitions; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Whether or not config is finalized. |
||
| 70 | * @type bool |
||
| 71 | */ |
||
| 72 | protected $finalized = false; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Property list containing configuration directives. |
||
| 76 | * @type array |
||
| 77 | */ |
||
| 78 | protected $plist; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Whether or not a set is taking place due to an alias lookup. |
||
| 82 | * @type bool |
||
| 83 | */ |
||
| 84 | private $aliasMode; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Set to false if you do not want line and file numbers in errors. |
||
| 88 | * (useful when unit testing). This will also compress some errors |
||
| 89 | * and exceptions. |
||
| 90 | * @type bool |
||
| 91 | */ |
||
| 92 | public $chatty = true; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Current lock; only gets to this namespace are allowed. |
||
| 96 | * @type string |
||
| 97 | */ |
||
| 98 | private $lock; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Constructor |
||
| 102 | * @param HTMLPurifier_ConfigSchema $definition ConfigSchema that defines |
||
| 103 | * what directives are allowed. |
||
| 104 | * @param HTMLPurifier_PropertyList $parent |
||
| 105 | */ |
||
| 106 | public function __construct($definition, $parent = null) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Convenience constructor that creates a config object based on a mixed var |
||
| 116 | * @param mixed $config Variable that defines the state of the config |
||
| 117 | * object. Can be: a HTMLPurifier_Config() object, |
||
| 118 | * an array of directives based on loadArray(), |
||
| 119 | * or a string filename of an ini file. |
||
| 120 | * @param HTMLPurifier_ConfigSchema $schema Schema object |
||
| 121 | * @return HTMLPurifier_Config Configured object |
||
| 122 | */ |
||
| 123 | public static function create($config, $schema = null) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Creates a new config object that inherits from a previous one. |
||
| 142 | * @param HTMLPurifier_Config $config Configuration object to inherit from. |
||
| 143 | * @return HTMLPurifier_Config object with $config as its parent. |
||
| 144 | */ |
||
| 145 | public static function inherit(HTMLPurifier_Config $config) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Convenience constructor that creates a default configuration object. |
||
| 152 | * @return HTMLPurifier_Config default object. |
||
| 153 | */ |
||
| 154 | public static function createDefault() |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Retrieves a value from the configuration. |
||
| 163 | * |
||
| 164 | * @param string $key String key |
||
| 165 | * @param mixed $a |
||
| 166 | * |
||
| 167 | * @return mixed |
||
| 168 | */ |
||
| 169 | public function get($key, $a = null) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Retrieves an array of directives to values from a given namespace |
||
| 215 | * |
||
| 216 | * @param string $namespace String namespace |
||
| 217 | * |
||
| 218 | * @return array |
||
| 219 | */ |
||
| 220 | public function getBatch($namespace) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Returns a SHA-1 signature of a segment of the configuration object |
||
| 239 | * that uniquely identifies that particular configuration |
||
| 240 | * |
||
| 241 | * @param string $namespace Namespace to get serial for |
||
| 242 | * |
||
| 243 | * @return string |
||
| 244 | * @note Revision is handled specially and is removed from the batch |
||
| 245 | * before processing! |
||
| 246 | */ |
||
| 247 | public function getBatchSerial($namespace) |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Returns a SHA-1 signature for the entire configuration object |
||
| 259 | * that uniquely identifies that particular configuration |
||
| 260 | * |
||
| 261 | * @return string |
||
| 262 | */ |
||
| 263 | public function getSerial() |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Retrieves all directives, organized by namespace |
||
| 273 | * |
||
| 274 | * @warning This is a pretty inefficient function, avoid if you can |
||
| 275 | */ |
||
| 276 | public function getAll() |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Sets a value to configuration. |
||
| 291 | * |
||
| 292 | * @param string $key key |
||
| 293 | * @param mixed $value value |
||
| 294 | * @param mixed $a |
||
| 295 | */ |
||
| 296 | public function set($key, $value, $a = null) |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Convenience function for error reporting |
||
| 385 | * |
||
| 386 | * @param array $lookup |
||
| 387 | * |
||
| 388 | * @return string |
||
| 389 | */ |
||
| 390 | private function _listify($lookup) |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Retrieves object reference to the HTML definition. |
||
| 401 | * |
||
| 402 | * @param bool $raw Return a copy that has not been setup yet. Must be |
||
| 403 | * called before it's been setup, otherwise won't work. |
||
| 404 | * @param bool $optimized If true, this method may return null, to |
||
| 405 | * indicate that a cached version of the modified |
||
| 406 | * definition object is available and no further edits |
||
| 407 | * are necessary. Consider using |
||
| 408 | * maybeGetRawHTMLDefinition, which is more explicitly |
||
| 409 | * named, instead. |
||
| 410 | * |
||
| 411 | * @return HTMLPurifier_HTMLDefinition |
||
| 412 | */ |
||
| 413 | public function getHTMLDefinition($raw = false, $optimized = false) |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Retrieves object reference to the CSS definition |
||
| 420 | * |
||
| 421 | * @param bool $raw Return a copy that has not been setup yet. Must be |
||
| 422 | * called before it's been setup, otherwise won't work. |
||
| 423 | * @param bool $optimized If true, this method may return null, to |
||
| 424 | * indicate that a cached version of the modified |
||
| 425 | * definition object is available and no further edits |
||
| 426 | * are necessary. Consider using |
||
| 427 | * maybeGetRawCSSDefinition, which is more explicitly |
||
| 428 | * named, instead. |
||
| 429 | * |
||
| 430 | * @return HTMLPurifier_CSSDefinition |
||
| 431 | */ |
||
| 432 | public function getCSSDefinition($raw = false, $optimized = false) |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Retrieves object reference to the URI definition |
||
| 439 | * |
||
| 440 | * @param bool $raw Return a copy that has not been setup yet. Must be |
||
| 441 | * called before it's been setup, otherwise won't work. |
||
| 442 | * @param bool $optimized If true, this method may return null, to |
||
| 443 | * indicate that a cached version of the modified |
||
| 444 | * definition object is available and no further edits |
||
| 445 | * are necessary. Consider using |
||
| 446 | * maybeGetRawURIDefinition, which is more explicitly |
||
| 447 | * named, instead. |
||
| 448 | * |
||
| 449 | * @return HTMLPurifier_URIDefinition |
||
| 450 | */ |
||
| 451 | public function getURIDefinition($raw = false, $optimized = false) |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Retrieves a definition |
||
| 458 | * |
||
| 459 | * @param string $type Type of definition: HTML, CSS, etc |
||
| 460 | * @param bool $raw Whether or not definition should be returned raw |
||
| 461 | * @param bool $optimized Only has an effect when $raw is true. Whether |
||
| 462 | * or not to return null if the result is already present in |
||
| 463 | * the cache. This is off by default for backwards |
||
| 464 | * compatibility reasons, but you need to do things this |
||
| 465 | * way in order to ensure that caching is done properly. |
||
| 466 | * Check out enduser-customize.html for more details. |
||
| 467 | * We probably won't ever change this default, as much as the |
||
| 468 | * maybe semantics is the "right thing to do." |
||
| 469 | * |
||
| 470 | * @throws HTMLPurifier_Exception |
||
| 471 | * @return HTMLPurifier_Definition |
||
| 472 | */ |
||
| 473 | public function getDefinition($type, $raw = false, $optimized = false) |
||
| 617 | |||
| 618 | /** |
||
| 619 | * Initialise definition |
||
| 620 | * |
||
| 621 | * @param string $type What type of definition to create |
||
| 622 | * |
||
| 623 | * @return HTMLPurifier_CSSDefinition|HTMLPurifier_HTMLDefinition|HTMLPurifier_URIDefinition |
||
| 624 | * @throws HTMLPurifier_Exception |
||
| 625 | */ |
||
| 626 | private function initDefinition($type) |
||
| 643 | |||
| 644 | public function maybeGetRawDefinition($name) |
||
| 648 | |||
| 649 | /** |
||
| 650 | * @return HTMLPurifier_HTMLDefinition |
||
| 651 | */ |
||
| 652 | public function maybeGetRawHTMLDefinition() |
||
| 656 | |||
| 657 | /** |
||
| 658 | * @return HTMLPurifier_CSSDefinition |
||
| 659 | */ |
||
| 660 | public function maybeGetRawCSSDefinition() |
||
| 664 | |||
| 665 | /** |
||
| 666 | * @return HTMLPurifier_URIDefinition |
||
| 667 | */ |
||
| 668 | public function maybeGetRawURIDefinition() |
||
| 672 | |||
| 673 | /** |
||
| 674 | * Loads configuration values from an array with the following structure: |
||
| 675 | * Namespace.Directive => Value |
||
| 676 | * |
||
| 677 | * @param array $config_array Configuration associative array |
||
| 678 | */ |
||
| 679 | public function loadArray($config_array) |
||
| 697 | |||
| 698 | /** |
||
| 699 | * Returns a list of array(namespace, directive) for all directives |
||
| 700 | * that are allowed in a web-form context as per an allowed |
||
| 701 | * namespaces/directives list. |
||
| 702 | * |
||
| 703 | * @param array $allowed List of allowed namespaces/directives |
||
| 704 | * @param HTMLPurifier_ConfigSchema $schema Schema to use, if not global copy |
||
| 705 | * |
||
| 706 | * @return array |
||
| 707 | */ |
||
| 708 | public static function getAllowedDirectivesForForm($allowed, $schema = null) |
||
| 755 | |||
| 756 | /** |
||
| 757 | * Loads configuration values from $_GET/$_POST that were posted |
||
| 758 | * via ConfigForm |
||
| 759 | * |
||
| 760 | * @param array $array $_GET or $_POST array to import |
||
| 761 | * @param string|bool $index Index/name that the config variables are in |
||
| 762 | * @param array|bool $allowed List of allowed namespaces/directives |
||
| 763 | * @param bool $mq_fix Boolean whether or not to enable magic quotes fix |
||
| 764 | * @param HTMLPurifier_ConfigSchema $schema Schema to use, if not global copy |
||
| 765 | * |
||
| 766 | * @return mixed |
||
| 767 | */ |
||
| 768 | public static function loadArrayFromForm($array, $index = false, $allowed = true, $mq_fix = true, $schema = null) |
||
| 774 | |||
| 775 | /** |
||
| 776 | * Merges in configuration values from $_GET/$_POST to object. NOT STATIC. |
||
| 777 | * |
||
| 778 | * @param array $array $_GET or $_POST array to import |
||
| 779 | * @param string|bool $index Index/name that the config variables are in |
||
| 780 | * @param array|bool $allowed List of allowed namespaces/directives |
||
| 781 | * @param bool $mq_fix Boolean whether or not to enable magic quotes fix |
||
| 782 | */ |
||
| 783 | public function mergeArrayFromForm($array, $index = false, $allowed = true, $mq_fix = true) |
||
| 788 | |||
| 789 | /** |
||
| 790 | * Prepares an array from a form into something usable for the more |
||
| 791 | * strict parts of HTMLPurifier_Config |
||
| 792 | * |
||
| 793 | * @param array $array $_GET or $_POST array to import |
||
| 794 | * @param string|bool $index Index/name that the config variables are in |
||
| 795 | * @param array|bool $allowed List of allowed namespaces/directives |
||
| 796 | * @param bool $mq_fix Boolean whether or not to enable magic quotes fix |
||
| 797 | * @param HTMLPurifier_ConfigSchema $schema Schema to use, if not global copy |
||
| 798 | * |
||
| 799 | * @return array |
||
| 800 | */ |
||
| 801 | public static function prepareArrayFromForm($array, $index = false, $allowed = true, $mq_fix = true, $schema = null) |
||
| 825 | |||
| 826 | /** |
||
| 827 | * Loads configuration values from an ini file |
||
| 828 | * |
||
| 829 | * @param string $filename Name of ini file |
||
| 830 | */ |
||
| 831 | public function loadIni($filename) |
||
| 839 | |||
| 840 | /** |
||
| 841 | * Checks whether or not the configuration object is finalized. |
||
| 842 | * |
||
| 843 | * @param string|bool $error String error message, or false for no error |
||
| 844 | * |
||
| 845 | * @return bool |
||
| 846 | */ |
||
| 847 | public function isFinalized($error = false) |
||
| 854 | |||
| 855 | /** |
||
| 856 | * Finalizes configuration only if auto finalize is on and not |
||
| 857 | * already finalized |
||
| 858 | */ |
||
| 859 | public function autoFinalize() |
||
| 867 | |||
| 868 | /** |
||
| 869 | * Finalizes a configuration object, prohibiting further change |
||
| 870 | */ |
||
| 871 | public function finalize() |
||
| 876 | |||
| 877 | /** |
||
| 878 | * Produces a nicely formatted error message by supplying the |
||
| 879 | * stack frame information OUTSIDE of HTMLPurifier_Config. |
||
| 880 | * |
||
| 881 | * @param string $msg An error message |
||
| 882 | * @param int $no An error number |
||
| 883 | */ |
||
| 884 | protected function triggerError($msg, $no) |
||
| 903 | |||
| 904 | /** |
||
| 905 | * Returns a serialized form of the configuration object that can |
||
| 906 | * be reconstituted. |
||
| 907 | * |
||
| 908 | * @return string |
||
| 909 | */ |
||
| 910 | public function serialize() |
||
| 917 | |||
| 918 | } |
||
| 919 | |||
| 921 |
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.