Skip to content

Commit

Permalink
Prevent copy and move to same destinations as the source.
Browse files Browse the repository at this point in the history
  • Loading branch information
frankdejonge committed Oct 19, 2023
1 parent bd4c9b2 commit 22b7be2
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 10 deletions.
27 changes: 17 additions & 10 deletions src/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,20 +119,27 @@ private function pipeListing(string $location, bool $deep, iterable $listing): G

public function move(string $source, string $destination, array $config = []): void
{
$this->adapter->move(
$this->pathNormalizer->normalizePath($source),
$this->pathNormalizer->normalizePath($destination),
$this->config->extend($config)
);
$from = $this->pathNormalizer->normalizePath($source);
$to = $this->pathNormalizer->normalizePath($destination);

if ($from === $to) {
throw UnableToMoveFile::sourceAndDestinationAreTheSame($source, $destination);
throw UnableToMoveFile::because('Source and destination are the same', $source, $destination);

Check failure on line 127 in src/Filesystem.php

View workflow job for this annotation

GitHub Actions / PHPUnit tests on 8.0

Unreachable statement - code above always terminates.

Check failure on line 127 in src/Filesystem.php

View workflow job for this annotation

GitHub Actions / PHPUnit tests on 8.1

Unreachable statement - code above always terminates.
}

$this->adapter->move($from, $to, $this->config->extend($config));
}

public function copy(string $source, string $destination, array $config = []): void
{
$this->adapter->copy(
$this->pathNormalizer->normalizePath($source),
$this->pathNormalizer->normalizePath($destination),
$this->config->extend($config)
);
$from = $this->pathNormalizer->normalizePath($source);
$to = $this->pathNormalizer->normalizePath($destination);

if ($from === $to) {
throw UnableToCopyFile::sourceAndDestinationAreTheSame($source, $destination);
}

$this->adapter->copy($from, $to, $this->config->extend($config));
}

public function lastModified(string $path): int
Expand Down
20 changes: 20 additions & 0 deletions src/FilesystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,26 @@ public function publicUrl(string $path, Config $config): string
self::assertSame('custom/file.txt', $filesystem->publicUrl('file.txt'));
}

/**
* @test
*/
public function copying_from_and_to_the_same_location_fails(): void
{
$this->expectExceptionObject(UnableToMoveFile::sourceAndDestinationAreTheSame('from.txt', 'from.txt'));

$this->filesystem->move('from.txt', 'from.txt');
}

/**
* @test
*/
public function moving_from_and_to_the_same_location_fails(): void
{
$this->expectExceptionObject(UnableToCopyFile::sourceAndDestinationAreTheSame('from.txt', 'from.txt'));

$this->filesystem->copy('from.txt', 'from.txt');
}

/**
* @test
*/
Expand Down
14 changes: 14 additions & 0 deletions src/UnableToCopyFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ public static function fromLocationTo(
return $e;
}

public static function sourceAndDestinationAreTheSame(string $source, string $destination): UnableToCopyFile
{
return UnableToCopyFile::because('Source and destination are the same', $source, $destination);
}

public static function because(string $reason, string $sourcePath, string $destinationPath): UnableToCopyFile
{
$e = new static("Unable to copy file from $sourcePath to $destinationPath, because $reason");
$e->source = $sourcePath;
$e->destination = $destinationPath;

return $e;
}

public function operation(): string
{
return FilesystemOperationFailed::OPERATION_COPY;
Expand Down
18 changes: 18 additions & 0 deletions src/UnableToMoveFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ final class UnableToMoveFile extends RuntimeException implements FilesystemOpera
*/
private $destination;

public static function sourceAndDestinationAreTheSame(string $source, string $destination): UnableToMoveFile
{
return UnableToMoveFile::because('Source and destination are the same', $source, $destination);
}

public function source(): string
{
return $this->source;
Expand All @@ -42,6 +47,19 @@ public static function fromLocationTo(
return $e;
}

public static function because(
string $reason,
string $sourcePath,
string $destinationPath,
): UnableToMoveFile {
$message = "Unable to move file from $sourcePath to $destinationPath, because $reason";
$e = new static($message);
$e->source = $sourcePath;
$e->destination = $destinationPath;

return $e;
}

public function operation(): string
{
return FilesystemOperationFailed::OPERATION_MOVE;
Expand Down

0 comments on commit 22b7be2

Please sign in to comment.