Complex classes like Share 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 Share, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class Share extends AbstractShare { |
||
| 18 | /** |
||
| 19 | * @var Server $server |
||
| 20 | */ |
||
| 21 | private $server; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var string $name |
||
| 25 | */ |
||
| 26 | private $name; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var Connection $connection |
||
| 30 | */ |
||
| 31 | public $connection; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var \Icewind\SMB\Parser |
||
| 35 | */ |
||
| 36 | protected $parser; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var \Icewind\SMB\System |
||
| 40 | */ |
||
| 41 | private $system; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @param Server $server |
||
| 45 | * @param string $name |
||
| 46 | * @param System $system |
||
| 47 | */ |
||
| 48 | 510 | public function __construct($server, $name, System $system = null) { |
|
| 55 | |||
| 56 | 508 | protected function getConnection() { |
|
| 57 | 508 | $workgroupArgument = ($this->server->getWorkgroup()) ? ' -W ' . escapeshellarg($this->server->getWorkgroup()) : ''; |
|
| 58 | 508 | $smbClientPath = $this->system->getSmbclientPath(); |
|
| 59 | 508 | if (!$smbClientPath) { |
|
| 60 | 2 | throw new DependencyException('Can\'t find smbclient binary in path'); |
|
| 61 | } |
||
| 62 | 508 | $command = sprintf('%s%s %s --authentication-file=%s %s', |
|
| 63 | 508 | $this->system->hasStdBuf() ? 'stdbuf -o0 ' : '', |
|
| 64 | 508 | $this->system->getSmbclientPath(), |
|
| 65 | 254 | $workgroupArgument, |
|
| 66 | 508 | System::getFD(3), |
|
| 67 | 508 | escapeshellarg('//' . $this->server->getHost() . '/' . $this->name) |
|
| 68 | 254 | ); |
|
| 69 | 508 | $connection = new Connection($command, $this->parser); |
|
| 70 | 508 | $connection->writeAuthentication($this->server->getUser(), $this->server->getPassword()); |
|
| 71 | 508 | if (!$connection->isValid()) { |
|
| 72 | throw new ConnectionException(); |
||
| 73 | } |
||
| 74 | 508 | return $connection; |
|
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @throws \Icewind\SMB\Exception\ConnectionException |
||
| 79 | * @throws \Icewind\SMB\Exception\AuthenticationException |
||
| 80 | * @throws \Icewind\SMB\Exception\InvalidHostException |
||
| 81 | */ |
||
| 82 | 506 | protected function connect() { |
|
| 83 | 506 | if ($this->connection and $this->connection->isValid()) { |
|
| 84 | 506 | return; |
|
| 85 | } |
||
| 86 | 506 | $this->connection = $this->getConnection(); |
|
| 87 | 506 | } |
|
| 88 | |||
| 89 | 2 | protected function reconnect() { |
|
| 96 | |||
| 97 | /** |
||
| 98 | * Get the name of the share |
||
| 99 | * |
||
| 100 | * @return string |
||
| 101 | */ |
||
| 102 | 4 | public function getName() { |
|
| 105 | |||
| 106 | 506 | protected function simpleCommand($command, $path) { |
|
| 112 | |||
| 113 | /** |
||
| 114 | * List the content of a remote folder |
||
| 115 | * |
||
| 116 | * @param $path |
||
| 117 | * @return \Icewind\SMB\IFileInfo[] |
||
| 118 | * |
||
| 119 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
| 120 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
| 121 | */ |
||
| 122 | 500 | public function dir($path) { |
|
| 132 | |||
| 133 | /** |
||
| 134 | * @param string $path |
||
| 135 | * @return \Icewind\SMB\IFileInfo |
||
| 136 | */ |
||
| 137 | 66 | public function stat($path) { |
|
| 152 | |||
| 153 | /** |
||
| 154 | * Create a folder on the share |
||
| 155 | * |
||
| 156 | * @param string $path |
||
| 157 | * @return bool |
||
| 158 | * |
||
| 159 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
| 160 | * @throws \Icewind\SMB\Exception\AlreadyExistsException |
||
| 161 | */ |
||
| 162 | 502 | public function mkdir($path) { |
|
| 165 | |||
| 166 | /** |
||
| 167 | * Remove a folder on the share |
||
| 168 | * |
||
| 169 | * @param string $path |
||
| 170 | * @return bool |
||
| 171 | * |
||
| 172 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
| 173 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
| 174 | */ |
||
| 175 | 502 | public function rmdir($path) { |
|
| 178 | |||
| 179 | /** |
||
| 180 | * Delete a file on the share |
||
| 181 | * |
||
| 182 | * @param string $path |
||
| 183 | * @param bool $secondTry |
||
| 184 | * @return bool |
||
| 185 | * @throws InvalidTypeException |
||
| 186 | * @throws NotFoundException |
||
| 187 | * @throws \Exception |
||
| 188 | */ |
||
| 189 | 260 | public function del($path, $secondTry = false) { |
|
| 212 | |||
| 213 | /** |
||
| 214 | * Rename a remote file |
||
| 215 | * |
||
| 216 | * @param string $from |
||
| 217 | * @param string $to |
||
| 218 | * @return bool |
||
| 219 | * |
||
| 220 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
| 221 | * @throws \Icewind\SMB\Exception\AlreadyExistsException |
||
| 222 | */ |
||
| 223 | 56 | public function rename($from, $to) { |
|
| 224 | 56 | $path1 = $this->escapePath($from); |
|
| 225 | 38 | $path2 = $this->escapePath($to); |
|
| 226 | 38 | $output = $this->execute('rename ' . $path1 . ' ' . $path2); |
|
| 227 | 38 | return $this->parseOutput($output, $to); |
|
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Upload a local file |
||
| 232 | * |
||
| 233 | * @param string $source local file |
||
| 234 | * @param string $target remove file |
||
| 235 | * @return bool |
||
| 236 | * |
||
| 237 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
| 238 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
| 239 | */ |
||
| 240 | 228 | public function put($source, $target) { |
|
| 241 | 228 | $path1 = $this->escapeLocalPath($source); //first path is local, needs different escaping |
|
| 242 | 228 | $path2 = $this->escapePath($target); |
|
| 243 | 210 | $output = $this->execute('put ' . $path1 . ' ' . $path2); |
|
| 244 | 210 | return $this->parseOutput($output, $target); |
|
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Download a remote file |
||
| 249 | * |
||
| 250 | * @param string $source remove file |
||
| 251 | * @param string $target local file |
||
| 252 | * @return bool |
||
| 253 | * |
||
| 254 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
| 255 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
| 256 | */ |
||
| 257 | 88 | public function get($source, $target) { |
|
| 258 | 88 | $path1 = $this->escapePath($source); |
|
| 259 | 70 | $path2 = $this->escapeLocalPath($target); //second path is local, needs different escaping |
|
| 260 | 70 | $output = $this->execute('get ' . $path1 . ' ' . $path2); |
|
| 261 | 70 | return $this->parseOutput($output, $source); |
|
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Open a readable stream to a remote file |
||
| 266 | * |
||
| 267 | * @param string $source |
||
| 268 | * @return resource a read only stream with the contents of the remote file |
||
| 269 | * |
||
| 270 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
| 271 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
| 272 | */ |
||
| 273 | 50 | public function read($source) { |
|
| 274 | 50 | $source = $this->escapePath($source); |
|
| 275 | // since returned stream is closed by the caller we need to create a new instance |
||
| 276 | // since we can't re-use the same file descriptor over multiple calls |
||
| 277 | 32 | $connection = $this->getConnection(); |
|
| 278 | |||
| 279 | 32 | $connection->write('get ' . $source . ' ' . System::getFD(5)); |
|
| 280 | 32 | $connection->write('exit'); |
|
| 281 | 32 | $fh = $connection->getFileOutputStream(); |
|
| 282 | 32 | stream_context_set_option($fh, 'file', 'connection', $connection); |
|
| 283 | 32 | return $fh; |
|
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Open a writable stream to a remote file |
||
| 288 | * |
||
| 289 | * @param string $target |
||
| 290 | * @return resource a write only stream to upload a remote file |
||
| 291 | * |
||
| 292 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
| 293 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
| 294 | */ |
||
| 295 | 50 | public function write($target) { |
|
| 296 | 50 | $target = $this->escapePath($target); |
|
| 297 | // since returned stream is closed by the caller we need to create a new instance |
||
| 298 | // since we can't re-use the same file descriptor over multiple calls |
||
| 299 | 32 | $connection = $this->getConnection(); |
|
| 300 | |||
| 301 | 32 | $fh = $connection->getFileInputStream(); |
|
| 302 | 32 | $connection->write('put ' . System::getFD(4) . ' ' . $target); |
|
| 303 | 32 | $connection->write('exit'); |
|
| 304 | |||
| 305 | // use a close callback to ensure the upload is finished before continuing |
||
| 306 | // this also serves as a way to keep the connection in scope |
||
| 307 | 32 | return CallbackWrapper::wrap($fh, null, null, function () use ($connection, $target) { |
|
| 308 | 32 | $connection->close(false); // dont terminate, give the upload some time |
|
| 309 | 32 | }); |
|
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * @param string $path |
||
| 314 | * @param int $mode a combination of FileInfo::MODE_READONLY, FileInfo::MODE_ARCHIVE, FileInfo::MODE_SYSTEM and FileInfo::MODE_HIDDEN, FileInfo::NORMAL |
||
| 315 | * @return mixed |
||
| 316 | */ |
||
| 317 | 16 | public function setMode($path, $mode) { |
|
| 346 | |||
| 347 | /** |
||
| 348 | * @param string $path |
||
| 349 | * @return INotifyHandler |
||
| 350 | * @throws ConnectionException |
||
| 351 | * @throws DependencyException |
||
| 352 | */ |
||
| 353 | 8 | public function notify($path) { |
|
| 362 | |||
| 363 | /** |
||
| 364 | * @param string $command |
||
| 365 | * @return array |
||
| 366 | */ |
||
| 367 | 506 | protected function execute($command) { |
|
| 373 | |||
| 374 | /** |
||
| 375 | * check output for errors |
||
| 376 | * |
||
| 377 | * @param string[] $lines |
||
| 378 | * @param string $path |
||
| 379 | * |
||
| 380 | * @throws NotFoundException |
||
| 381 | * @throws \Icewind\SMB\Exception\AlreadyExistsException |
||
| 382 | * @throws \Icewind\SMB\Exception\AccessDeniedException |
||
| 383 | * @throws \Icewind\SMB\Exception\NotEmptyException |
||
| 384 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
| 385 | * @throws \Icewind\SMB\Exception\Exception |
||
| 386 | * @return bool |
||
| 387 | */ |
||
| 388 | 506 | protected function parseOutput($lines, $path = '') { |
|
| 396 | |||
| 397 | /** |
||
| 398 | * @param string $string |
||
| 399 | * @return string |
||
| 400 | */ |
||
| 401 | protected function escape($string) { |
||
| 404 | |||
| 405 | /** |
||
| 406 | * @param string $path |
||
| 407 | * @return string |
||
| 408 | */ |
||
| 409 | 508 | protected function escapePath($path) { |
|
| 418 | |||
| 419 | /** |
||
| 420 | * @param string $path |
||
| 421 | * @return string |
||
| 422 | */ |
||
| 423 | 264 | protected function escapeLocalPath($path) { |
|
| 427 | |||
| 428 | 510 | public function __destruct() { |
|
| 431 | } |
||
| 432 |