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 | * @inheritdoc |
||
| 69 | */ |
||
| 70 | protected static $invalidCharactersRegex = ',[?#],'; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * new instance |
||
| 74 | * |
||
| 75 | * @param string $path the component value |
||
| 76 | */ |
||
| 77 | 130 | public function __construct($path = '') |
|
| 78 | { |
||
| 79 | 130 | $path = $this->validateString($path); |
|
| 80 | 130 | if ('' === $path) { |
|
| 81 | 4 | $path = static::DEFAULT_MIMETYPE.';'.static::DEFAULT_PARAMETER.','; |
|
| 82 | 4 | } |
|
| 83 | 130 | $this->setComponentProperties($path); |
|
| 84 | 106 | } |
|
| 85 | |||
| 86 | /** |
||
| 87 | * Set Data Path properties |
||
| 88 | * |
||
| 89 | * @param string $path |
||
| 90 | */ |
||
| 91 | 130 | protected function setComponentProperties($path) |
|
| 110 | |||
| 111 | /** |
||
| 112 | * @inheritdoc |
||
| 113 | */ |
||
| 114 | 130 | protected function assertValidComponent($path) |
|
| 126 | |||
| 127 | /** |
||
| 128 | * Filter the mimeType property |
||
| 129 | * |
||
| 130 | * @param string $mimetype |
||
| 131 | * |
||
| 132 | * @throws InvalidArgumentException If the mimetype is invalid |
||
| 133 | * |
||
| 134 | * @return string |
||
| 135 | */ |
||
| 136 | 115 | protected function filterMimeType($mimetype) |
|
| 137 | { |
||
| 138 | 115 | if (!preg_match(static::REGEXP_MIMETYPE, $mimetype)) { |
|
| 139 | 3 | throw new InvalidArgumentException(sprintf('invalid mimeType, `%s`', $mimetype)); |
|
| 140 | } |
||
| 141 | |||
| 142 | 112 | return $mimetype; |
|
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Extract and set the binary flag from the parameters if it exists |
||
| 147 | * |
||
| 148 | * @param string $parameters |
||
| 149 | * |
||
| 150 | * @throws InvalidArgumentException If the mediatype parameters contain invalid data |
||
| 151 | * |
||
| 152 | * @return string[] |
||
| 153 | */ |
||
| 154 | 112 | protected function filterParameters($parameters) |
|
| 155 | { |
||
| 156 | 112 | $this->isBinaryData = false; |
|
| 157 | 112 | if ('' == $parameters) { |
|
| 158 | 6 | return [static::DEFAULT_PARAMETER]; |
|
| 159 | } |
||
| 160 | |||
| 161 | 109 | if (preg_match(',(;|^)'.static::BINARY_PARAMETER.'$,', $parameters, $matches)) { |
|
| 162 | 63 | $parameters = mb_substr($parameters, 0, - strlen($matches[0])); |
|
| 163 | 63 | $this->isBinaryData = true; |
|
| 164 | 63 | } |
|
| 165 | |||
| 166 | 109 | $params = array_filter(explode(';', $parameters)); |
|
| 167 | 109 | if (!empty(array_filter($params, [$this, 'validateParameter']))) { |
|
| 168 | 12 | throw new InvalidArgumentException(sprintf('invalid mediatype parameters, `%s`', $parameters)); |
|
| 169 | } |
||
| 170 | |||
| 171 | 106 | return $params; |
|
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Validate mediatype parameter |
||
| 176 | * |
||
| 177 | * @param string $parameter a mediatype parameter |
||
| 178 | * |
||
| 179 | * @return bool |
||
| 180 | */ |
||
| 181 | 103 | protected function validateParameter($parameter) |
|
| 187 | |||
| 188 | /** |
||
| 189 | * Validate the path document string representation |
||
| 190 | * |
||
| 191 | * @throws InvalidArgumentException If the data is invalid |
||
| 192 | */ |
||
| 193 | 60 | protected function validateDocument() |
|
| 200 | |||
| 201 | /** |
||
| 202 | * Retrieves the data string. |
||
| 203 | * |
||
| 204 | * Retrieves the data part of the path. If no data part is provided return |
||
| 205 | * a empty string |
||
| 206 | * |
||
| 207 | * @return string |
||
| 208 | */ |
||
| 209 | 18 | public function getData() |
|
| 210 | { |
||
| 211 | 18 | return $this->document; |
|
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Tells whether the data is binary safe encoded |
||
| 216 | * |
||
| 217 | * @return bool |
||
| 218 | */ |
||
| 219 | 27 | public function isBinaryData() |
|
| 220 | { |
||
| 221 | 27 | return $this->isBinaryData; |
|
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Retrieve the data mime type associated to the URI. |
||
| 226 | * |
||
| 227 | * If no mimetype is present, this method MUST return the default mimetype 'text/plain'. |
||
| 228 | * |
||
| 229 | * @see http://tools.ietf.org/html/rfc2397#section-2 |
||
| 230 | * |
||
| 231 | * @return string The URI scheme. |
||
| 232 | */ |
||
| 233 | 30 | public function getMimeType() |
|
| 234 | { |
||
| 235 | 30 | return $this->mimetype; |
|
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Retrieve the parameters associated with the Mime Type of the URI. |
||
| 240 | * |
||
| 241 | * If no parameters is present, this method MUST return the default parameter 'charset=US-ASCII'. |
||
| 242 | * |
||
| 243 | * @see http://tools.ietf.org/html/rfc2397#section-2 |
||
| 244 | * |
||
| 245 | * @return string The URI scheme. |
||
| 246 | */ |
||
| 247 | 94 | public function getParameters() |
|
| 248 | { |
||
| 249 | 94 | return implode(';', $this->parameters); |
|
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Retrieve the mediatype associated with the URI. |
||
| 254 | * |
||
| 255 | * If no mediatype is present, this method MUST return the default parameter 'text/plain;charset=US-ASCII'. |
||
| 256 | * |
||
| 257 | * @see http://tools.ietf.org/html/rfc2397#section-3 |
||
| 258 | * |
||
| 259 | * @return string The URI scheme. |
||
| 260 | */ |
||
| 261 | 15 | public function getMediaType() |
|
| 265 | |||
| 266 | /** |
||
| 267 | * Save the data to a specific file |
||
| 268 | * |
||
| 269 | * @param string $path The path to the file where to save the data |
||
| 270 | * @param string $mode The mode parameter specifies the type of access you require to the stream. |
||
| 271 | * |
||
| 272 | * @throws RuntimeException if the path is not reachable |
||
| 273 | * |
||
| 274 | * @return SplFileObject |
||
| 275 | */ |
||
| 276 | 9 | public function save($path, $mode = 'w') |
|
| 284 | |||
| 285 | /** |
||
| 286 | * Returns the component literal value. |
||
| 287 | * |
||
| 288 | * @return string |
||
| 289 | */ |
||
| 290 | 70 | public function getContent() |
|
| 291 | { |
||
| 292 | 70 | return $this->format( |
|
| 293 | 70 | $this->mimetype, |
|
| 294 | 70 | $this->getParameters(), |
|
| 295 | 70 | $this->isBinaryData, |
|
| 296 | 70 | $this->document |
|
| 297 | 70 | ); |
|
| 298 | } |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Format the DataURI string |
||
| 302 | * |
||
| 303 | * @param string $mimetype |
||
| 304 | * @param string $parameters |
||
| 305 | * @param bool $isBinaryData |
||
| 306 | * @param string $data |
||
| 307 | * |
||
| 308 | * @return string |
||
| 309 | */ |
||
| 310 | 103 | protected static function format($mimetype, $parameters, $isBinaryData, $data) |
|
| 322 | |||
| 323 | /** |
||
| 324 | * Returns an instance where the data part is base64 encoded |
||
| 325 | * |
||
| 326 | * This method MUST retain the state of the current instance, and return |
||
| 327 | * an instance where the data part is base64 encoded |
||
| 328 | * |
||
| 329 | * @return static |
||
| 330 | */ |
||
| 331 | 12 | public function toBinary() |
|
| 344 | |||
| 345 | /** |
||
| 346 | * Returns an instance where the data part is url encoded following RFC3986 rules |
||
| 347 | * |
||
| 348 | * This method MUST retain the state of the current instance, and return |
||
| 349 | * an instance where the data part is url encoded |
||
| 350 | * |
||
| 351 | * @return static |
||
| 352 | */ |
||
| 353 | 12 | public function toAscii() |
|
| 366 | |||
| 367 | /** |
||
| 368 | * Return an instance with the specified mediatype parameters. |
||
| 369 | * |
||
| 370 | * This method MUST retain the state of the current instance, and return |
||
| 371 | * an instance that contains the specified mediatype parameters. |
||
| 372 | * |
||
| 373 | * Users must provide encoded characters. |
||
| 374 | * |
||
| 375 | * An empty parameters value is equivalent to removing the parameter. |
||
| 376 | * |
||
| 377 | * @param string $parameters The mediatype parameters to use with the new instance. |
||
| 378 | * |
||
| 379 | * @throws InvalidArgumentException for invalid query strings. |
||
| 380 | * |
||
| 381 | * @return static A new instance with the specified mediatype parameters. |
||
| 382 | */ |
||
| 383 | 21 | public function withParameters($parameters) |
|
| 400 | |||
| 401 | /** |
||
| 402 | * Create a new instance from a file path |
||
| 403 | * |
||
| 404 | * @param string $path |
||
| 405 | * |
||
| 406 | * @throws RuntimeException If the File is not readable |
||
| 407 | * |
||
| 408 | * @return static |
||
| 409 | */ |
||
| 410 | 54 | public static function createFromPath($path) |
|
| 423 | |||
| 424 | /** |
||
| 425 | * @inheritdoc |
||
| 426 | */ |
||
| 427 | 3 | public static function __set_state(array $properties) |
|
| 436 | } |
||
| 437 |