From b05376b8edcf977afba67f6b4a85be4d96f80c64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin?= Date: Mon, 24 Apr 2023 15:22:50 +0200 Subject: [PATCH] Type PhpseclibSftp --- src/Gaufrette/Adapter/PhpseclibSftp.php | 48 ++++++++++++------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/src/Gaufrette/Adapter/PhpseclibSftp.php b/src/Gaufrette/Adapter/PhpseclibSftp.php index 2e71d625..e16e0901 100644 --- a/src/Gaufrette/Adapter/PhpseclibSftp.php +++ b/src/Gaufrette/Adapter/PhpseclibSftp.php @@ -9,31 +9,27 @@ class PhpseclibSftp implements Adapter, FileFactory, ListKeysAware { - protected $sftp; - protected $directory; - protected $create; - protected $initialized = false; + protected bool $initialized = false; /** - * @param SecLibSFTP $sftp An Sftp instance * @param string $directory The distant directory * @param bool $create Whether to create the remote directory if it * does not exist */ - public function __construct(SecLibSFTP $sftp, $directory = null, $create = false) - { + public function __construct( + private readonly SecLibSFTP $sftp, + private readonly ?string $directory = null, + private readonly bool $create = false + ) { if (!class_exists(SecLibSFTP::class)) { throw new \LogicException('You need to install package "phpseclib/phpseclib" to use this adapter'); } - $this->sftp = $sftp; - $this->directory = $directory; - $this->create = $create; } /** * {@inheritdoc} */ - public function read($key) + public function read(string $key): string|bool { return $this->sftp->get($this->computePath($key)); } @@ -41,7 +37,7 @@ public function read($key) /** * {@inheritdoc} */ - public function rename($sourceKey, $targetKey) + public function rename(string $sourceKey, string $targetKey): bool { $this->initialize(); @@ -56,7 +52,7 @@ public function rename($sourceKey, $targetKey) /** * {@inheritdoc} */ - public function write($key, $content) + public function write(string $key, mixed $content): int|bool { $this->initialize(); @@ -72,7 +68,7 @@ public function write($key, $content) /** * {@inheritdoc} */ - public function exists($key) + public function exists(string $key): bool { $this->initialize(); @@ -82,7 +78,7 @@ public function exists($key) /** * {@inheritdoc} */ - public function isDirectory($key) + public function isDirectory(string $key): bool { $this->initialize(); @@ -99,7 +95,7 @@ public function isDirectory($key) /** * {@inheritdoc} */ - public function keys() + public function keys(): array { $keys = $this->fetchKeys(); @@ -109,7 +105,7 @@ public function keys() /** * {@inheritdoc} */ - public function listKeys($prefix = '') + public function listKeys(string $prefix = ''): array { preg_match('/(.*?)[^\/]*$/', $prefix, $match); $directory = rtrim($match[1], '/'); @@ -136,7 +132,7 @@ public function listKeys($prefix = '') /** * {@inheritdoc} */ - public function mtime($key) + public function mtime(string $key): int|bool { $this->initialize(); @@ -148,7 +144,7 @@ public function mtime($key) /** * {@inheritdoc} */ - public function delete($key) + public function delete(string $key): bool { return $this->sftp->delete($this->computePath($key), false); } @@ -156,7 +152,7 @@ public function delete($key) /** * {@inheritdoc} */ - public function createFile($key, Filesystem $filesystem) + public function createFile(string $key, Filesystem $filesystem): File { $file = new File($key, $filesystem); @@ -173,7 +169,8 @@ public function createFile($key, Filesystem $filesystem) * * It will ensure the root directory exists */ - protected function initialize() + + protected function initialize(): void { if ($this->initialized) { return; @@ -184,7 +181,7 @@ protected function initialize() $this->initialized = true; } - protected function ensureDirectoryExists($directory, $create) + protected function ensureDirectoryExists(string $directory, bool $create) { $pwd = $this->sftp->pwd(); if ($this->sftp->chdir($directory)) { @@ -198,12 +195,15 @@ protected function ensureDirectoryExists($directory, $create) } } - protected function computePath($key) + protected function computePath(string $key): string { return $this->directory . '/' . ltrim($key, '/'); } - protected function fetchKeys($directory = '', $onlyKeys = true) + /** + * @return array> + */ + protected function fetchKeys(string $directory = '', bool $onlyKeys = true): array { $keys = ['keys' => [], 'dirs' => []]; $computedPath = $this->computePath($directory);