Complex classes like MediaType 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 MediaType, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types=1); |
||
| 27 | class MediaType implements MediaTypeInterface |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | private $type; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | private $subType; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var string? |
||
| 41 | */ |
||
| 42 | private $mediaType = null; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var array<string,string>|null |
||
| 46 | */ |
||
| 47 | private $parameters; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * A list of parameter names for case-insensitive compare. Keys must be lower-cased. |
||
| 51 | * |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | protected const PARAMETER_NAMES = [ |
||
| 55 | 'charset' => true, |
||
| 56 | ]; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @param string $type |
||
| 60 | * @param string $subType |
||
| 61 | * @param array<string,string>|null $parameters |
||
| 62 | */ |
||
| 63 | 45 | public function __construct(string $type, string $subType, array $parameters = null) |
|
| 79 | |||
| 80 | /** |
||
| 81 | * @inheritdoc |
||
| 82 | */ |
||
| 83 | 41 | public function getType(): string |
|
| 87 | |||
| 88 | /** |
||
| 89 | * @inheritdoc |
||
| 90 | */ |
||
| 91 | 41 | public function getSubType(): string |
|
| 95 | |||
| 96 | /** |
||
| 97 | * @inheritdoc |
||
| 98 | */ |
||
| 99 | 35 | public function getMediaType(): string |
|
| 107 | |||
| 108 | /** |
||
| 109 | * @inheritdoc |
||
| 110 | */ |
||
| 111 | 22 | public function getParameters(): ?array |
|
| 115 | |||
| 116 | /** |
||
| 117 | * @inheritdoc |
||
| 118 | */ |
||
| 119 | 3 | public function matchesTo(MediaTypeInterface $mediaType): bool |
|
| 126 | |||
| 127 | /** |
||
| 128 | * @inheritdoc |
||
| 129 | */ |
||
| 130 | 4 | public function equalsTo(MediaTypeInterface $mediaType): bool |
|
| 137 | |||
| 138 | /** |
||
| 139 | * @param MediaTypeInterface $mediaType |
||
| 140 | * |
||
| 141 | * @return bool |
||
| 142 | */ |
||
| 143 | 3 | private function isTypeMatches(MediaTypeInterface $mediaType): bool |
|
| 147 | |||
| 148 | /** |
||
| 149 | * @param MediaTypeInterface $mediaType |
||
| 150 | * |
||
| 151 | * @return bool |
||
| 152 | */ |
||
| 153 | 6 | private function isTypeEquals(MediaTypeInterface $mediaType): bool |
|
| 159 | |||
| 160 | /** |
||
| 161 | * @param MediaTypeInterface $mediaType |
||
| 162 | * |
||
| 163 | * @return bool |
||
| 164 | */ |
||
| 165 | 3 | private function isSubTypeMatches(MediaTypeInterface $mediaType): bool |
|
| 169 | |||
| 170 | /** |
||
| 171 | * @param MediaTypeInterface $mediaType |
||
| 172 | * |
||
| 173 | * @return bool |
||
| 174 | */ |
||
| 175 | 6 | private function isSubTypeEquals(MediaTypeInterface $mediaType): bool |
|
| 181 | |||
| 182 | /** |
||
| 183 | * @param MediaTypeInterface $mediaType |
||
| 184 | * |
||
| 185 | * @return bool |
||
| 186 | */ |
||
| 187 | 3 | private function isMediaParametersMatch(MediaTypeInterface $mediaType): bool |
|
| 217 | |||
| 218 | /** |
||
| 219 | * @param MediaTypeInterface $mediaType |
||
| 220 | * |
||
| 221 | * @return bool |
||
| 222 | */ |
||
| 223 | 4 | private function isMediaParametersEqual(MediaTypeInterface $mediaType): bool |
|
| 253 | |||
| 254 | /** |
||
| 255 | * @param MediaTypeInterface $mediaType |
||
| 256 | * |
||
| 257 | * @return bool |
||
| 258 | */ |
||
| 259 | 6 | private function bothMediaTypeParamsEmpty(MediaTypeInterface $mediaType): bool |
|
| 263 | |||
| 264 | /** |
||
| 265 | * @param MediaTypeInterface $mediaType |
||
| 266 | * |
||
| 267 | * @return bool |
||
| 268 | */ |
||
| 269 | 4 | private function bothMediaTypeParamsNotEmptyAndEqualInSize(MediaTypeInterface $mediaType): bool |
|
| 276 | |||
| 277 | /** |
||
| 278 | * @param string $name |
||
| 279 | * |
||
| 280 | * @return bool |
||
| 281 | */ |
||
| 282 | 4 | private function isParamCaseInsensitive(string $name): bool |
|
| 286 | |||
| 287 | /** |
||
| 288 | * @param string $name |
||
| 289 | * @param string $value |
||
| 290 | * @param string $valueToCompare |
||
| 291 | * |
||
| 292 | * @return bool |
||
| 293 | */ |
||
| 294 | 4 | private function paramValuesEqual(string $name, string $value, string $valueToCompare): bool |
|
| 301 | |||
| 302 | /** |
||
| 303 | * @param string $name |
||
| 304 | * @param string $value |
||
| 305 | * @param string $valueToCompare |
||
| 306 | * |
||
| 307 | * @return bool |
||
| 308 | */ |
||
| 309 | 2 | private function paramValuesMatch(string $name, string $value, string $valueToCompare): bool |
|
| 315 | } |
||
| 316 |