Complex classes like PartFilter 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 PartFilter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 44 | class PartFilter |
||
| 45 | { |
||
| 46 | /** |
||
| 47 | * @var int indicates a filter is not in use |
||
| 48 | */ |
||
| 49 | const FILTER_OFF = 0; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var int an excluded filter must not be included in a part |
||
| 53 | */ |
||
| 54 | const FILTER_EXCLUDE = 1; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var int an included filter must be included in a part |
||
| 58 | */ |
||
| 59 | const FILTER_INCLUDE = 2; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var int filters based on whether MimePart::isMultiPart is set |
||
| 63 | */ |
||
| 64 | private $multipart = PartFilter::FILTER_OFF; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var int filters based on whether MimePart::isTextPart is set |
||
| 68 | */ |
||
| 69 | private $textpart = PartFilter::FILTER_OFF; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var int filters based on whether the parent of a part is a |
||
| 73 | * multipart/signed part and this part has a content-type equal to its |
||
| 74 | * parent's 'protocol' parameter in its content-type header |
||
| 75 | */ |
||
| 76 | private $signedpart = PartFilter::FILTER_EXCLUDE; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var string[][] array of header rules. The top-level contains keys of |
||
| 80 | * FILTER_INCLUDE and/or FILTER_EXCLUDE, which contain key => value mapping |
||
| 81 | * of header names => values to search for. Note that when searching |
||
| 82 | * MimePart::getHeaderValue is used (so additional parameters need not be |
||
| 83 | * matched) and strcasecmp is used. |
||
| 84 | * |
||
| 85 | * ```php |
||
| 86 | * $filter = new PartFilter(); |
||
| 87 | * $filter->headers = [ PartFilter::FILTER_INCLUDE => [ 'Content-Type' => 'text/plain' ] ]; |
||
| 88 | * ``` |
||
| 89 | */ |
||
| 90 | private $headers = []; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var string[] map of headers and default values if the header isn't set. |
||
| 94 | * This allows text/plain to match a Content-Type header that hasn't |
||
| 95 | * been set for instance. |
||
| 96 | */ |
||
| 97 | private $defaultHeaderValues = [ |
||
|
|
|||
| 98 | 'Content-Type' => 'text/plain', |
||
| 99 | 'Content-Disposition' => 'inline', |
||
| 100 | ]; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Convenience method to filter for a specific mime type. |
||
| 104 | * |
||
| 105 | * @param string $mimeType |
||
| 106 | * @return PartFilter |
||
| 107 | */ |
||
| 108 | public static function fromContentType($mimeType) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Convenience method to look for parts of a specific mime-type, and that |
||
| 121 | * do not specifically have a Content-Disposition equal to 'attachment'. |
||
| 122 | * |
||
| 123 | * @param string $mimeType |
||
| 124 | * @return PartFilter |
||
| 125 | */ |
||
| 126 | public static function fromInlineContentType($mimeType) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Convenience method to search for parts with a specific |
||
| 142 | * Content-Disposition, optionally including multipart parts. |
||
| 143 | * |
||
| 144 | * @param string $disposition |
||
| 145 | * @param int $multipart |
||
| 146 | * @return PartFilter |
||
| 147 | */ |
||
| 148 | public static function fromDisposition($disposition, $multipart = PartFilter::FILTER_OFF) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Constructs a PartFilter, optionally instantiating member variables with |
||
| 162 | * values in the passed array. |
||
| 163 | * |
||
| 164 | * The passed array must use keys equal to member variable names, e.g. |
||
| 165 | * 'multipart', 'textpart', 'signedpart' and 'headers'. |
||
| 166 | * |
||
| 167 | * @param array $filter |
||
| 168 | */ |
||
| 169 | public function __construct(array $filter = []) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Validates an argument passed to __set to insure it's set to a value in |
||
| 181 | * $valid. |
||
| 182 | * |
||
| 183 | * @param string $name Name of the member variable |
||
| 184 | * @param string $value The value to test |
||
| 185 | * @param array $valid an array of valid values |
||
| 186 | * @throws InvalidArgumentException |
||
| 187 | */ |
||
| 188 | private function validateArgument($name, $value, array $valid) |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Sets the PartFilter's headers filter to the passed array after validating |
||
| 202 | * it. |
||
| 203 | * |
||
| 204 | * @param array $headers |
||
| 205 | * @throws InvalidArgumentException |
||
| 206 | */ |
||
| 207 | public function setHeaders(array $headers) |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Sets the member variable denoted by $name to the passed $value after |
||
| 228 | * validating it. |
||
| 229 | * |
||
| 230 | * @param string $name |
||
| 231 | * @param int|array $value |
||
| 232 | * @throws InvalidArgumentException |
||
| 233 | */ |
||
| 234 | public function __set($name, $value) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Returns true if the variable denoted by $name is a member variable of |
||
| 253 | * PartFilter. |
||
| 254 | * |
||
| 255 | * @param string $name |
||
| 256 | * @return bool |
||
| 257 | */ |
||
| 258 | public function __isset($name) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Returns the value of the member variable denoted by $name |
||
| 265 | * |
||
| 266 | * @param string $name |
||
| 267 | * @return mixed |
||
| 268 | */ |
||
| 269 | public function __get($name) |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Returns true if the passed MimePart fails the filter's multipart filter |
||
| 276 | * settings. |
||
| 277 | * |
||
| 278 | * @param \ZBateson\MailMimeParser\Message\Part\MimePart $part |
||
| 279 | * @return bool |
||
| 280 | */ |
||
| 281 | private function failsMultiPartFilter(MessagePart $part) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Returns true if the passed MimePart fails the filter's textpart filter |
||
| 292 | * settings. |
||
| 293 | * |
||
| 294 | * @param \ZBateson\MailMimeParser\Message\Part\MimePart $part |
||
| 295 | * @return bool |
||
| 296 | */ |
||
| 297 | private function failsTextPartFilter(MessagePart $part) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Returns true if the passed MimePart fails the filter's signedpart filter |
||
| 305 | * settings. |
||
| 306 | * |
||
| 307 | * @param \ZBateson\MailMimeParser\Message\Part\MimePart $part |
||
| 308 | * @return boolean |
||
| 309 | */ |
||
| 310 | private function failsSignedPartFilter(MessagePart $part) |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Returns true if the passed MimePart fails the filter's header filter |
||
| 328 | * settings. |
||
| 329 | * |
||
| 330 | * @param \ZBateson\MailMimeParser\Message\Part\MimePart $part |
||
| 331 | * @return boolean |
||
| 332 | */ |
||
| 333 | public function failsHeaderPartFilter(MessagePart $part) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Determines if the passed MimePart should be filtered out or not. If the |
||
| 361 | * MimePart passes all filter tests, true is returned. Otherwise false is |
||
| 362 | * returned. |
||
| 363 | * |
||
| 364 | * @param \ZBateson\MailMimeParser\Message\Part\MimePart $part |
||
| 365 | * @return boolean |
||
| 366 | */ |
||
| 367 | public function filter(MessagePart $part) |
||
| 374 | } |
||
| 375 |
This check marks private properties in classes that are never used. Those properties can be removed.