| Conditions | 29 |
| Paths | 5456 |
| Total Lines | 118 |
| Code Lines | 47 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 275 | protected function doGetOperationCollection(Index $localIndex = null, Index $remoteIndex = null, Index $mergedIndex = null): OperationCollection |
||
| 276 | { |
||
| 277 | $localIndex = $localIndex ?: $this->buildLocalIndex(); |
||
| 278 | $remoteIndex = $remoteIndex ?: $this->loadRemoteIndex(); |
||
| 279 | $mergedIndex = $mergedIndex ?: $this->doBuildMergedIndex($localIndex, $remoteIndex); |
||
| 280 | |||
| 281 | |||
| 282 | $operationCollection = new OperationCollection(); |
||
| 283 | |||
| 284 | $directoryMtimes = []; |
||
| 285 | |||
| 286 | foreach ($mergedIndex as $indexObject) |
||
| 287 | { |
||
| 288 | /** @var IndexObject $indexObject */ |
||
| 289 | |||
| 290 | $absoluteLocalPath = $this->localPath . $indexObject->getRelativePath(); |
||
| 291 | |||
| 292 | $localObject = $localIndex->getObjectByPath($indexObject->getRelativePath()); |
||
| 293 | $remoteObject = $remoteIndex ? $remoteIndex->getObjectByPath($indexObject->getRelativePath()) : null; |
||
| 294 | |||
| 295 | |||
| 296 | if ($localObject !== null && $localObject->getType() !== $indexObject->getType()) |
||
| 297 | { |
||
| 298 | $operationCollection->addOperation(new UnlinkOperation($absoluteLocalPath)); |
||
| 299 | } |
||
| 300 | |||
| 301 | |||
| 302 | if ($indexObject->isDirectory()) |
||
| 303 | { |
||
| 304 | if ($localObject === null) |
||
| 305 | { |
||
| 306 | $operationCollection->addOperation(new MkdirOperation($absoluteLocalPath, $indexObject->getMode())); |
||
| 307 | } |
||
| 308 | elseif (!$localObject->isDirectory()) |
||
| 309 | { |
||
| 310 | $operationCollection->addOperation(new MkdirOperation($absoluteLocalPath, $indexObject->getMode())); |
||
| 311 | } |
||
| 312 | |||
| 313 | if ($localObject !== null && $localObject->isDirectory()) |
||
| 314 | { |
||
| 315 | if ($localObject->getMtime() !== $indexObject->getMtime()) |
||
| 316 | { |
||
| 317 | $directoryMtimes[$absoluteLocalPath] = $indexObject->getMtime(); |
||
| 318 | } |
||
| 319 | } |
||
| 320 | } |
||
| 321 | |||
| 322 | elseif ($indexObject->isFile()) |
||
| 323 | { |
||
| 324 | // local file did not exist, hasn't been a file before or is outdated |
||
| 325 | if ($localObject === null || !$localObject->isFile() || $localObject->getMtime() < $indexObject->getMtime()) |
||
| 326 | { |
||
| 327 | $operationCollection->addOperation(new DownloadOperation($absoluteLocalPath, $indexObject->getBlobId(), $this->vaultConnection)); |
||
| 328 | $operationCollection->addOperation(new TouchOperation($absoluteLocalPath, $indexObject->getMtime())); |
||
| 329 | $operationCollection->addOperation(new ChmodOperation($absoluteLocalPath, $indexObject->getMode())); |
||
| 330 | |||
| 331 | $directoryMtimes[dirname($absoluteLocalPath)] = $indexObject->getMtime(); |
||
| 332 | } |
||
| 333 | |||
| 334 | // local file got updated |
||
| 335 | elseif ($remoteObject === null || $indexObject->getBlobId() !== $remoteObject->getBlobId()) |
||
| 336 | { |
||
| 337 | // generate blob id |
||
| 338 | do |
||
| 339 | { |
||
| 340 | $blobId = $mergedIndex->generateNewBlobId(); |
||
| 341 | } |
||
| 342 | while ($this->vaultConnection->exists($blobId)); |
||
| 343 | |||
| 344 | $indexObject->setBlobId($blobId); |
||
| 345 | |||
| 346 | $operationCollection->addOperation(new UploadOperation($absoluteLocalPath, $indexObject->getBlobId(), $this->vaultConnection)); |
||
| 347 | } |
||
| 348 | } |
||
| 349 | |||
| 350 | elseif ($indexObject->isLink()) |
||
| 351 | { |
||
| 352 | $absoluteLinkTarget = dirname($absoluteLocalPath) . DIRECTORY_SEPARATOR . $indexObject->getLinkTarget(); |
||
| 353 | |||
| 354 | if ($localObject !== null && $localObject->getLinkTarget() !== $indexObject->getLinkTarget()) |
||
| 355 | { |
||
| 356 | $operationCollection->addOperation(new UnlinkOperation($absoluteLocalPath)); |
||
| 357 | $operationCollection->addOperation(new SymlinkOperation($absoluteLocalPath, $absoluteLinkTarget, $indexObject->getMode())); |
||
| 358 | } |
||
| 359 | } |
||
| 360 | |||
| 361 | else |
||
| 362 | { |
||
| 363 | // unknown object type |
||
| 364 | throw new \RuntimeException(); |
||
| 365 | } |
||
| 366 | |||
| 367 | |||
| 368 | if ($localObject !== null && $localObject->getMode() !== $indexObject->getMode()) |
||
| 369 | { |
||
| 370 | $operationCollection->addOperation(new ChmodOperation($absoluteLocalPath, $indexObject->getMode())); |
||
| 371 | } |
||
| 372 | } |
||
| 373 | |||
| 374 | // remove superfluous local files |
||
| 375 | foreach ($localIndex as $localObject) |
||
| 376 | { |
||
| 377 | /** @var IndexObject $localObject */ |
||
| 378 | |||
| 379 | if ($mergedIndex->getObjectByPath($localObject->getRelativePath()) === null) |
||
| 380 | { |
||
| 381 | $operationCollection->addOperation(new UnlinkOperation($this->localPath . $localObject->getRelativePath())); |
||
| 382 | } |
||
| 383 | } |
||
| 384 | |||
| 385 | // set directory mtimes |
||
| 386 | foreach ($directoryMtimes as $absoluteLocalPath => $mtime) |
||
| 387 | { |
||
| 388 | $operationCollection->addOperation(new TouchOperation($absoluteLocalPath, $mtime)); |
||
| 389 | } |
||
| 390 | |||
| 391 | return $operationCollection; |
||
| 392 | } |
||
| 393 | |||
| 426 |