Skip to content

Commit

Permalink
refacto: type filesystem and filesystem interface
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinArtus committed Oct 30, 2023
1 parent 0cb8b54 commit 4efd05c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 95 deletions.
61 changes: 23 additions & 38 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
*/
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|Stream\InMemoryBuffer
{
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 4efd05c

Please sign in to comment.