Total Complexity | 70 |
Total Lines | 464 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like HTMLPurifier_HTMLDefinition 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.
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_HTMLDefinition, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class HTMLPurifier_HTMLDefinition extends HTMLPurifier_Definition |
||
27 | { |
||
28 | |||
29 | // FULLY-PUBLIC VARIABLES --------------------------------------------- |
||
30 | |||
31 | /** |
||
32 | * Associative array of element names to HTMLPurifier_ElementDef. |
||
33 | * @type HTMLPurifier_ElementDef[] |
||
34 | */ |
||
35 | public $info = array(); |
||
36 | |||
37 | /** |
||
38 | * Associative array of global attribute name to attribute definition. |
||
39 | * @type array |
||
40 | */ |
||
41 | public $info_global_attr = array(); |
||
42 | |||
43 | /** |
||
44 | * String name of parent element HTML will be going into. |
||
45 | * @type string |
||
46 | */ |
||
47 | public $info_parent = 'div'; |
||
48 | |||
49 | /** |
||
50 | * Definition for parent element, allows parent element to be a |
||
51 | * tag that's not allowed inside the HTML fragment. |
||
52 | * @type HTMLPurifier_ElementDef |
||
53 | */ |
||
54 | public $info_parent_def; |
||
55 | |||
56 | /** |
||
57 | * String name of element used to wrap inline elements in block context. |
||
58 | * @type string |
||
59 | * @note This is rarely used except for BLOCKQUOTEs in strict mode |
||
60 | */ |
||
61 | public $info_block_wrapper = 'p'; |
||
62 | |||
63 | /** |
||
64 | * Associative array of deprecated tag name to HTMLPurifier_TagTransform. |
||
65 | * @type array |
||
66 | */ |
||
67 | public $info_tag_transform = array(); |
||
68 | |||
69 | /** |
||
70 | * Indexed list of HTMLPurifier_AttrTransform to be performed before validation. |
||
71 | * @type HTMLPurifier_AttrTransform[] |
||
72 | */ |
||
73 | public $info_attr_transform_pre = array(); |
||
74 | |||
75 | /** |
||
76 | * Indexed list of HTMLPurifier_AttrTransform to be performed after validation. |
||
77 | * @type HTMLPurifier_AttrTransform[] |
||
78 | */ |
||
79 | public $info_attr_transform_post = array(); |
||
80 | |||
81 | /** |
||
82 | * Nested lookup array of content set name (Block, Inline) to |
||
83 | * element name to whether or not it belongs in that content set. |
||
84 | * @type array |
||
85 | */ |
||
86 | public $info_content_sets = array(); |
||
87 | |||
88 | /** |
||
89 | * Indexed list of HTMLPurifier_Injector to be used. |
||
90 | * @type HTMLPurifier_Injector[] |
||
91 | */ |
||
92 | public $info_injector = array(); |
||
93 | |||
94 | /** |
||
95 | * Doctype object |
||
96 | * @type HTMLPurifier_Doctype |
||
97 | */ |
||
98 | public $doctype; |
||
99 | |||
100 | |||
101 | |||
102 | // RAW CUSTOMIZATION STUFF -------------------------------------------- |
||
103 | |||
104 | /** |
||
105 | * Adds a custom attribute to a pre-existing element |
||
106 | * @note This is strictly convenience, and does not have a corresponding |
||
107 | * method in HTMLPurifier_HTMLModule |
||
108 | * @param string $element_name Element name to add attribute to |
||
109 | * @param string $attr_name Name of attribute |
||
110 | * @param mixed $def Attribute definition, can be string or object, see |
||
111 | * HTMLPurifier_AttrTypes for details |
||
112 | */ |
||
113 | public function addAttribute($element_name, $attr_name, $def) |
||
114 | { |
||
115 | $module = $this->getAnonymousModule(); |
||
116 | if (!isset($module->info[$element_name])) { |
||
117 | $element = $module->addBlankElement($element_name); |
||
118 | } else { |
||
119 | $element = $module->info[$element_name]; |
||
120 | } |
||
121 | $element->attr[$attr_name] = $def; |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Adds a custom element to your HTML definition |
||
126 | * @see HTMLPurifier_HTMLModule::addElement() for detailed |
||
127 | * parameter and return value descriptions. |
||
128 | */ |
||
129 | public function addElement($element_name, $type, $contents, $attr_collections, $attributes = array()) |
||
130 | { |
||
131 | $module = $this->getAnonymousModule(); |
||
132 | // assume that if the user is calling this, the element |
||
133 | // is safe. This may not be a good idea |
||
134 | $element = $module->addElement($element_name, $type, $contents, $attr_collections, $attributes); |
||
135 | return $element; |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * Adds a blank element to your HTML definition, for overriding |
||
140 | * existing behavior |
||
141 | * @param string $element_name |
||
142 | * @return HTMLPurifier_ElementDef |
||
143 | * @see HTMLPurifier_HTMLModule::addBlankElement() for detailed |
||
144 | * parameter and return value descriptions. |
||
145 | */ |
||
146 | public function addBlankElement($element_name) |
||
147 | { |
||
148 | $module = $this->getAnonymousModule(); |
||
149 | $element = $module->addBlankElement($element_name); |
||
150 | return $element; |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * Retrieves a reference to the anonymous module, so you can |
||
155 | * bust out advanced features without having to make your own |
||
156 | * module. |
||
157 | * @return HTMLPurifier_HTMLModule |
||
158 | */ |
||
159 | public function getAnonymousModule() |
||
160 | { |
||
161 | if (!$this->_anonModule) { |
||
162 | $this->_anonModule = new HTMLPurifier_HTMLModule(); |
||
163 | $this->_anonModule->name = 'Anonymous'; |
||
164 | } |
||
165 | return $this->_anonModule; |
||
166 | } |
||
167 | |||
168 | private $_anonModule = null; |
||
169 | |||
170 | // PUBLIC BUT INTERNAL VARIABLES -------------------------------------- |
||
171 | |||
172 | /** |
||
173 | * @type string |
||
174 | */ |
||
175 | public $type = 'HTML'; |
||
176 | |||
177 | /** |
||
178 | * @type HTMLPurifier_HTMLModuleManager |
||
179 | */ |
||
180 | public $manager; |
||
181 | |||
182 | /** |
||
183 | * Performs low-cost, preliminary initialization. |
||
184 | */ |
||
185 | public function __construct() |
||
186 | { |
||
187 | $this->manager = new HTMLPurifier_HTMLModuleManager(); |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * @param HTMLPurifier_Config $config |
||
192 | */ |
||
193 | protected function doSetup($config) |
||
194 | { |
||
195 | $this->processModules($config); |
||
196 | $this->setupConfigStuff($config); |
||
197 | unset($this->manager); |
||
198 | |||
199 | // cleanup some of the element definitions |
||
200 | foreach ($this->info as $k => $v) { |
||
201 | unset($this->info[$k]->content_model); |
||
202 | unset($this->info[$k]->content_model_type); |
||
203 | } |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * Extract out the information from the manager |
||
208 | * @param HTMLPurifier_Config $config |
||
209 | */ |
||
210 | protected function processModules($config) |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * Sets up stuff based on config. We need a better way of doing this. |
||
259 | * @param HTMLPurifier_Config $config |
||
260 | */ |
||
261 | protected function setupConfigStuff($config) |
||
445 | } |
||
446 | } |
||
447 | } |
||
448 | |||
449 | /** |
||
450 | * Parses a TinyMCE-flavored Allowed Elements and Attributes list into |
||
451 | * separate lists for processing. Format is element[attr1|attr2],element2... |
||
452 | * @warning Although it's largely drawn from TinyMCE's implementation, |
||
453 | * it is different, and you'll probably have to modify your lists |
||
454 | * @param array $list String list to parse |
||
455 | * @return array |
||
456 | * @todo Give this its own class, probably static interface |
||
457 | */ |
||
458 | public function parseTinyMCEAllowedList($list) |
||
490 | } |
||
491 | } |
||
494 |
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..