Total Complexity | 49 |
Total Lines | 326 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like HTMLPurifier_Lexer 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_Lexer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
42 | class HTMLPurifier_Lexer |
||
43 | { |
||
44 | |||
45 | /** |
||
46 | * Whether or not this lexer implements line-number/column-number tracking. |
||
47 | * If it does, set to true. |
||
48 | */ |
||
49 | public $tracksLineNumbers = false; |
||
50 | |||
51 | /** |
||
52 | * @type HTMLPurifier_EntityParser |
||
53 | */ |
||
54 | private $_entity_parser; |
||
55 | |||
56 | // -- STATIC ---------------------------------------------------------- |
||
57 | |||
58 | /** |
||
59 | * Retrieves or sets the default Lexer as a Prototype Factory. |
||
60 | * |
||
61 | * By default HTMLPurifier_Lexer_DOMLex will be returned. There are |
||
62 | * a few exceptions involving special features that only DirectLex |
||
63 | * implements. |
||
64 | * |
||
65 | * @note The behavior of this class has changed, rather than accepting |
||
66 | * a prototype object, it now accepts a configuration object. |
||
67 | * To specify your own prototype, set %Core.LexerImpl to it. |
||
68 | * This change in behavior de-singletonizes the lexer object. |
||
69 | * |
||
70 | * @param HTMLPurifier_Config $config |
||
71 | * @return HTMLPurifier_Lexer |
||
72 | * @throws HTMLPurifier_Exception |
||
73 | */ |
||
74 | public static function create($config) |
||
152 | |||
153 | } |
||
154 | |||
155 | // -- CONVENIENCE MEMBERS --------------------------------------------- |
||
156 | |||
157 | public function __construct() |
||
158 | { |
||
159 | $this->_entity_parser = new HTMLPurifier_EntityParser(); |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * Most common entity to raw value conversion table for special entities. |
||
164 | * @type array |
||
165 | */ |
||
166 | protected $_special_entity2str = |
||
167 | array( |
||
168 | '"' => '"', |
||
169 | '&' => '&', |
||
170 | '<' => '<', |
||
171 | '>' => '>', |
||
172 | ''' => "'", |
||
173 | ''' => "'", |
||
174 | ''' => "'" |
||
175 | ); |
||
176 | |||
177 | public function parseText($string, $config) { |
||
178 | return $this->parseData($string, false, $config); |
||
179 | } |
||
180 | |||
181 | public function parseAttr($string, $config) { |
||
182 | return $this->parseData($string, true, $config); |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * Parses special entities into the proper characters. |
||
187 | * |
||
188 | * This string will translate escaped versions of the special characters |
||
189 | * into the correct ones. |
||
190 | * |
||
191 | * @param string $string String character data to be parsed. |
||
192 | * @return string Parsed character data. |
||
193 | */ |
||
194 | public function parseData($string, $is_attr, $config) |
||
195 | { |
||
196 | // following functions require at least one character |
||
197 | if ($string === '') { |
||
198 | return ''; |
||
199 | } |
||
200 | |||
201 | // subtracts amps that cannot possibly be escaped |
||
202 | $num_amp = substr_count($string, '&') - substr_count($string, '& ') - |
||
203 | ($string[strlen($string) - 1] === '&' ? 1 : 0); |
||
204 | |||
205 | if (!$num_amp) { |
||
206 | return $string; |
||
207 | } // abort if no entities |
||
208 | $num_esc_amp = substr_count($string, '&'); |
||
209 | $string = strtr($string, $this->_special_entity2str); |
||
210 | |||
211 | // code duplication for sake of optimization, see above |
||
212 | $num_amp_2 = substr_count($string, '&') - substr_count($string, '& ') - |
||
213 | ($string[strlen($string) - 1] === '&' ? 1 : 0); |
||
214 | |||
215 | if ($num_amp_2 <= $num_esc_amp) { |
||
216 | return $string; |
||
217 | } |
||
218 | |||
219 | // hmm... now we have some uncommon entities. Use the callback. |
||
220 | if ($config->get('Core.LegacyEntityDecoder')) { |
||
221 | $string = $this->_entity_parser->substituteSpecialEntities($string); |
||
222 | } else { |
||
223 | if ($is_attr) { |
||
224 | $string = $this->_entity_parser->substituteAttrEntities($string); |
||
225 | } else { |
||
226 | $string = $this->_entity_parser->substituteTextEntities($string); |
||
227 | } |
||
228 | } |
||
229 | return $string; |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * Lexes an HTML string into tokens. |
||
234 | * @param $string String HTML. |
||
235 | * @param HTMLPurifier_Config $config |
||
236 | * @param HTMLPurifier_Context $context |
||
237 | * @return HTMLPurifier_Token[] array representation of HTML. |
||
238 | */ |
||
239 | public function tokenizeHTML($string, $config, $context) |
||
242 | } |
||
243 | |||
244 | /** |
||
245 | * Translates CDATA sections into regular sections (through escaping). |
||
246 | * @param string $string HTML string to process. |
||
247 | * @return string HTML with CDATA sections escaped. |
||
248 | */ |
||
249 | protected static function escapeCDATA($string) |
||
255 | ); |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * Special CDATA case that is especially convoluted for <script> |
||
260 | * @param string $string HTML string to process. |
||
261 | * @return string HTML with CDATA sections escaped. |
||
262 | */ |
||
263 | protected static function escapeCommentedCDATA($string) |
||
269 | ); |
||
270 | } |
||
271 | |||
272 | /** |
||
273 | * Callback function for escapeCDATA() that does the work. |
||
274 | * |
||
275 | * @warning Though this is public in order to let the callback happen, |
||
276 | * calling it directly is not recommended. |
||
277 | * @param array $matches PCRE matches array, with index 0 the entire match |
||
278 | * and 1 the inside of the CDATA section. |
||
279 | * @return string Escaped internals of the CDATA section. |
||
280 | */ |
||
281 | protected static function CDATACallback($matches) |
||
285 | } |
||
286 | |||
287 | /** |
||
288 | * Takes a piece of HTML and normalizes it by converting entities, fixing |
||
289 | * encoding, extracting bits, and other good stuff. |
||
290 | * @param string $html HTML. |
||
291 | * @param HTMLPurifier_Config $config |
||
292 | * @param HTMLPurifier_Context $context |
||
293 | * @return string |
||
294 | * @todo Consider making protected |
||
295 | */ |
||
296 | public function normalize($html, $config, $context) |
||
348 | } |
||
349 | |||
350 | /** |
||
351 | * Takes a string of HTML (fragment or document) and returns the content |
||
352 | * @todo Consider making protected |
||
353 | */ |
||
354 | public function extractBody($html) |
||
372 |