Complex classes like Swift_Mime_SimpleHeaderSet 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 Swift_Mime_SimpleHeaderSet, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class Swift_Mime_SimpleHeaderSet implements Swift_Mime_HeaderSet |
||
|
|
|||
| 17 | { |
||
| 18 | /** |
||
| 19 | * HeaderFactory |
||
| 20 | * |
||
| 21 | * @var Swift_Mime_HeaderFactory |
||
| 22 | */ |
||
| 23 | private $_factory; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Collection of set Headers |
||
| 27 | * |
||
| 28 | * @var array[] |
||
| 29 | */ |
||
| 30 | private $_headers = array(); |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Field ordering details |
||
| 34 | * |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | private $_order = array(); |
||
| 38 | |||
| 39 | /** |
||
| 40 | * List of fields which are required to be displayed |
||
| 41 | * |
||
| 42 | * @var array |
||
| 43 | */ |
||
| 44 | private $_required = array(); |
||
| 45 | |||
| 46 | /** |
||
| 47 | * The charset used by Headers |
||
| 48 | */ |
||
| 49 | private $_charset; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Create a new SimpleHeaderSet with the given $factory. |
||
| 53 | * |
||
| 54 | * @param Swift_Mime_HeaderFactory $factory |
||
| 55 | * @param string $charset |
||
| 56 | */ |
||
| 57 | 190 | public function __construct(Swift_Mime_HeaderFactory $factory, $charset = null) |
|
| 64 | |||
| 65 | /** |
||
| 66 | * Set the charset used by these headers. |
||
| 67 | * |
||
| 68 | * @param string $charset |
||
| 69 | */ |
||
| 70 | 132 | public function setCharset($charset) |
|
| 76 | |||
| 77 | /** |
||
| 78 | * Add a new Mailbox Header with a list of $addresses. |
||
| 79 | * |
||
| 80 | * @param string $name |
||
| 81 | * @param array|string $addresses |
||
| 82 | */ |
||
| 83 | 111 | public function addMailboxHeader($name, $addresses = null) |
|
| 88 | |||
| 89 | /** |
||
| 90 | * Add a new Date header using $timestamp (UNIX time). |
||
| 91 | * |
||
| 92 | * @param string $name |
||
| 93 | * @param int $timestamp |
||
| 94 | */ |
||
| 95 | 111 | public function addDateHeader($name, $timestamp = null) |
|
| 100 | |||
| 101 | /** |
||
| 102 | * Add a new basic text header with $name and $value. |
||
| 103 | * |
||
| 104 | * @param string $name |
||
| 105 | * @param string $value |
||
| 106 | */ |
||
| 107 | 157 | public function addTextHeader($name, $value = null) |
|
| 112 | |||
| 113 | /** |
||
| 114 | * Add a new ParameterizedHeader with $name, $value and $params. |
||
| 115 | * |
||
| 116 | * @param string $name |
||
| 117 | * @param string $value |
||
| 118 | * @param array $params |
||
| 119 | */ |
||
| 120 | 145 | public function addParameterizedHeader($name, $value = null, $params = array()) |
|
| 124 | |||
| 125 | /** |
||
| 126 | * Add a new ID header for Message-ID or Content-ID. |
||
| 127 | * |
||
| 128 | * @param string $name |
||
| 129 | * @param string|array $ids |
||
| 130 | */ |
||
| 131 | 140 | public function addIdHeader($name, $ids = null) |
|
| 135 | |||
| 136 | /** |
||
| 137 | * Add a new Path header with an address (path) in it. |
||
| 138 | * |
||
| 139 | * @param string $name |
||
| 140 | * @param string $path |
||
| 141 | */ |
||
| 142 | 32 | public function addPathHeader($name, $path = null) |
|
| 146 | |||
| 147 | /** |
||
| 148 | * Returns true if at least one header with the given $name exists. |
||
| 149 | * |
||
| 150 | * If multiple headers match, the actual one may be specified by $index. |
||
| 151 | * |
||
| 152 | * @param string $name |
||
| 153 | * @param int $index |
||
| 154 | * |
||
| 155 | * @return bool |
||
| 156 | */ |
||
| 157 | 171 | public function has($name, $index = 0) |
|
| 163 | |||
| 164 | /** |
||
| 165 | * Set a header in the HeaderSet. |
||
| 166 | * |
||
| 167 | * The header may be a previously fetched header via {@link get()} or it may |
||
| 168 | * be one that has been created separately. |
||
| 169 | * |
||
| 170 | * If $index is specified, the header will be inserted into the set at this |
||
| 171 | * offset. |
||
| 172 | * |
||
| 173 | * @param Swift_Mime_Header $header |
||
| 174 | * @param int $index |
||
| 175 | */ |
||
| 176 | 5 | public function set(Swift_Mime_Header $header, $index = 0) |
|
| 180 | |||
| 181 | /** |
||
| 182 | * Get the header with the given $name. |
||
| 183 | * |
||
| 184 | * If multiple headers match, the actual one may be specified by $index. |
||
| 185 | * Returns NULL if none present. |
||
| 186 | * |
||
| 187 | * @param string $name |
||
| 188 | * @param int $index |
||
| 189 | * |
||
| 190 | * @return Swift_Mime_Header |
||
| 191 | */ |
||
| 192 | 147 | public function get($name, $index = 0) |
|
| 200 | |||
| 201 | /** |
||
| 202 | * Get all headers with the given $name. |
||
| 203 | * |
||
| 204 | * @param string $name |
||
| 205 | * |
||
| 206 | * @return array |
||
| 207 | */ |
||
| 208 | 21 | public function getAll($name = null) |
|
| 226 | |||
| 227 | /** |
||
| 228 | * Return the name of all Headers. |
||
| 229 | * |
||
| 230 | * @return array |
||
| 231 | */ |
||
| 232 | 5 | public function listAll() |
|
| 241 | |||
| 242 | /** |
||
| 243 | * Remove the header with the given $name if it's set. |
||
| 244 | * |
||
| 245 | * If multiple headers match, the actual one may be specified by $index. |
||
| 246 | * |
||
| 247 | * @param string $name |
||
| 248 | * @param int $index |
||
| 249 | */ |
||
| 250 | 48 | public function remove($name, $index = 0) |
|
| 261 | |||
| 262 | /** |
||
| 263 | * Remove all headers with the given $name. |
||
| 264 | * |
||
| 265 | * @param string $name |
||
| 266 | */ |
||
| 267 | 51 | public function removeAll($name) |
|
| 272 | |||
| 273 | /** |
||
| 274 | * Create a new instance of this HeaderSet. |
||
| 275 | * |
||
| 276 | * @return Swift_Mime_HeaderSet |
||
| 277 | */ |
||
| 278 | 26 | public function newInstance() |
|
| 282 | |||
| 283 | /** |
||
| 284 | * Define a list of Header names as an array in the correct order. |
||
| 285 | * |
||
| 286 | * These Headers will be output in the given order where present. |
||
| 287 | * |
||
| 288 | * @param array $sequence |
||
| 289 | */ |
||
| 290 | 145 | public function defineOrdering(array $sequence) |
|
| 294 | |||
| 295 | /** |
||
| 296 | * Set a list of header names which must always be displayed when set. |
||
| 297 | * |
||
| 298 | * Usually headers without a field value won't be output unless set here. |
||
| 299 | * |
||
| 300 | * @param array $names |
||
| 301 | */ |
||
| 302 | 110 | public function setAlwaysDisplayed(array $names) |
|
| 306 | |||
| 307 | /** |
||
| 308 | * Notify this observer that the entity's charset has changed. |
||
| 309 | * |
||
| 310 | * @param string $charset |
||
| 311 | */ |
||
| 312 | 118 | public function charsetChanged($charset) |
|
| 316 | |||
| 317 | /** |
||
| 318 | * Returns a string with a representation of all headers. |
||
| 319 | * |
||
| 320 | * @return string |
||
| 321 | */ |
||
| 322 | 135 | public function toString() |
|
| 340 | |||
| 341 | /** |
||
| 342 | * Returns a string representation of this object. |
||
| 343 | * |
||
| 344 | * @return string |
||
| 345 | * |
||
| 346 | * @see toString() |
||
| 347 | */ |
||
| 348 | public function __toString() |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Save a Header to the internal collection |
||
| 355 | * |
||
| 356 | * @param string $name |
||
| 357 | * @param Swift_Mime_Header $header |
||
| 358 | * @param null|int $offset |
||
| 359 | */ |
||
| 360 | 185 | private function _storeHeader($name, Swift_Mime_Header $header, $offset = null) |
|
| 372 | |||
| 373 | /** Test if the headers can be sorted */ |
||
| 374 | 140 | private function _canSort() |
|
| 378 | |||
| 379 | /** |
||
| 380 | * uksort() algorithm for Header ordering |
||
| 381 | * |
||
| 382 | * @param string $a |
||
| 383 | * @param string $b |
||
| 384 | * |
||
| 385 | * @return int |
||
| 386 | */ |
||
| 387 | 132 | private function _sortHeaders($a, $b) |
|
| 407 | |||
| 408 | /** |
||
| 409 | * Test if the given Header is always displayed |
||
| 410 | * |
||
| 411 | * @param Swift_Mime_Header $header |
||
| 412 | * |
||
| 413 | * @return bool |
||
| 414 | */ |
||
| 415 | 135 | private function _isDisplayed(Swift_Mime_Header $header) |
|
| 419 | |||
| 420 | /** |
||
| 421 | * Notify all Headers of the new charset |
||
| 422 | * |
||
| 423 | * @param $charset |
||
| 424 | */ |
||
| 425 | 132 | private function _notifyHeadersOfCharset($charset) |
|
| 434 | |||
| 435 | /** |
||
| 436 | * Make a deep copy of object. |
||
| 437 | */ |
||
| 438 | 5 | public function __clone() |
|
| 448 | } |
||
| 449 |
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.