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 | /** |
||
| 173 | * Parses special entities into the proper characters. |
||
| 174 | * |
||
| 175 | * This string will translate escaped versions of the special characters |
||
| 176 | * into the correct ones. |
||
| 177 | * |
||
| 178 | * @warning |
||
| 179 | * You should be able to treat the output of this function as |
||
| 180 | * completely parsed, but that's only because all other entities should |
||
| 181 | * have been handled previously in substituteNonSpecialEntities() |
||
| 182 | * |
||
| 183 | * @param string $string String character data to be parsed. |
||
| 184 | * @return string Parsed character data. |
||
| 185 | */ |
||
| 186 | public function parseData($string) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Lexes an HTML string into tokens. |
||
| 218 | * @param $string String HTML. |
||
| 219 | * @param HTMLPurifier_Config $config |
||
| 220 | * @param HTMLPurifier_Context $context |
||
| 221 | * @return HTMLPurifier_Token[] array representation of HTML. |
||
| 222 | */ |
||
| 223 | public function tokenizeHTML($string, $config, $context) |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Translates CDATA sections into regular sections (through escaping). |
||
| 230 | * @param string $string HTML string to process. |
||
| 231 | * @return string HTML with CDATA sections escaped. |
||
| 232 | */ |
||
| 233 | protected static function escapeCDATA($string) |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Special CDATA case that is especially convoluted for <script> |
||
| 244 | * @param string $string HTML string to process. |
||
| 245 | * @return string HTML with CDATA sections escaped. |
||
| 246 | */ |
||
| 247 | protected static function escapeCommentedCDATA($string) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Special Internet Explorer conditional comments should be removed. |
||
| 258 | * @param string $string HTML string to process. |
||
| 259 | * @return string HTML with conditional comments removed. |
||
| 260 | */ |
||
| 261 | protected static function removeIEConditional($string) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Callback function for escapeCDATA() that does the work. |
||
| 272 | * |
||
| 273 | * @warning Though this is public in order to let the callback happen, |
||
| 274 | * calling it directly is not recommended. |
||
| 275 | * @param array $matches PCRE matches array, with index 0 the entire match |
||
| 276 | * and 1 the inside of the CDATA section. |
||
| 277 | * @return string Escaped internals of the CDATA section. |
||
| 278 | */ |
||
| 279 | protected static function CDATACallback($matches) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Takes a piece of HTML and normalizes it by converting entities, fixing |
||
| 287 | * encoding, extracting bits, and other good stuff. |
||
| 288 | * @param string $html HTML. |
||
| 289 | * @param HTMLPurifier_Config $config |
||
| 290 | * @param HTMLPurifier_Context $context |
||
| 291 | * @return string |
||
| 292 | * @todo Consider making protected |
||
| 293 | */ |
||
| 294 | public function normalize($html, $config, $context) |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Takes a string of HTML (fragment or document) and returns the content |
||
| 343 | * @todo Consider making protected |
||
| 344 | */ |
||
| 345 | public function extractBody($html) |
||
| 360 | } |
||
| 361 | |||
| 363 |
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.