Complex classes like DataPath 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 DataPath, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class DataPath extends AbstractComponent implements DataPathInterface |
||
| 28 | { |
||
| 29 | use PathTrait; |
||
| 30 | |||
| 31 | const DEFAULT_MIMETYPE = 'text/plain'; |
||
| 32 | |||
| 33 | const DEFAULT_PARAMETER = 'charset=us-ascii'; |
||
| 34 | |||
| 35 | const BINARY_PARAMETER = 'base64'; |
||
| 36 | |||
| 37 | const REGEXP_MIMETYPE = ',^\w+/[-.\w]+(?:\+[-.\w]+)?$,'; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * The mediatype mimetype |
||
| 41 | * |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | protected $mimetype; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * The mediatype parameters |
||
| 48 | * |
||
| 49 | * @var string[] |
||
| 50 | */ |
||
| 51 | protected $parameters; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Is the Document bas64 encoded |
||
| 55 | * |
||
| 56 | * @var bool |
||
| 57 | */ |
||
| 58 | protected $isBinaryData; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * The document string representation |
||
| 62 | * |
||
| 63 | * @var string |
||
| 64 | */ |
||
| 65 | protected $document; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * new instance |
||
| 69 | * |
||
| 70 | * @param string $path the component value |
||
| 71 | */ |
||
| 72 | 131 | public function __construct($path = '') |
|
| 80 | |||
| 81 | /** |
||
| 82 | * @inheritdoc |
||
| 83 | */ |
||
| 84 | 131 | protected function validate($path) |
|
| 103 | |||
| 104 | /** |
||
| 105 | * @inheritdoc |
||
| 106 | */ |
||
| 107 | 131 | protected function assertValidComponent($path) |
|
| 118 | |||
| 119 | /** |
||
| 120 | * Filter the mimeType property |
||
| 121 | * |
||
| 122 | * @param string $mimetype |
||
| 123 | * |
||
| 124 | * @throws InvalidArgumentException If the mimetype is invalid |
||
| 125 | * |
||
| 126 | * @return string |
||
| 127 | */ |
||
| 128 | 116 | protected function filterMimeType($mimetype) |
|
| 136 | |||
| 137 | /** |
||
| 138 | * Extract and set the binary flag from the parameters if it exists |
||
| 139 | * |
||
| 140 | * @param string $parameters |
||
| 141 | * |
||
| 142 | * @throws InvalidArgumentException If the mediatype parameters contain invalid data |
||
| 143 | * |
||
| 144 | * @return string[] |
||
| 145 | */ |
||
| 146 | 113 | protected function filterParameters($parameters) |
|
| 165 | |||
| 166 | /** |
||
| 167 | * Validate mediatype parameter |
||
| 168 | * |
||
| 169 | * @param string $parameter a mediatype parameter |
||
| 170 | * |
||
| 171 | * @return bool |
||
| 172 | */ |
||
| 173 | 104 | protected function validateParameter($parameter) |
|
| 179 | |||
| 180 | /** |
||
| 181 | * Validate the path document string representation |
||
| 182 | * |
||
| 183 | * @throws InvalidArgumentException If the data is invalid |
||
| 184 | */ |
||
| 185 | 60 | protected function validateDocument() |
|
| 192 | |||
| 193 | /** |
||
| 194 | * Retrieves the data string. |
||
| 195 | * |
||
| 196 | * Retrieves the data part of the path. If no data part is provided return |
||
| 197 | * a empty string |
||
| 198 | * |
||
| 199 | * @return string |
||
| 200 | */ |
||
| 201 | 18 | public function getData() |
|
| 205 | |||
| 206 | /** |
||
| 207 | * Tells whether the data is binary safe encoded |
||
| 208 | * |
||
| 209 | * @return bool |
||
| 210 | */ |
||
| 211 | 27 | public function isBinaryData() |
|
| 215 | |||
| 216 | /** |
||
| 217 | * Retrieve the data mime type associated to the URI. |
||
| 218 | * |
||
| 219 | * If no mimetype is present, this method MUST return the default mimetype 'text/plain'. |
||
| 220 | * |
||
| 221 | * @see http://tools.ietf.org/html/rfc2397#section-2 |
||
| 222 | * |
||
| 223 | * @return string The URI scheme. |
||
| 224 | */ |
||
| 225 | 30 | public function getMimeType() |
|
| 229 | |||
| 230 | /** |
||
| 231 | * Retrieve the parameters associated with the Mime Type of the URI. |
||
| 232 | * |
||
| 233 | * If no parameters is present, this method MUST return the default parameter 'charset=US-ASCII'. |
||
| 234 | * |
||
| 235 | * @see http://tools.ietf.org/html/rfc2397#section-2 |
||
| 236 | * |
||
| 237 | * @return string The URI scheme. |
||
| 238 | */ |
||
| 239 | 95 | public function getParameters() |
|
| 243 | |||
| 244 | /** |
||
| 245 | * Retrieve the mediatype associated with the URI. |
||
| 246 | * |
||
| 247 | * If no mediatype is present, this method MUST return the default parameter 'text/plain;charset=US-ASCII'. |
||
| 248 | * |
||
| 249 | * @see http://tools.ietf.org/html/rfc2397#section-3 |
||
| 250 | * |
||
| 251 | * @return string The URI scheme. |
||
| 252 | */ |
||
| 253 | 15 | public function getMediaType() |
|
| 257 | |||
| 258 | /** |
||
| 259 | * Save the data to a specific file |
||
| 260 | * |
||
| 261 | * @param string $path The path to the file where to save the data |
||
| 262 | * @param string $mode The mode parameter specifies the type of access you require to the stream. |
||
| 263 | * |
||
| 264 | * @throws RuntimeException if the path is not reachable |
||
| 265 | * |
||
| 266 | * @return SplFileObject |
||
| 267 | */ |
||
| 268 | 9 | public function save($path, $mode = 'w') |
|
| 276 | |||
| 277 | /** |
||
| 278 | * Returns the component literal value. |
||
| 279 | * |
||
| 280 | * @return string |
||
| 281 | */ |
||
| 282 | 71 | public function getContent() |
|
| 291 | |||
| 292 | /** |
||
| 293 | * Format the DataURI string |
||
| 294 | * |
||
| 295 | * @param string $mimetype |
||
| 296 | * @param string $parameters |
||
| 297 | * @param bool $isBinaryData |
||
| 298 | * @param string $data |
||
| 299 | * |
||
| 300 | * @return string |
||
| 301 | */ |
||
| 302 | 104 | protected static function format($mimetype, $parameters, $isBinaryData, $data) |
|
| 314 | |||
| 315 | /** |
||
| 316 | * Returns an instance where the data part is base64 encoded |
||
| 317 | * |
||
| 318 | * This method MUST retain the state of the current instance, and return |
||
| 319 | * an instance where the data part is base64 encoded |
||
| 320 | * |
||
| 321 | * @return static |
||
| 322 | */ |
||
| 323 | 12 | public function toBinary() |
|
| 336 | |||
| 337 | /** |
||
| 338 | * Returns an instance where the data part is url encoded following RFC3986 rules |
||
| 339 | * |
||
| 340 | * This method MUST retain the state of the current instance, and return |
||
| 341 | * an instance where the data part is url encoded |
||
| 342 | * |
||
| 343 | * @return static |
||
| 344 | */ |
||
| 345 | 12 | public function toAscii() |
|
| 358 | |||
| 359 | /** |
||
| 360 | * Return an instance with the specified mediatype parameters. |
||
| 361 | * |
||
| 362 | * This method MUST retain the state of the current instance, and return |
||
| 363 | * an instance that contains the specified mediatype parameters. |
||
| 364 | * |
||
| 365 | * Users must provide encoded characters. |
||
| 366 | * |
||
| 367 | * An empty parameters value is equivalent to removing the parameter. |
||
| 368 | * |
||
| 369 | * @param string $parameters The mediatype parameters to use with the new instance. |
||
| 370 | * |
||
| 371 | * @throws InvalidArgumentException for invalid query strings. |
||
| 372 | * |
||
| 373 | * @return static A new instance with the specified mediatype parameters. |
||
| 374 | */ |
||
| 375 | 21 | public function withParameters($parameters) |
|
| 392 | |||
| 393 | /** |
||
| 394 | * Create a new instance from a file path |
||
| 395 | * |
||
| 396 | * @param string $path |
||
| 397 | * |
||
| 398 | * @throws RuntimeException If the File is not readable |
||
| 399 | * |
||
| 400 | * @return static |
||
| 401 | */ |
||
| 402 | 54 | public static function createFromPath($path) |
|
| 415 | |||
| 416 | /** |
||
| 417 | * @inheritdoc |
||
| 418 | */ |
||
| 419 | 3 | public static function __set_state(array $properties) |
|
| 428 | } |
||
| 429 |