Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 16 | abstract class HTMLPurifier_Injector |
||
| 17 | { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Advisory name of injector, this is for friendly error messages. |
||
| 21 | * @type string |
||
| 22 | */ |
||
| 23 | public $name; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @type HTMLPurifier_HTMLDefinition |
||
| 27 | */ |
||
| 28 | protected $htmlDefinition; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Reference to CurrentNesting variable in Context. This is an array |
||
| 32 | * list of tokens that we are currently "inside" |
||
| 33 | * @type array |
||
| 34 | */ |
||
| 35 | protected $currentNesting; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Reference to current token. |
||
| 39 | * @type HTMLPurifier_Token |
||
| 40 | */ |
||
| 41 | protected $currentToken; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Reference to InputZipper variable in Context. |
||
| 45 | * @type HTMLPurifier_Zipper |
||
| 46 | */ |
||
| 47 | protected $inputZipper; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Array of elements and attributes this injector creates and therefore |
||
| 51 | * need to be allowed by the definition. Takes form of |
||
| 52 | * array('element' => array('attr', 'attr2'), 'element2') |
||
| 53 | * @type array |
||
| 54 | */ |
||
| 55 | public $needed = array(); |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Number of elements to rewind backwards (relative). |
||
| 59 | * @type bool|int |
||
| 60 | */ |
||
| 61 | protected $rewindOffset = false; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Rewind to a spot to re-perform processing. This is useful if you |
||
| 65 | * deleted a node, and now need to see if this change affected any |
||
| 66 | * earlier nodes. Rewinding does not affect other injectors, and can |
||
| 67 | * result in infinite loops if not used carefully. |
||
| 68 | * @param bool|int $offset |
||
| 69 | * @warning HTML Purifier will prevent you from fast-forwarding with this |
||
| 70 | * function. |
||
| 71 | */ |
||
| 72 | public function rewindOffset($offset) |
||
| 73 | { |
||
| 74 | $this->rewindOffset = $offset; |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Retrieves rewind offset, and then unsets it. |
||
| 79 | * @return bool|int |
||
| 80 | */ |
||
| 81 | public function getRewindOffset() |
||
| 82 | { |
||
| 83 | $r = $this->rewindOffset; |
||
| 84 | $this->rewindOffset = false; |
||
| 85 | return $r; |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Prepares the injector by giving it the config and context objects: |
||
| 90 | * this allows references to important variables to be made within |
||
| 91 | * the injector. This function also checks if the HTML environment |
||
| 92 | * will work with the Injector (see checkNeeded()). |
||
| 93 | * @param HTMLPurifier_Config $config |
||
| 94 | * @param HTMLPurifier_Context $context |
||
| 95 | * @return bool|string Boolean false if success, string of missing needed element/attribute if failure |
||
| 96 | */ |
||
| 97 | public function prepare($config, $context) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * This function checks if the HTML environment |
||
| 115 | * will work with the Injector: if p tags are not allowed, the |
||
| 116 | * Auto-Paragraphing injector should not be enabled. |
||
| 117 | * @param HTMLPurifier_Config $config |
||
| 118 | * @return bool|string Boolean false if success, string of missing needed element/attribute if failure |
||
| 119 | */ |
||
| 120 | public function checkNeeded($config) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Tests if the context node allows a certain element |
||
| 144 | * @param string $name Name of element to test for |
||
| 145 | * @return bool True if element is allowed, false if it is not |
||
| 146 | */ |
||
| 147 | public function allowsElement($name) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Iterator function, which starts with the next token and continues until |
||
| 174 | * you reach the end of the input tokens. |
||
| 175 | * @warning Please prevent previous references from interfering with this |
||
| 176 | * functions by setting $i = null beforehand! |
||
| 177 | * @param int $i Current integer index variable for inputTokens |
||
| 178 | * @param HTMLPurifier_Token $current Current token variable. |
||
| 179 | * Do NOT use $token, as that variable is also a reference |
||
| 180 | * @return bool |
||
| 181 | */ |
||
| 182 | View Code Duplication | protected function forward(&$i, &$current) |
|
| 195 | |||
| 196 | /** |
||
| 197 | * Similar to _forward, but accepts a third parameter $nesting (which |
||
| 198 | * should be initialized at 0) and stops when we hit the end tag |
||
| 199 | * for the node $this->inputIndex starts in. |
||
| 200 | * @param int $i Current integer index variable for inputTokens |
||
| 201 | * @param HTMLPurifier_Token $current Current token variable. |
||
| 202 | * Do NOT use $token, as that variable is also a reference |
||
| 203 | * @param int $nesting |
||
| 204 | * @return bool |
||
| 205 | */ |
||
| 206 | protected function forwardUntilEndToken(&$i, &$current, &$nesting) |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Iterator function, starts with the previous token and continues until |
||
| 228 | * you reach the beginning of input tokens. |
||
| 229 | * @warning Please prevent previous references from interfering with this |
||
| 230 | * functions by setting $i = null beforehand! |
||
| 231 | * @param int $i Current integer index variable for inputTokens |
||
| 232 | * @param HTMLPurifier_Token $current Current token variable. |
||
| 233 | * Do NOT use $token, as that variable is also a reference |
||
| 234 | * @return bool |
||
| 235 | */ |
||
| 236 | View Code Duplication | protected function backward(&$i, &$current) |
|
| 249 | |||
| 250 | /** |
||
| 251 | * Handler that is called when a text token is processed |
||
| 252 | */ |
||
| 253 | public function handleText(&$token) |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Handler that is called when a start or empty token is processed |
||
| 259 | */ |
||
| 260 | public function handleElement(&$token) |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Handler that is called when an end token is processed |
||
| 266 | */ |
||
| 267 | public function handleEnd(&$token) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Notifier that is called when an end token is processed |
||
| 274 | * @param HTMLPurifier_Token $token Current token variable. |
||
| 275 | * @note This differs from handlers in that the token is read-only |
||
| 276 | * @deprecated |
||
| 277 | */ |
||
| 278 | public function notifyEnd($token) |
||
| 281 | } |
||
| 282 | |||
| 284 |
This method has been deprecated.