Complex classes like Uploader 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 Uploader, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 7 | class Uploader | ||
| 8 | { | ||
| 9 | protected $cwd; | ||
| 10 | protected $adapter; | ||
| 11 | protected $original; | ||
| 12 | |||
| 13 | protected $adapters = [ | ||
| 14 | 'Uploader\\Adapters\\Base64', | ||
| 15 | 'Uploader\\Adapters\\Psr7', | ||
| 16 | 'Uploader\\Adapters\\Upload', | ||
| 17 | 'Uploader\\Adapters\\Url', | ||
| 18 | ]; | ||
| 19 | |||
| 20 | protected $callbacks = [ | ||
| 21 | 'destination' => null, | ||
| 22 | 'prefix' => null, | ||
| 23 | 'overwrite' => null, | ||
| 24 | 'directory' => null, | ||
| 25 | 'filename' => null, | ||
| 26 | 'extension' => null, | ||
| 27 | ]; | ||
| 28 | |||
| 29 | protected $options = [ | ||
| 30 | 'prefix' => null, | ||
| 31 | 'overwrite' => false, | ||
| 32 | 'create_dir' => false, | ||
| 33 | 'directory' => null, | ||
| 34 | 'filename' => null, | ||
| 35 | 'extension' => null, | ||
| 36 | ]; | ||
| 37 | |||
| 38 | /** | ||
| 39 | * @param string $cwd The current working directory used to save the file | ||
| 40 | */ | ||
| 41 | public function __construct($cwd) | ||
| 45 | |||
| 46 | /** | ||
| 47 | * Execute a upload. | ||
| 48 | * | ||
| 49 | * @param mixed $original | ||
| 50 | * @param null|string $adapter | ||
| 51 | * | ||
| 52 | * @throws \InvalidArgumentException On error | ||
| 53 | * | ||
| 54 | * @return string The file destination | ||
| 55 | */ | ||
| 56 | public function __invoke($original, $adapter = null) | ||
| 57 |     { | ||
| 58 | return $this | ||
| 59 | ->with($original, $adapter) | ||
| 60 | ->save() | ||
| 61 | ->getDestination(); | ||
| 62 | } | ||
| 63 | |||
| 64 | /** | ||
| 65 | * Set the current working directory. | ||
| 66 | * | ||
| 67 | * @param string $cwd | ||
| 68 | * | ||
| 69 | * @return $this | ||
| 70 | */ | ||
| 71 | public function setCwd($cwd) | ||
| 72 |     { | ||
| 73 | $this->cwd = $cwd; | ||
| 74 | |||
| 75 | return $this; | ||
| 76 | } | ||
| 77 | |||
| 78 | /** | ||
| 79 | * Set a prefix for the filenames. | ||
| 80 | * | ||
| 81 | * @return string|null | ||
| 82 | */ | ||
| 83 | public function getCwd() | ||
| 87 | |||
| 88 | /** | ||
| 89 | * Set a prefix for the filenames. | ||
| 90 | * | ||
| 91 | * @param string|Closure $prefix | ||
| 92 | * | ||
| 93 | * @return $this | ||
| 94 | */ | ||
| 95 | public function setPrefix($prefix) | ||
| 99 | |||
| 100 | /** | ||
| 101 | * Set a prefix for the filenames. | ||
| 102 | * | ||
| 103 | * @return string|null | ||
| 104 | */ | ||
| 105 | public function getPrefix() | ||
| 109 | |||
| 110 | /** | ||
| 111 | * Set the overwrite configuration. | ||
| 112 | * | ||
| 113 | * @param bool|Closure $overwrite | ||
| 114 | * | ||
| 115 | * @return $this | ||
| 116 | */ | ||
| 117 | public function setOverwrite($overwrite) | ||
| 121 | |||
| 122 | /** | ||
| 123 | * Get the overwrite configuration. | ||
| 124 | * | ||
| 125 | * @return bool | ||
| 126 | */ | ||
| 127 | public function getOverwrite() | ||
| 131 | |||
| 132 | /** | ||
| 133 | * Set the create_dir configuration. | ||
| 134 | * | ||
| 135 | * @param bool|Closure $create_dir | ||
| 136 | * | ||
| 137 | * @return $this | ||
| 138 | */ | ||
| 139 | public function setCreateDir($create_dir) | ||
| 143 | |||
| 144 | /** | ||
| 145 | * Get the create_dir configuration. | ||
| 146 | * | ||
| 147 | * @return bool | ||
| 148 | */ | ||
| 149 | public function getCreateDir() | ||
| 153 | |||
| 154 | /** | ||
| 155 | * Set the destination of the file. It includes the directory, filename and extension. | ||
| 156 | * | ||
| 157 | * @param string|Closure $destination | ||
| 158 | * | ||
| 159 | * @return $this | ||
| 160 | */ | ||
| 161 | public function setDestination($destination) | ||
| 162 |     { | ||
| 163 |         if ($destination instanceof Closure) { | ||
| 164 | $this->callbacks['destination'] = $destination; | ||
| 165 |         } else { | ||
| 166 | $this->options = self::parsePath($destination) + $this->options; | ||
| 167 | } | ||
| 168 | |||
| 169 | return $this; | ||
| 170 | } | ||
| 171 | |||
| 172 | /** | ||
| 173 | * Returns the file destination. | ||
| 174 | * | ||
| 175 | * @param bool $absolute Whether or not returns the cwd | ||
| 176 | * | ||
| 177 | * @return string | ||
| 178 | */ | ||
| 179 | public function getDestination($absolute = false) | ||
| 183 | |||
| 184 | /** | ||
| 185 | * Set only the directory of the destination. | ||
| 186 | * | ||
| 187 | * @param string|Closure $directory | ||
| 188 | * | ||
| 189 | * @return $this | ||
| 190 | */ | ||
| 191 | public function setDirectory($directory) | ||
| 195 | |||
| 196 | /** | ||
| 197 | * Get the directory of the destination. | ||
| 198 | * | ||
| 199 | * @return null|string | ||
| 200 | */ | ||
| 201 | public function getDirectory() | ||
| 205 | |||
| 206 | /** | ||
| 207 | * Set only the filename of the destination. | ||
| 208 | * | ||
| 209 | * @param string|Closure $filename | ||
| 210 | * | ||
| 211 | * @return $this | ||
| 212 | */ | ||
| 213 | public function setFilename($filename) | ||
| 217 | |||
| 218 | /** | ||
| 219 | * Get the filename of the destination. | ||
| 220 | * | ||
| 221 | * @return null|string | ||
| 222 | */ | ||
| 223 | public function getFilename() | ||
| 227 | |||
| 228 | /** | ||
| 229 | * Set only the file extension of the destination. | ||
| 230 | * | ||
| 231 | * @param string|Closure $extension | ||
| 232 | * | ||
| 233 | * @return $this | ||
| 234 | */ | ||
| 235 | public function setExtension($extension) | ||
| 239 | |||
| 240 | /** | ||
| 241 | * Get the extension of the destination. | ||
| 242 | * | ||
| 243 | * @return null|string | ||
| 244 | */ | ||
| 245 | public function getExtension() | ||
| 249 | |||
| 250 | /** | ||
| 251 | * Set the original source. | ||
| 252 | * | ||
| 253 | * @param mixed $original | ||
| 254 | * @param null|string $adapter | ||
| 255 | * | ||
| 256 | * @throws \InvalidArgumentException On error | ||
| 257 | * | ||
| 258 | * @return Uploader A new cloned copy with the source and adapter configured | ||
| 259 | */ | ||
| 260 | public function with($original, $adapter = null) | ||
| 281 | |||
| 282 | /** | ||
| 283 | * Save the file. | ||
| 284 | * | ||
| 285 | * @throws \Exception On error | ||
| 286 | * | ||
| 287 | * @return $this | ||
| 288 | */ | ||
| 289 | public function save() | ||
| 324 | |||
| 325 | /** | ||
| 326 | * Saves an option. | ||
| 327 | * | ||
| 328 | * @param string $name | ||
| 329 | * @param mixed $value | ||
| 330 | * | ||
| 331 | * @return $this | ||
| 332 | */ | ||
| 333 | protected function setOption($name, $value) | ||
| 343 | |||
| 344 | /** | ||
| 345 | * Helper function used to parse a path. | ||
| 346 | * | ||
| 347 | * @param string $path | ||
| 348 | * | ||
| 349 | * @return array With 3 keys: directory, filename and extension | ||
| 350 | */ | ||
| 351 | public static function parsePath($path) | ||
| 361 | |||
| 362 | /** | ||
| 363 | * Resolve paths with ../, //, etc... | ||
| 364 | * | ||
| 365 | * @param string $path | ||
| 366 | * | ||
| 367 | * @return string | ||
| 368 | */ | ||
| 369 | private static function fixPath($path) | ||
| 383 | } | ||
| 384 |