Skip to content
Snippets Groups Projects
Commit f2535503 authored by Jan-Hendrik Willms's avatar Jan-Hendrik Willms
Browse files

remove zip archive legacy trait and adjust signature to match original and...

remove zip archive legacy trait and adjust signature to match original and satisfy phpstan, fixes #4696

Closes #4696

Merge request !3491
parent 57711844
No related branches found
No related tags found
No related merge requests found
......@@ -99,6 +99,7 @@
"ext-pdo": "*",
"ext-mbstring": "*",
"ext-dom": "*",
"ext-iconv": "*",
"opis/json-schema": "2.3.0",
"slim/slim": "4.13.0",
"php-di/php-di": "7.0.0",
......
<?php
namespace Studip;
if (version_compare(phpversion('zip'), '1.18') < 0) {
require_once __DIR__ . '/ZipArchiveLegacyTrait.php';
} else {
require_once __DIR__ . '/ZipArchiveTrait.php';
}
/**
* Custom derived ZipArchive class with convenience methods for
* zip archive handling.
......@@ -19,8 +13,6 @@ if (version_compare(phpversion('zip'), '1.18') < 0) {
*/
class ZipArchive extends \ZipArchive
{
use ZipArchiveTrait;
/**
* @var string encoding for filenames in zip
*/
......@@ -47,7 +39,7 @@ class ZipArchive extends \ZipArchive
* shall have the .zip file extension (true) or not (false).
* Defaults to false.
*
* @return Studip\ZipArchive
* @return static
*/
public static function create($filename, $force_zip_extension = false)
{
......@@ -141,8 +133,8 @@ class ZipArchive extends \ZipArchive
* Adds all files from a certain path.
*
* @param String $path Path name to add
* @return Array of local filenames
* @uses Studip\ZipArchive::addFile
* @return array of local filenames
* @uses ZipArchive::addFile
*/
public function addFromPath($path, $folder = '')
{
......@@ -155,13 +147,34 @@ class ZipArchive extends \ZipArchive
$result,
$this->addFromPath($file, $folder . basename($file) . '/')
);
} else {
$result[] = $this->addFile($file, $folder . basename($file));
} elseif ($this->addFile($file, $folder . basename($file))) {
$result[] = $this->convertLocalFilename($folder . basename($file));
}
}
return array_filter($result);
}
/**
* Adds a single file.
*
* @param string $filepath Name of the file to add
* @param ?string $entryname Name of the file inside the archive,
* will default to $filename
* @param int $start Unused but required (according to php doc)
* @param int $length Unused but required (according to php doc)
* @param int $flags Bitmask (see https://php.net/ziparchive.addfile)
*/
public function addFile(
string $filepath,
string $entryname = null,
int $start = 0,
int $length = 0,
int $flags = self::FL_OVERWRITE
): bool {
$localname = $this->convertLocalFilename($entryname ?: basename($filepath));
return parent::addFile($filepath, $localname, $start, $length, $flags);
}
/**
* Converts the filename to a format that a zip file should be able
* to handle.
......@@ -171,7 +184,7 @@ class ZipArchive extends \ZipArchive
*/
public function convertLocalFilename($filename)
{
if ($this->output_encoding != 'UTF-8') {
if ($this->output_encoding !== 'UTF-8') {
return iconv('UTF-8', $this->output_encoding . '//TRANSLIT', $filename);
} else {
return $filename;
......
<?php
namespace Studip;
/**
* Trait that handles the different method signatures in addFile() due to
* changes in the Zip module since v1.18. This is the legacy part.
*/
trait ZipArchiveTrait
{
/**
* Adds a single file.
*
* @param String $filename Name of the file to add
* @param String $localname Name of the file inside the archive,
* will default to $filename
* @param int $start Unused but required (according to php doc)
* @param int $length Unused but required (according to php doc)
* @return false on error, $localname otherwise
*/
public function addFile($filename, $localname = null, $start = 0, $length = 0)
{
$localname = $this->convertLocalFilename($localname ?: basename($filename));
return parent::addFile($filename, $localname, $start, $length)
? $localname
: false;
}
}
<?php
namespace Studip;
/**
* Trait that handles the different method signatures in addFile() due to
* changes in the Zip module since v1.18. This is the regular part.
*/
trait ZipArchiveTrait
{
/**
* Adds a single file.
*
* @param String $filename Name of the file to add
* @param String $localname Name of the file inside the archive,
* will default to $filename
* @param int $start Unused but required (according to php doc)
* @param int $length Unused but required (according to php doc)
* @param int $flags Bitmask (see https://php.net/ziparchive.addfile)
* @return false on error, $localname otherwise
*/
public function addFile($filename, $localname = null, $start = 0, $length = 0, $flags = \ZipArchive::FL_OVERWRITE)
{
$localname = $this->convertLocalFilename($localname ?: basename($filename));
return parent::addFile($filename, $localname, $start, $length, $flags)
? $localname
: false;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment