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. 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_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 | // -- STATIC ---------------------------------------------------------- |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Retrieves or sets the default Lexer as a Prototype Factory. |
||
| 55 | * |
||
| 56 | * By default HTMLPurifier_Lexer_DOMLex will be returned. There are |
||
| 57 | * a few exceptions involving special features that only DirectLex |
||
| 58 | * implements. |
||
| 59 | * |
||
| 60 | * @note The behavior of this class has changed, rather than accepting |
||
| 61 | * a prototype object, it now accepts a configuration object. |
||
| 62 | * To specify your own prototype, set %Core.LexerImpl to it. |
||
| 63 | * This change in behavior de-singletonizes the lexer object. |
||
| 64 | * |
||
| 65 | * @param HTMLPurifier_Config $config |
||
| 66 | * @return HTMLPurifier_Lexer |
||
| 67 | * @throws HTMLPurifier_Exception |
||
| 68 | */ |
||
| 69 | public static function create($config) |
||
| 149 | |||
| 150 | // -- CONVENIENCE MEMBERS --------------------------------------------- |
||
| 151 | |||
| 152 | public function __construct() |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Most common entity to raw value conversion table for special entities. |
||
| 159 | * @type array |
||
| 160 | */ |
||
| 161 | protected $_special_entity2str = |
||
| 162 | array( |
||
| 163 | '"' => '"', |
||
| 164 | '&' => '&', |
||
| 165 | '<' => '<', |
||
| 166 | '>' => '>', |
||
| 167 | ''' => "'", |
||
| 168 | ''' => "'", |
||
| 169 | ''' => "'" |
||
| 170 | ); |
||
| 171 | |||
| 172 | public function parseText($string, $config) { |
||
| 175 | |||
| 176 | public function parseAttr($string, $config) { |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Parses special entities into the proper characters. |
||
| 182 | * |
||
| 183 | * This string will translate escaped versions of the special characters |
||
| 184 | * into the correct ones. |
||
| 185 | * |
||
| 186 | * @param string $string String character data to be parsed. |
||
| 187 | * @return string Parsed character data. |
||
| 188 | */ |
||
| 189 | public function parseData($string, $is_attr, $config) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Lexes an HTML string into tokens. |
||
| 229 | * @param $string String HTML. |
||
| 230 | * @param HTMLPurifier_Config $config |
||
| 231 | * @param HTMLPurifier_Context $context |
||
| 232 | * @return HTMLPurifier_Token[] array representation of HTML. |
||
| 233 | */ |
||
| 234 | public function tokenizeHTML($string, $config, $context) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Translates CDATA sections into regular sections (through escaping). |
||
| 241 | * @param string $string HTML string to process. |
||
| 242 | * @return string HTML with CDATA sections escaped. |
||
| 243 | */ |
||
| 244 | protected static function escapeCDATA($string) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Special CDATA case that is especially convoluted for <script> |
||
| 255 | * @param string $string HTML string to process. |
||
| 256 | * @return string HTML with CDATA sections escaped. |
||
| 257 | */ |
||
| 258 | protected static function escapeCommentedCDATA($string) |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Special Internet Explorer conditional comments should be removed. |
||
| 269 | * @param string $string HTML string to process. |
||
| 270 | * @return string HTML with conditional comments removed. |
||
| 271 | */ |
||
| 272 | protected static function removeIEConditional($string) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Callback function for escapeCDATA() that does the work. |
||
| 283 | * |
||
| 284 | * @warning Though this is public in order to let the callback happen, |
||
| 285 | * calling it directly is not recommended. |
||
| 286 | * @param array $matches PCRE matches array, with index 0 the entire match |
||
| 287 | * and 1 the inside of the CDATA section. |
||
| 288 | * @return string Escaped internals of the CDATA section. |
||
| 289 | */ |
||
| 290 | protected static function CDATACallback($matches) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Takes a piece of HTML and normalizes it by converting entities, fixing |
||
| 298 | * encoding, extracting bits, and other good stuff. |
||
| 299 | * @param string $html HTML. |
||
| 300 | * @param HTMLPurifier_Config $config |
||
| 301 | * @param HTMLPurifier_Context $context |
||
| 302 | * @return string |
||
| 303 | * @todo Consider making protected |
||
| 304 | */ |
||
| 305 | public function normalize($html, $config, $context) |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Takes a string of HTML (fragment or document) and returns the content |
||
| 363 | * @todo Consider making protected |
||
| 364 | */ |
||
| 365 | public function extractBody($html) |
||
| 380 | } |
||
| 381 | |||
| 383 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.