Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Path 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 Path, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 41 | class Path extends ObjectAbstract implements PathInterface, ErrorAwareInterface, FilesystemAwareInterface, ExtensionAwareInterface |
||
| 42 | { |
||
| 43 | use FilesystemAwareTrait, ErrorAwareTrait, ExtensionAwareTrait; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Full path with mount point |
||
| 47 | * |
||
| 48 | * @var string |
||
| 49 | * @access protected |
||
| 50 | */ |
||
| 51 | protected $full; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * relative path without mount point prefix |
||
| 55 | * |
||
| 56 | * @var string |
||
| 57 | * @access protected |
||
| 58 | */ |
||
| 59 | protected $path; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Instantiate the path object |
||
| 63 | * |
||
| 64 | * @param string $full full path |
||
| 65 | * @param string $path relative path without mount point |
||
| 66 | * @param FilesystemInterface $filesystem |
||
| 67 | * @access public |
||
| 68 | * @api |
||
| 69 | */ |
||
| 70 | public function __construct( |
||
| 71 | /*# string */ $full, |
||
| 72 | /*# string */ $path, |
||
| 73 | FilesystemInterface $filesystem |
||
| 74 | ) { |
||
| 75 | $this->setFilesystem($filesystem); |
||
| 76 | $this->full = $full; |
||
| 77 | $this->path = $path; |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * {@inheritDoc} |
||
| 82 | */ |
||
| 83 | public function exists()/*# : bool */ |
||
| 84 | { |
||
| 85 | if (!$this->getDriver()->exists($this->path)) { |
||
| 86 | return $this->setError( |
||
| 87 | Message::get(Message::MSG_PATH_NOTFOUND, $this->full), |
||
| 88 | Message::MSG_PATH_NOTFOUND |
||
| 89 | ); |
||
| 90 | } |
||
| 91 | return true; |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * {@inheritDoc} |
||
| 96 | */ |
||
| 97 | View Code Duplication | public function getContent(/*# bool */ $stream = false) |
|
| 98 | { |
||
| 99 | // not exists or filesystem not readable |
||
| 100 | if (!$this->exists() || !$this->isFilesystemReadable()) { |
||
| 101 | return null; |
||
| 102 | } |
||
| 103 | |||
| 104 | $res = $this->getDriver()->getContent($this->path, $stream); |
||
| 105 | $this->resetError(); |
||
| 106 | return $res; |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * {@inheritDoc} |
||
| 111 | */ |
||
| 112 | public function getMeta()/*# : array */ |
||
| 113 | { |
||
| 114 | if (!$this->exists()) { |
||
| 115 | return []; |
||
| 116 | } |
||
| 117 | |||
| 118 | $res = $this->getDriver()->getMeta($this->path); |
||
| 119 | $this->resetError(); |
||
| 120 | return $res; |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * {@inheritDoc} |
||
| 125 | */ |
||
| 126 | public function getPath()/*# : string */ |
||
| 130 | |||
| 131 | /** |
||
| 132 | * {@inheritDoc} |
||
| 133 | */ |
||
| 134 | public function getFullPath()/*# : string */ |
||
| 138 | |||
| 139 | /** |
||
| 140 | * {@inheritDoc} |
||
| 141 | */ |
||
| 142 | public function setContent($content)/*# : bool */ |
||
| 153 | |||
| 154 | /** |
||
| 155 | * {@inheritDoc} |
||
| 156 | */ |
||
| 157 | View Code Duplication | public function setMeta(array $meta)/*# : bool */ |
|
| 170 | |||
| 171 | /** |
||
| 172 | * {@inheritDoc} |
||
| 173 | */ |
||
| 174 | public function isDir(/*# string */ $path = '')/*# : bool */ |
||
| 182 | |||
| 183 | /** |
||
| 184 | * {@inheritDoc} |
||
| 185 | */ |
||
| 186 | public function rename(/*# string */ $destination)/*# : bool */ |
||
| 190 | |||
| 191 | /** |
||
| 192 | * {@inheritDoc} |
||
| 193 | */ |
||
| 194 | public function copy(/*# string */ $destination)/*# : bool */ |
||
| 198 | |||
| 199 | /** |
||
| 200 | * {@inheritDoc} |
||
| 201 | */ |
||
| 202 | View Code Duplication | public function delete()/*# : bool */ |
|
| 215 | |||
| 216 | /** |
||
| 217 | * Reset error to driver's error |
||
| 218 | * |
||
| 219 | * @access protected |
||
| 220 | */ |
||
| 221 | protected function resetError() |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Get the driver |
||
| 228 | * |
||
| 229 | * @return DriverInterface |
||
| 230 | * @access protected |
||
| 231 | */ |
||
| 232 | protected function getDriver()/*# : DriverInterface */ |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Do copy or rename in same filesystem |
||
| 239 | * |
||
| 240 | * @param string $destination |
||
| 241 | * @param string $action 'copy' or 'rename' |
||
| 242 | * @return bool |
||
| 243 | * @access protected |
||
| 244 | */ |
||
| 245 | protected function alterAction( |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Is current path has trailing '/' |
||
| 265 | * |
||
| 266 | * @param string $path |
||
| 267 | * @return bool |
||
| 268 | * @access protected |
||
| 269 | */ |
||
| 270 | protected function hasTrailingSlash(/*# string */ $path)/*# : bool */ |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Check filesystem readable or not |
||
| 281 | * |
||
| 282 | * @return bool |
||
| 283 | * @access protected |
||
| 284 | */ |
||
| 285 | protected function isFilesystemReadable()/*# : bool */ |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Check filesystem writable or not |
||
| 299 | * |
||
| 300 | * @return bool |
||
| 301 | * @access protected |
||
| 302 | */ |
||
| 303 | protected function isFilesystemWritable()/*# : bool */ |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Check filesystem file deletable or not |
||
| 317 | * |
||
| 318 | * @return bool |
||
| 319 | * @access protected |
||
| 320 | */ |
||
| 321 | protected function isFilesystemDeletable()/*# : bool */ |
||
| 332 | } |
||
| 333 |