Complex classes like HTMLPurifier_HTMLModuleManager 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_HTMLModuleManager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
3 | class HTMLPurifier_HTMLModuleManager |
||
|
|||
4 | { |
||
5 | |||
6 | /** |
||
7 | * @type HTMLPurifier_DoctypeRegistry |
||
8 | */ |
||
9 | public $doctypes; |
||
10 | |||
11 | /** |
||
12 | * Instance of current doctype. |
||
13 | * @type string |
||
14 | */ |
||
15 | public $doctype; |
||
16 | |||
17 | /** |
||
18 | * @type HTMLPurifier_AttrTypes |
||
19 | */ |
||
20 | public $attrTypes; |
||
21 | |||
22 | /** |
||
23 | * Active instances of modules for the specified doctype are |
||
24 | * indexed, by name, in this array. |
||
25 | * @type HTMLPurifier_HTMLModule[] |
||
26 | */ |
||
27 | public $modules = array(); |
||
28 | |||
29 | /** |
||
30 | * Array of recognized HTMLPurifier_HTMLModule instances, |
||
31 | * indexed by module's class name. This array is usually lazy loaded, but a |
||
32 | * user can overload a module by pre-emptively registering it. |
||
33 | * @type HTMLPurifier_HTMLModule[] |
||
34 | */ |
||
35 | public $registeredModules = array(); |
||
36 | |||
37 | /** |
||
38 | * List of extra modules that were added by the user |
||
39 | * using addModule(). These get unconditionally merged into the current doctype, whatever |
||
40 | * it may be. |
||
41 | * @type HTMLPurifier_HTMLModule[] |
||
42 | */ |
||
43 | public $userModules = array(); |
||
44 | |||
45 | /** |
||
46 | * Associative array of element name to list of modules that have |
||
47 | * definitions for the element; this array is dynamically filled. |
||
48 | * @type array |
||
49 | */ |
||
50 | public $elementLookup = array(); |
||
51 | |||
52 | /** |
||
53 | * List of prefixes we should use for registering small names. |
||
54 | * @type array |
||
55 | */ |
||
56 | public $prefixes = array('HTMLPurifier_HTMLModule_'); |
||
57 | |||
58 | /** |
||
59 | * @type HTMLPurifier_ContentSets |
||
60 | */ |
||
61 | public $contentSets; |
||
62 | |||
63 | /** |
||
64 | * @type HTMLPurifier_AttrCollections |
||
65 | */ |
||
66 | public $attrCollections; |
||
67 | |||
68 | /** |
||
69 | * If set to true, unsafe elements and attributes will be allowed. |
||
70 | * @type bool |
||
71 | */ |
||
72 | public $trusted = false; |
||
73 | |||
74 | public function __construct() |
||
148 | |||
149 | /** |
||
150 | * Registers a module to the recognized module list, useful for |
||
151 | * overloading pre-existing modules. |
||
152 | * @param $module Mixed: string module name, with or without |
||
153 | * HTMLPurifier_HTMLModule prefix, or instance of |
||
154 | * subclass of HTMLPurifier_HTMLModule. |
||
155 | * @param $overload Boolean whether or not to overload previous modules. |
||
156 | * If this is not set, and you do overload a module, |
||
157 | * HTML Purifier will complain with a warning. |
||
158 | * @note This function will not call autoload, you must instantiate |
||
159 | * (and thus invoke) autoload outside the method. |
||
160 | * @note If a string is passed as a module name, different variants |
||
161 | * will be tested in this order: |
||
162 | * - Check for HTMLPurifier_HTMLModule_$name |
||
163 | * - Check all prefixes with $name in order they were added |
||
164 | * - Check for literal object name |
||
165 | * - Throw fatal error |
||
166 | * If your object name collides with an internal class, specify |
||
167 | * your module manually. All modules must have been included |
||
168 | * externally: registerModule will not perform inclusions for you! |
||
169 | */ |
||
170 | public function registerModule($module, $overload = false) |
||
204 | |||
205 | /** |
||
206 | * Adds a module to the current doctype by first registering it, |
||
207 | * and then tacking it on to the active doctype |
||
208 | */ |
||
209 | public function addModule($module) |
||
217 | |||
218 | /** |
||
219 | * Adds a class prefix that registerModule() will use to resolve a |
||
220 | * string name to a concrete class |
||
221 | */ |
||
222 | public function addPrefix($prefix) |
||
226 | |||
227 | /** |
||
228 | * Performs processing on modules, after being called you may |
||
229 | * use getElement() and getElements() |
||
230 | * @param HTMLPurifier_Config $config |
||
231 | */ |
||
232 | public function setup($config) |
||
325 | |||
326 | /** |
||
327 | * Takes a module and adds it to the active module collection, |
||
328 | * registering it if necessary. |
||
329 | */ |
||
330 | public function processModule($module) |
||
337 | |||
338 | /** |
||
339 | * Retrieves merged element definitions. |
||
340 | * @return Array of HTMLPurifier_ElementDef |
||
341 | */ |
||
342 | public function getElements() |
||
368 | |||
369 | /** |
||
370 | * Retrieves a single merged element definition |
||
371 | * @param string $name Name of element |
||
372 | * @param bool $trusted Boolean trusted overriding parameter: set to true |
||
373 | * if you want the full version of an element |
||
374 | * @return HTMLPurifier_ElementDef Merged HTMLPurifier_ElementDef |
||
375 | * @note You may notice that modules are getting iterated over twice (once |
||
376 | * in getElements() and once here). This |
||
377 | * is because |
||
378 | */ |
||
379 | public function getElement($name, $trusted = null) |
||
457 | } |
||
458 | |||
460 |
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.