From 7b1a0441312124784b9d6efab72c8c496f8f7c69 Mon Sep 17 00:00:00 2001 From: =?utf8?q?C=C3=B4me=20Chilliet?= Date: Thu, 24 Feb 2022 12:03:36 +0100 Subject: [PATCH] Improve typing in OC\Archive classes MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- lib/private/Archive/Archive.php | 7 ++++--- lib/private/Archive/TAR.php | 31 +++++++++++++++++++++++++------ lib/private/Archive/ZIP.php | 30 +++++++++++++++++++++--------- 3 files changed, 50 insertions(+), 18 deletions(-) diff --git a/lib/private/Archive/Archive.php b/lib/private/Archive/Archive.php index 141db11f419..4f8100530ac 100644 --- a/lib/private/Archive/Archive.php +++ b/lib/private/Archive/Archive.php @@ -31,7 +31,7 @@ namespace OC\Archive; abstract class Archive { /** - * @param $source + * @param string $source */ abstract public function __construct($source); /** @@ -80,14 +80,14 @@ abstract class Archive { /** * get the content of a file * @param string $path - * @return string + * @return string|false */ abstract public function getFile($path); /** * extract a single file from the archive * @param string $path * @param string $dest - * @return bool + * @return bool success */ abstract public function extractFile($path, $dest); /** @@ -119,6 +119,7 @@ abstract class Archive { * add a folder and all its content * @param string $path * @param string $source + * @return void */ public function addRecursive($path, $source) { $dh = opendir($source); diff --git a/lib/private/Archive/TAR.php b/lib/private/Archive/TAR.php index a024ce84b2f..1b8f72fd6ce 100644 --- a/lib/private/Archive/TAR.php +++ b/lib/private/Archive/TAR.php @@ -39,13 +39,23 @@ class TAR extends Archive { public const GZIP = 1; public const BZIP = 2; - private $fileList; - private $cachedHeaders; + /** + * @var string[]|false + */ + private $fileList = false; + /** + * @var array|false + */ + private $cachedHeaders = false; /** - * @var \Archive_Tar tar + * @var \Archive_Tar */ private $tar = null; + + /** + * @var string + */ private $path; /** @@ -154,6 +164,7 @@ class TAR extends Archive { /** * @param string $file + * @return array|null */ private function getHeader($file) { if (!$this->cachedHeaders) { @@ -244,10 +255,15 @@ class TAR extends Archive { * get the content of a file * * @param string $path - * @return string + * @return string|false */ public function getFile($path) { - return $this->tar->extractInString($path); + $string = $this->tar->extractInString($path); + if (is_string($string)) { + return $string; + } else { + return false; + } } /** @@ -364,6 +380,9 @@ class TAR extends Archive { /** * write back temporary files + * @param string $tmpFile + * @param string $path + * @return void */ public function writeBack($tmpFile, $path) { $this->addFile($path, $tmpFile); @@ -373,7 +392,7 @@ class TAR extends Archive { /** * reopen the archive to ensure everything is written */ - private function reopen() { + private function reopen(): void { if ($this->tar) { $this->tar->_close(); $this->tar = null; diff --git a/lib/private/Archive/ZIP.php b/lib/private/Archive/ZIP.php index e058d476021..8c1b162206d 100644 --- a/lib/private/Archive/ZIP.php +++ b/lib/private/Archive/ZIP.php @@ -33,12 +33,17 @@ namespace OC\Archive; use Icewind\Streams\CallbackWrapper; use OCP\ILogger; +use Psr\Log\LoggerInterface; class ZIP extends Archive { /** * @var \ZipArchive zip */ - private $zip = null; + private $zip; + + /** + * @var string + */ private $path; /** @@ -49,7 +54,7 @@ class ZIP extends Archive { $this->zip = new \ZipArchive(); if ($this->zip->open($source, \ZipArchive::CREATE)) { } else { - \OCP\Util::writeLog('files_archive', 'Error while opening archive '.$source, ILogger::WARN); + \OC::$server->get(LoggerInterface::class)->warning('Error while opening archive '.$source, ['app' => 'files_archive']); } } /** @@ -82,12 +87,12 @@ class ZIP extends Archive { * rename a file or folder in the archive * @param string $source * @param string $dest - * @return boolean|null + * @return bool */ public function rename($source, $dest) { $source = $this->stripPath($source); $dest = $this->stripPath($dest); - $this->zip->renameName($source, $dest); + return $this->zip->renameName($source, $dest); } /** * get the uncompressed size of a file in the archive @@ -139,7 +144,7 @@ class ZIP extends Archive { /** * get the content of a file * @param string $path - * @return string + * @return string|false */ public function getFile($path) { return $this->zip->getFromName($path); @@ -148,11 +153,14 @@ class ZIP extends Archive { * extract a single file from the archive * @param string $path * @param string $dest - * @return boolean|null + * @return bool */ public function extractFile($path, $dest) { $fp = $this->zip->getStream($path); - file_put_contents($dest, $fp); + if ($fp === false) { + return false; + } + return file_put_contents($dest, $fp) !== false; } /** * extract the archive @@ -195,8 +203,9 @@ class ZIP extends Archive { //since we can't directly get a writable stream, //make a temp copy of the file and put it back //in the archive when the stream is closed - if (strrpos($path, '.') !== false) { - $ext = substr($path, strrpos($path, '.')); + $lastPoint = strrpos($path, '.'); + if ($lastPoint !== false) { + $ext = substr($path, $lastPoint); } else { $ext = ''; } @@ -213,6 +222,9 @@ class ZIP extends Archive { /** * write back temporary files + * @param string $tmpFile + * @param string $path + * @return void */ public function writeBack($tmpFile, $path) { $this->addFile($path, $tmpFile); -- 2.39.5