Skip to content

Commit

Permalink
refacto: type filesystem and filesystem interface (#710)
Browse files Browse the repository at this point in the history
* refacto: type filesystem and filesystem interface

* fix review

* fix return type createStream

* refactor: fix php-cs-fixer error

---------

Co-authored-by: PedroTroller <[email protected]>
  • Loading branch information
KevinArtus and PedroTroller authored Feb 26, 2024
1 parent 568a0c4 commit 0bbbb18
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 99 deletions.
2 changes: 1 addition & 1 deletion spec/Gaufrette/FileSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function it_sets_content_of_file(Filesystem $filesystem, MetadataAdapter $adapte
$filesystem->write('filename', 'some content', true)->shouldBeCalled()->willReturn(21);

$this->setContent('some content')->shouldReturn(21);
$this->getContent('filename')->shouldReturn('some content');
$this->getContent()->shouldReturn('some content');
}

function it_sets_key_as_name_by_default()
Expand Down
63 changes: 24 additions & 39 deletions src/Gaufrette/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
*/
class Filesystem implements FilesystemInterface
{
protected $adapter;
protected Adapter $adapter;

/**
* Contains File objects created with $this->createFile() method.
*
* @var array
* @var array<string, File>
*/
protected $fileRegister = [];
protected array $fileRegister = [];

/**
* @param Adapter $adapter A configured Adapter instance
Expand All @@ -29,20 +29,15 @@ public function __construct(Adapter $adapter)
$this->adapter = $adapter;
}

/**
* Returns the adapter.
*
* @return Adapter
*/
public function getAdapter()
public function getAdapter(): Adapter
{
return $this->adapter;
}

/**
* {@inheritdoc}
*/
public function has($key)
public function has(string $key): bool
{
self::assertValidKey($key);

Expand All @@ -52,7 +47,7 @@ public function has($key)
/**
* {@inheritdoc}
*/
public function rename($sourceKey, $targetKey)
public function rename(string $sourceKey, string $targetKey): bool
{
self::assertValidKey($sourceKey);
self::assertValidKey($targetKey);
Expand All @@ -78,7 +73,7 @@ public function rename($sourceKey, $targetKey)
/**
* {@inheritdoc}
*/
public function get($key, $create = false)
public function get(string $key, bool $create = false): File
{
self::assertValidKey($key);

Expand All @@ -92,7 +87,7 @@ public function get($key, $create = false)
/**
* {@inheritdoc}
*/
public function write($key, $content, $overwrite = false)
public function write(string $key, string $content, bool $overwrite = false): int
{
self::assertValidKey($key);

Expand All @@ -112,7 +107,7 @@ public function write($key, $content, $overwrite = false)
/**
* {@inheritdoc}
*/
public function read($key)
public function read(string $key): string
{
self::assertValidKey($key);

Expand All @@ -130,7 +125,7 @@ public function read($key)
/**
* {@inheritdoc}
*/
public function delete($key)
public function delete(string $key): bool
{
self::assertValidKey($key);

Expand All @@ -148,15 +143,15 @@ public function delete($key)
/**
* {@inheritdoc}
*/
public function keys()
public function keys(): array
{
return $this->adapter->keys();
}

/**
* {@inheritdoc}
*/
public function listKeys($prefix = '')
public function listKeys(string $prefix = ''): array
{
if ($this->adapter instanceof ListKeysAware) {
return $this->adapter->listKeys($prefix);
Expand Down Expand Up @@ -184,7 +179,7 @@ public function listKeys($prefix = '')
/**
* {@inheritdoc}
*/
public function mtime($key)
public function mtime(string $key): int|bool
{
self::assertValidKey($key);

Expand All @@ -196,7 +191,7 @@ public function mtime($key)
/**
* {@inheritdoc}
*/
public function checksum($key)
public function checksum(string $key): string
{
self::assertValidKey($key);

Expand All @@ -212,7 +207,7 @@ public function checksum($key)
/**
* {@inheritdoc}
*/
public function size($key)
public function size(string $key): int
{
self::assertValidKey($key);

Expand All @@ -228,7 +223,7 @@ public function size($key)
/**
* {@inheritdoc}
*/
public function createStream($key)
public function createStream(string $key): Stream
{
self::assertValidKey($key);

Expand All @@ -242,7 +237,7 @@ public function createStream($key)
/**
* {@inheritdoc}
*/
public function createFile($key)
public function createFile(string $key): File
{
self::assertValidKey($key);

Expand All @@ -260,7 +255,7 @@ public function createFile($key)
/**
* {@inheritdoc}
*/
public function mimeType($key)
public function mimeType(string $key): string|bool
{
self::assertValidKey($key);

Expand All @@ -282,11 +277,9 @@ public function mimeType($key)
* Key must be non empty string, otherwise it will throw Exception\FileNotFound
* {@see http://php.net/manual/en/function.empty.php}
*
* @param string $key
*
* @throws Exception\FileNotFound when sourceKey does not exist
*/
private function assertHasFile($key)
private function assertHasFile(string $key): void
{
if (!$this->has($key)) {
throw new Exception\FileNotFound($key);
Expand All @@ -295,30 +288,24 @@ private function assertHasFile($key)

/**
* Checks if matching File object by given key exists in the fileRegister.
*
* @param string $key
*
* @return bool
*/
private function isFileInRegister($key)
private function isFileInRegister(string $key): bool
{
return array_key_exists($key, $this->fileRegister);
}

/**
* Clear files register.
*/
public function clearFileRegister()
public function clearFileRegister(): void
{
$this->fileRegister = [];
}

/**
* Removes File object from register.
*
* @param string $key
*/
public function removeFromRegister($key)
public function removeFromRegister(string $key): void
{
if ($this->isFileInRegister($key)) {
unset($this->fileRegister[$key]);
Expand All @@ -328,17 +315,15 @@ public function removeFromRegister($key)
/**
* {@inheritdoc}
*/
public function isDirectory($key)
public function isDirectory(string $key): bool
{
return $this->adapter->isDirectory($key);
}

/**
* @param string $key
*
* @throws \InvalidArgumentException Given $key should not be empty
*/
private static function assertValidKey($key)
private static function assertValidKey(string $key): void
{
if (empty($key)) {
throw new \InvalidArgumentException('Object path is empty.');
Expand Down
Loading

0 comments on commit 0bbbb18

Please sign in to comment.