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 |
||
| 42 | class PartFilter |
||
| 43 | { |
||
| 44 | /** |
||
| 45 | * @var int indicates a filter is not in use |
||
| 46 | */ |
||
| 47 | const FILTER_OFF = 0; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var int an excluded filter must not be included in a part |
||
| 51 | */ |
||
| 52 | const FILTER_EXCLUDE = 1; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var int an included filter must be included in a part |
||
| 56 | */ |
||
| 57 | const FILTER_INCLUDE = 2; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var int filters based on whether MimePart::isMultiPart is set |
||
| 61 | */ |
||
| 62 | private $multipart = PartFilter::FILTER_OFF; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var int filters based on whether MimePart::isTextPart is set |
||
| 66 | */ |
||
| 67 | private $textpart = PartFilter::FILTER_OFF; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var int filters based on whether the parent of a part is a |
||
| 71 | * multipart/signed part and this part has a content-type equal to its |
||
| 72 | * parent's 'protocol' parameter in its content-type header |
||
| 73 | */ |
||
| 74 | private $signedpart = PartFilter::FILTER_EXCLUDE; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var string[][] array of header rules. The top-level contains keys of |
||
| 78 | * FILTER_INCLUDE and/or FILTER_EXCLUDE, which contain key => value mapping |
||
| 79 | * of header names => values to search for. Note that when searching |
||
| 80 | * MimePart::getHeaderValue is used (so additional parameters need not be |
||
| 81 | * matched) and strcasecmp is used. |
||
| 82 | * |
||
| 83 | * ```php |
||
| 84 | * $filter = new PartFilter(); |
||
| 85 | * $filter->headers = [ PartFilter::FILTER_INCLUDE => [ 'Content-Type' => 'text/plain' ] ]; |
||
| 86 | * ``` |
||
| 87 | */ |
||
| 88 | private $headers = []; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Convenience method to filter for a specific mime type. |
||
| 92 | * |
||
| 93 | * @param string $mimeType |
||
| 94 | * @return PartFilter |
||
| 95 | */ |
||
| 96 | 2 | public static function fromContentType($mimeType) |
|
| 106 | |||
| 107 | /** |
||
| 108 | * Convenience method to look for parts of a specific mime-type, and that |
||
| 109 | * do not specifically have a Content-Disposition equal to 'attachment'. |
||
| 110 | * |
||
| 111 | * @param string $mimeType |
||
| 112 | * @return PartFilter |
||
| 113 | */ |
||
| 114 | 1 | public static function fromInlineContentType($mimeType) |
|
| 127 | |||
| 128 | /** |
||
| 129 | * Convenience method to search for parts with a specific |
||
| 130 | * Content-Disposition, optionally including multipart parts. |
||
| 131 | * |
||
| 132 | * @param string $disposition |
||
| 133 | * @param int $multipart |
||
| 134 | * @return PartFilter |
||
| 135 | */ |
||
| 136 | 5 | public static function fromDisposition($disposition, $multipart = PartFilter::FILTER_OFF) |
|
| 147 | |||
| 148 | /** |
||
| 149 | * Constructs a PartFilter, optionally instantiating member variables with |
||
| 150 | * values in the passed array. |
||
| 151 | * |
||
| 152 | * The passed array must use keys equal to member variable names, e.g. |
||
| 153 | * 'multipart', 'textpart', 'signedpart' and 'headers'. |
||
| 154 | * |
||
| 155 | * @param array $filter |
||
| 156 | */ |
||
| 157 | 16 | public function __construct(array $filter = []) |
|
| 166 | |||
| 167 | /** |
||
| 168 | * Validates an argument passed to __set to insure it's set to a value in |
||
| 169 | * $valid. |
||
| 170 | * |
||
| 171 | * @param string $name Name of the member variable |
||
| 172 | * @param string $value The value to test |
||
| 173 | * @param array $valid an array of valid values |
||
| 174 | * @throws InvalidArgumentException |
||
| 175 | */ |
||
| 176 | 16 | private function validateArgument($name, $value, array $valid) |
|
| 187 | |||
| 188 | /** |
||
| 189 | * Sets the PartFilter's headers filter to the passed array after validating |
||
| 190 | * it. |
||
| 191 | * |
||
| 192 | * @param array $headers |
||
| 193 | * @throws InvalidArgumentException |
||
| 194 | */ |
||
| 195 | public function setHeaders(array $headers) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Sets the member variable denoted by $name to the passed $value after |
||
| 216 | * validating it. |
||
| 217 | * |
||
| 218 | * @param string $name |
||
| 219 | * @param int|array $value |
||
| 220 | * @throws InvalidArgumentException |
||
| 221 | */ |
||
| 222 | 16 | public function __set($name, $value) |
|
| 238 | |||
| 239 | /** |
||
| 240 | * Returns true if the variable denoted by $name is a member variable of |
||
| 241 | * PartFilter. |
||
| 242 | * |
||
| 243 | * @param string $name |
||
| 244 | * @return bool |
||
| 245 | */ |
||
| 246 | public function __isset($name) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Returns the value of the member variable denoted by $name |
||
| 253 | * |
||
| 254 | * @param string $name |
||
| 255 | * @return mixed |
||
| 256 | */ |
||
| 257 | 16 | public function __get($name) |
|
| 261 | |||
| 262 | /** |
||
| 263 | * Returns true if the passed MimePart fails the filter's multipart filter |
||
| 264 | * settings. |
||
| 265 | * |
||
| 266 | * @param \ZBateson\MailMimeParser\Message\MimePart $part |
||
| 267 | * @return bool |
||
| 268 | */ |
||
| 269 | 16 | private function failsMultiPartFilter(MimePart $part) |
|
| 274 | |||
| 275 | /** |
||
| 276 | * Returns true if the passed MimePart fails the filter's textpart filter |
||
| 277 | * settings. |
||
| 278 | * |
||
| 279 | * @param \ZBateson\MailMimeParser\Message\MimePart $part |
||
| 280 | * @return bool |
||
| 281 | */ |
||
| 282 | 16 | private function failsTextPartFilter(MimePart $part) |
|
| 287 | |||
| 288 | /** |
||
| 289 | * Returns true if the passed MimePart fails the filter's signedpart filter |
||
| 290 | * settings. |
||
| 291 | * |
||
| 292 | * @param \ZBateson\MailMimeParser\Message\MimePart $part |
||
| 293 | * @return boolean |
||
| 294 | */ |
||
| 295 | 16 | private function failsSignedPartFilter(MimePart $part) |
|
| 310 | |||
| 311 | /** |
||
| 312 | * Returns true if the passed MimePart fails the filter's header filter |
||
| 313 | * settings. |
||
| 314 | * |
||
| 315 | * @param \ZBateson\MailMimeParser\Message\MimePart $part |
||
| 316 | * @return boolean |
||
| 317 | */ |
||
| 318 | 16 | public function failsHeaderPartFilter(MimePart $part) |
|
| 331 | |||
| 332 | /** |
||
| 333 | * Determines if the passed MimePart should be filtered out or not. If the |
||
| 334 | * MimePart passes all filter tests, true is returned. Otherwise false is |
||
| 335 | * returned. |
||
| 336 | * |
||
| 337 | * @param \ZBateson\MailMimeParser\Message\MimePart $part |
||
| 338 | * @return boolean |
||
| 339 | */ |
||
| 340 | 16 | public function filter(MimePart $part) |
|
| 347 | } |
||
| 348 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: