Complex classes like HTMLPurifier_URI 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_URI, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class HTMLPurifier_URI |
||
|
|
|||
| 12 | { |
||
| 13 | /** |
||
| 14 | * @type string |
||
| 15 | */ |
||
| 16 | public $scheme; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @type string |
||
| 20 | */ |
||
| 21 | public $userinfo; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @type string |
||
| 25 | */ |
||
| 26 | public $host; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @type int |
||
| 30 | */ |
||
| 31 | public $port; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @type string |
||
| 35 | */ |
||
| 36 | public $path; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @type string |
||
| 40 | */ |
||
| 41 | public $query; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @type string |
||
| 45 | */ |
||
| 46 | public $fragment; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @param string $scheme |
||
| 50 | * @param string $userinfo |
||
| 51 | * @param string $host |
||
| 52 | * @param int $port |
||
| 53 | * @param string $path |
||
| 54 | * @param string $query |
||
| 55 | * @param string $fragment |
||
| 56 | * @note Automatically normalizes scheme and port |
||
| 57 | */ |
||
| 58 | public function __construct($scheme, $userinfo, $host, $port, $path, $query, $fragment) |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Retrieves a scheme object corresponding to the URI's scheme/default |
||
| 71 | * @param HTMLPurifier_Config $config |
||
| 72 | * @param HTMLPurifier_Context $context |
||
| 73 | * @return HTMLPurifier_URIScheme Scheme object appropriate for validating this URI |
||
| 74 | */ |
||
| 75 | public function getSchemeObj($config, $context) |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Generic validation method applicable for all schemes. May modify |
||
| 101 | * this URI in order to get it into a compliant form. |
||
| 102 | * @param HTMLPurifier_Config $config |
||
| 103 | * @param HTMLPurifier_Context $context |
||
| 104 | * @return bool True if validation/filtering succeeds, false if failure |
||
| 105 | */ |
||
| 106 | public function validate($config, $context) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Convert URI back to string |
||
| 215 | * @return string URI appropriate for output |
||
| 216 | */ |
||
| 217 | public function toString() |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Returns true if this URL might be considered a 'local' URL given |
||
| 261 | * the current context. This is true when the host is null, or |
||
| 262 | * when it matches the host supplied to the configuration. |
||
| 263 | * |
||
| 264 | * Note that this does not do any scheme checking, so it is mostly |
||
| 265 | * only appropriate for metadata that doesn't care about protocol |
||
| 266 | * security. isBenign is probably what you actually want. |
||
| 267 | * @param HTMLPurifier_Config $config |
||
| 268 | * @param HTMLPurifier_Context $context |
||
| 269 | * @return bool |
||
| 270 | */ |
||
| 271 | public function isLocal($config, $context) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Returns true if this URL should be considered a 'benign' URL, |
||
| 285 | * that is: |
||
| 286 | * |
||
| 287 | * - It is a local URL (isLocal), and |
||
| 288 | * - It has a equal or better level of security |
||
| 289 | * @param HTMLPurifier_Config $config |
||
| 290 | * @param HTMLPurifier_Context $context |
||
| 291 | * @return bool |
||
| 292 | */ |
||
| 293 | public function isBenign($config, $context) |
||
| 312 | } |
||
| 313 | |||
| 315 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.