1
0
mirror of https://github.com/Ne-Lexa/php-zip.git synced 2025-08-02 13:37:25 +02:00

added static analysis

This commit is contained in:
Ne-Lexa
2021-02-23 19:44:48 +03:00
parent a85288d34b
commit 00dd097b2d
21 changed files with 84 additions and 86 deletions

View File

@@ -54,7 +54,7 @@ jobs:
extensions: ${{ env.extensions }} extensions: ${{ env.extensions }}
coverage: pcov coverage: pcov
ini-values: ${{ env.PHP_INI }} ini-values: ${{ env.PHP_INI }}
tools: composer:v2 tools: composer:v2, cs2pr
- -
name: Determine composer cache directory on Linux or MacOS name: Determine composer cache directory on Linux or MacOS
@@ -109,6 +109,10 @@ jobs:
if: env.PHPUNIT_COVERAGE == 1 if: env.PHPUNIT_COVERAGE == 1
run: vendor/bin/phpunit -v --coverage-clover=coverage.clover run: vendor/bin/phpunit -v --coverage-clover=coverage.clover
-
name: Static analysis
run: vendor/bin/psalm --shepherd --stats --output-format=checkstyle | cs2pr --graceful-warnings --colorize
- -
name: Upload code coverage scrutinizer name: Upload code coverage scrutinizer
if: env.PHPUNIT_COVERAGE == 1 if: env.PHPUNIT_COVERAGE == 1

View File

@@ -59,7 +59,7 @@ class ResponseStream implements StreamInterface
], ],
]; ];
/** @var resource */ /** @var resource|null */
private $stream; private $stream;
private ?int $size = null; private ?int $size = null;
@@ -97,7 +97,7 @@ class ResponseStream implements StreamInterface
*/ */
public function getMetadata($key = null) public function getMetadata($key = null)
{ {
if (!$this->stream) { if ($this->stream === null) {
return $key ? null : []; return $key ? null : [];
} }
$meta = stream_get_meta_data($this->stream); $meta = stream_get_meta_data($this->stream);
@@ -141,7 +141,7 @@ class ResponseStream implements StreamInterface
*/ */
public function rewind(): void public function rewind(): void
{ {
$this->seekable && rewind($this->stream); $this->stream !== null && $this->seekable && rewind($this->stream);
} }
/** /**
@@ -199,7 +199,7 @@ class ResponseStream implements StreamInterface
*/ */
public function seek($offset, $whence = \SEEK_SET): void public function seek($offset, $whence = \SEEK_SET): void
{ {
$this->seekable && fseek($this->stream, $offset, $whence); $this->stream !== null && $this->seekable && fseek($this->stream, $offset, $whence);
} }
/** /**
@@ -217,7 +217,7 @@ class ResponseStream implements StreamInterface
{ {
$this->size = null; $this->size = null;
return $this->writable ? fwrite($this->stream, $string) : false; return $this->stream !== null && $this->writable ? fwrite($this->stream, $string) : false;
} }
/** /**
@@ -233,7 +233,7 @@ class ResponseStream implements StreamInterface
*/ */
public function read($length): string public function read($length): string
{ {
return $this->readable ? fread($this->stream, $length) : ''; return $this->stream !== null && $this->readable ? fread($this->stream, $length) : '';
} }
/** /**
@@ -257,6 +257,8 @@ class ResponseStream implements StreamInterface
/** /**
* Closes the stream and any underlying resources. * Closes the stream and any underlying resources.
*
* @psalm-suppress InvalidPropertyAssignmentValue
*/ */
public function close(): void public function close(): void
{ {

View File

@@ -370,13 +370,10 @@ class ZipReader
if ($unicodePathExtraField !== null && $unicodePathExtraField->getCrc32() === crc32($entryName)) { if ($unicodePathExtraField !== null && $unicodePathExtraField->getCrc32() === crc32($entryName)) {
$unicodePath = $unicodePathExtraField->getUnicodeValue(); $unicodePath = $unicodePathExtraField->getUnicodeValue();
if ($unicodePath !== null) { if ($unicodePath !== '') {
$unicodePath = str_replace('\\', '/', $unicodePath); $unicodePath = str_replace('\\', '/', $unicodePath);
if ( if (substr_count($entryName, '/') === substr_count($unicodePath, '/')) {
$unicodePath !== ''
&& substr_count($entryName, '/') === substr_count($unicodePath, '/')
) {
$entryName = $unicodePath; $entryName = $unicodePath;
} }
} }
@@ -869,6 +866,9 @@ class ZipReader
return \PHP_INT_SIZE === 8; // true for 64bit system return \PHP_INT_SIZE === 8; // true for 64bit system
} }
/**
* @psalm-suppress InvalidPropertyAssignmentValue
*/
public function close(): void public function close(): void
{ {
if (\is_resource($this->inStream)) { if (\is_resource($this->inStream)) {

View File

@@ -71,11 +71,8 @@ class ZipFileData implements ZipData
*/ */
public function copyDataToStream($outStream): void public function copyDataToStream($outStream): void
{ {
try {
$stream = $this->getDataAsStream(); $stream = $this->getDataAsStream();
stream_copy_to_stream($stream, $outStream); stream_copy_to_stream($stream, $outStream);
} finally {
fclose($stream); fclose($stream);
} }
}
} }

View File

@@ -63,7 +63,7 @@ abstract class AbstractUnicodeExtraField implements ZipExtraField
* Populate data from this array as if it was in local file data. * Populate data from this array as if it was in local file data.
* *
* @param string $buffer the buffer to read data from * @param string $buffer the buffer to read data from
* @param ?ZipEntry $entry * @param ZipEntry|null $entry optional zip entry
* *
* @throws ZipException on error * @throws ZipException on error
* *
@@ -93,7 +93,7 @@ abstract class AbstractUnicodeExtraField implements ZipExtraField
* Populate data from this array as if it was in central directory data. * Populate data from this array as if it was in central directory data.
* *
* @param string $buffer the buffer to read data from * @param string $buffer the buffer to read data from
* @param ?ZipEntry $entry * @param ZipEntry|null $entry optional zip entry
* *
* @throws ZipException on error * @throws ZipException on error
* *

View File

@@ -21,7 +21,7 @@ use PhpZip\Model\ZipEntry;
* @see https://android.googlesource.com/platform/tools/apksig/+/master/src/main/java/com/android/apksig/ApkSigner.java * @see https://android.googlesource.com/platform/tools/apksig/+/master/src/main/java/com/android/apksig/ApkSigner.java
* @see https://developer.android.com/studio/command-line/zipalign * @see https://developer.android.com/studio/command-line/zipalign
*/ */
class ApkAlignmentExtraField implements ZipExtraField final class ApkAlignmentExtraField implements ZipExtraField
{ {
/** /**
* @var int Extensible data block/field header ID used for storing * @var int Extensible data block/field header ID used for storing
@@ -31,12 +31,6 @@ class ApkAlignmentExtraField implements ZipExtraField
*/ */
public const HEADER_ID = 0xd935; public const HEADER_ID = 0xd935;
/**
* @var int minimum size (in bytes) of the extensible data block/field used
* for alignment of uncompressed entries
*/
public const MIN_SIZE = 6;
/** @var int */ /** @var int */
public const ALIGNMENT_BYTES = 4; public const ALIGNMENT_BYTES = 4;
@@ -87,7 +81,7 @@ class ApkAlignmentExtraField implements ZipExtraField
* Populate data from this array as if it was in local file data. * Populate data from this array as if it was in local file data.
* *
* @param string $buffer the buffer to read data from * @param string $buffer the buffer to read data from
* @param ?ZipEntry $entry * @param ZipEntry|null $entry optional zip entry
* *
* @throws ZipException * @throws ZipException
* *
@@ -117,7 +111,7 @@ class ApkAlignmentExtraField implements ZipExtraField
* Populate data from this array as if it was in central directory data. * Populate data from this array as if it was in central directory data.
* *
* @param string $buffer the buffer to read data from * @param string $buffer the buffer to read data from
* @param ?ZipEntry $entry * @param ZipEntry|null $entry optional zip entry
* *
* @throws ZipException on error * @throws ZipException on error
* *

View File

@@ -53,7 +53,7 @@ use PhpZip\Model\ZipEntry;
* *
* @see ftp://ftp.info-zip.org/pub/infozip/doc/appnote-iz-latest.zip Info-ZIP version Specification * @see ftp://ftp.info-zip.org/pub/infozip/doc/appnote-iz-latest.zip Info-ZIP version Specification
*/ */
class AsiExtraField implements ZipExtraField final class AsiExtraField implements ZipExtraField
{ {
/** @var int Header id */ /** @var int Header id */
public const HEADER_ID = 0x756e; public const HEADER_ID = 0x756e;
@@ -100,11 +100,11 @@ class AsiExtraField implements ZipExtraField
* Populate data from this array as if it was in local file data. * Populate data from this array as if it was in local file data.
* *
* @param string $buffer the buffer to read data from * @param string $buffer the buffer to read data from
* @param ?ZipEntry $entry * @param ZipEntry|null $entry optional zip entry
* *
* @throws Crc32Exception * @throws Crc32Exception
* *
* @return static * @return AsiExtraField
*/ */
public static function unpackLocalFileData(string $buffer, ?ZipEntry $entry = null): self public static function unpackLocalFileData(string $buffer, ?ZipEntry $entry = null): self
{ {
@@ -135,7 +135,7 @@ class AsiExtraField implements ZipExtraField
* Populate data from this array as if it was in central directory data. * Populate data from this array as if it was in central directory data.
* *
* @param string $buffer the buffer to read data from * @param string $buffer the buffer to read data from
* @param ?ZipEntry $entry * @param ZipEntry|null $entry optional zip entry
* *
* @throws Crc32Exception * @throws Crc32Exception
* *
@@ -216,7 +216,7 @@ class AsiExtraField implements ZipExtraField
* *
* @return int the type with the mode * @return int the type with the mode
*/ */
protected function getPermissionsMode(int $mode): int private function getPermissionsMode(int $mode): int
{ {
$type = 0; $type = 0;

View File

@@ -73,7 +73,7 @@ use PhpZip\Model\ZipEntry;
* *
* @see ftp://ftp.info-zip.org/pub/infozip/doc/appnote-iz-latest.zip Info-ZIP version Specification * @see ftp://ftp.info-zip.org/pub/infozip/doc/appnote-iz-latest.zip Info-ZIP version Specification
*/ */
class ExtendedTimestampExtraField implements ZipExtraField final class ExtendedTimestampExtraField implements ZipExtraField
{ {
/** @var int Header id */ /** @var int Header id */
public const HEADER_ID = 0x5455; public const HEADER_ID = 0x5455;
@@ -159,7 +159,7 @@ class ExtendedTimestampExtraField implements ZipExtraField
* Populate data from this array as if it was in local file data. * Populate data from this array as if it was in local file data.
* *
* @param string $buffer the buffer to read data from * @param string $buffer the buffer to read data from
* @param ?ZipEntry $entry * @param ZipEntry|null $entry optional zip entry
* *
* @return ExtendedTimestampExtraField * @return ExtendedTimestampExtraField
*/ */
@@ -196,7 +196,7 @@ class ExtendedTimestampExtraField implements ZipExtraField
* Populate data from this array as if it was in central directory data. * Populate data from this array as if it was in central directory data.
* *
* @param string $buffer the buffer to read data from * @param string $buffer the buffer to read data from
* @param ?ZipEntry $entry * @param ZipEntry|null $entry optional zip entry
* *
* @return ExtendedTimestampExtraField * @return ExtendedTimestampExtraField
*/ */

View File

@@ -24,7 +24,7 @@ use PhpZip\Model\ZipEntry;
* If this extra field is added as the very first extra field of * If this extra field is added as the very first extra field of
* the archive, Solaris will consider it an executable jar file. * the archive, Solaris will consider it an executable jar file.
*/ */
class JarMarkerExtraField implements ZipExtraField final class JarMarkerExtraField implements ZipExtraField
{ {
/** @var int Header id. */ /** @var int Header id. */
public const HEADER_ID = 0xCAFE; public const HEADER_ID = 0xCAFE;
@@ -80,7 +80,7 @@ class JarMarkerExtraField implements ZipExtraField
* Populate data from this array as if it was in local file data. * Populate data from this array as if it was in local file data.
* *
* @param string $buffer the buffer to read data from * @param string $buffer the buffer to read data from
* @param ?ZipEntry $entry * @param ZipEntry|null $entry optional zip entry
* *
* @throws ZipException on error * @throws ZipException on error
* *
@@ -99,7 +99,7 @@ class JarMarkerExtraField implements ZipExtraField
* Populate data from this array as if it was in central directory data. * Populate data from this array as if it was in central directory data.
* *
* @param string $buffer the buffer to read data from * @param string $buffer the buffer to read data from
* @param ?ZipEntry $entry * @param ZipEntry|null $entry optional zip entry
* *
* @throws ZipException on error * @throws ZipException on error
* *

View File

@@ -51,7 +51,7 @@ use PhpZip\Model\ZipEntry;
* and this extra field are present, the values in this extra field * and this extra field are present, the values in this extra field
* supercede the values in that extra field. * supercede the values in that extra field.
*/ */
class NewUnixExtraField implements ZipExtraField final class NewUnixExtraField implements ZipExtraField
{ {
/** @var int header id */ /** @var int header id */
public const HEADER_ID = 0x7875; public const HEADER_ID = 0x7875;
@@ -89,7 +89,7 @@ class NewUnixExtraField implements ZipExtraField
* Populate data from this array as if it was in local file data. * Populate data from this array as if it was in local file data.
* *
* @param string $buffer the buffer to read data from * @param string $buffer the buffer to read data from
* @param ?ZipEntry $entry * @param ZipEntry|null $entry optional zip entry
* *
* @throws ZipException * @throws ZipException
* *
@@ -121,7 +121,7 @@ class NewUnixExtraField implements ZipExtraField
* Populate data from this array as if it was in central directory data. * Populate data from this array as if it was in central directory data.
* *
* @param string $buffer the buffer to read data from * @param string $buffer the buffer to read data from
* @param ?ZipEntry $entry * @param ZipEntry|null $entry optional zip entry
* *
* @throws ZipException * @throws ZipException
* *

View File

@@ -21,7 +21,7 @@ use PhpZip\Model\ZipEntry;
* *
* @see https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT .ZIP File Format Specification * @see https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT .ZIP File Format Specification
*/ */
class NtfsExtraField implements ZipExtraField final class NtfsExtraField implements ZipExtraField
{ {
/** @var int Header id */ /** @var int Header id */
public const HEADER_ID = 0x000a; public const HEADER_ID = 0x000a;
@@ -85,7 +85,7 @@ class NtfsExtraField implements ZipExtraField
* Populate data from this array as if it was in local file data. * Populate data from this array as if it was in local file data.
* *
* @param string $buffer the buffer to read data from * @param string $buffer the buffer to read data from
* @param ?ZipEntry $entry * @param ZipEntry|null $entry optional zip entry
* *
* @throws ZipException * @throws ZipException
* *
@@ -128,7 +128,7 @@ class NtfsExtraField implements ZipExtraField
* Populate data from this array as if it was in central directory data. * Populate data from this array as if it was in central directory data.
* *
* @param string $buffer the buffer to read data from * @param string $buffer the buffer to read data from
* @param ?ZipEntry $entry * @param ZipEntry|null $entry optional zip entry
* *
* @throws ZipException * @throws ZipException
* *

View File

@@ -62,7 +62,7 @@ use PhpZip\Model\ZipEntry;
* mid-1994. Therefore future archiving software should continue to * mid-1994. Therefore future archiving software should continue to
* support it. * support it.
*/ */
class OldUnixExtraField implements ZipExtraField final class OldUnixExtraField implements ZipExtraField
{ {
/** @var int Header id */ /** @var int Header id */
public const HEADER_ID = 0x5855; public const HEADER_ID = 0x5855;
@@ -101,7 +101,7 @@ class OldUnixExtraField implements ZipExtraField
* Populate data from this array as if it was in local file data. * Populate data from this array as if it was in local file data.
* *
* @param string $buffer the buffer to read data from * @param string $buffer the buffer to read data from
* @param ?ZipEntry $entry * @param ZipEntry|null $entry optional zip entry
* *
* @return OldUnixExtraField * @return OldUnixExtraField
*/ */
@@ -134,7 +134,7 @@ class OldUnixExtraField implements ZipExtraField
* Populate data from this array as if it was in central directory data. * Populate data from this array as if it was in central directory data.
* *
* @param string $buffer the buffer to read data from * @param string $buffer the buffer to read data from
* @param ?ZipEntry $entry * @param ZipEntry|null $entry optional zip entry
* *
* @return OldUnixExtraField * @return OldUnixExtraField
*/ */

View File

@@ -55,7 +55,7 @@ namespace PhpZip\Model\Extra\Fields;
* *
* @see https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT section 4.6.8 * @see https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT section 4.6.8
*/ */
class UnicodeCommentExtraField extends AbstractUnicodeExtraField final class UnicodeCommentExtraField extends AbstractUnicodeExtraField
{ {
public const HEADER_ID = 0x6375; public const HEADER_ID = 0x6375;

View File

@@ -56,7 +56,7 @@ namespace PhpZip\Model\Extra\Fields;
* *
* @see https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT section 4.6.9 * @see https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT section 4.6.9
*/ */
class UnicodePathExtraField extends AbstractUnicodeExtraField final class UnicodePathExtraField extends AbstractUnicodeExtraField
{ {
public const HEADER_ID = 0x7075; public const HEADER_ID = 0x7075;

View File

@@ -18,7 +18,7 @@ use PhpZip\Model\ZipEntry;
/** /**
* Simple placeholder for all those extra fields we don't want to deal with. * Simple placeholder for all those extra fields we don't want to deal with.
*/ */
class UnrecognizedExtraField implements ZipExtraField final class UnrecognizedExtraField implements ZipExtraField
{ {
private int $headerId; private int $headerId;

View File

@@ -24,7 +24,7 @@ use PhpZip\Model\ZipEntry;
* *
* @see http://www.winzip.com/win/en/aes_tips.htm AES Coding Tips for Developers * @see http://www.winzip.com/win/en/aes_tips.htm AES Coding Tips for Developers
*/ */
class WinZipAesExtraField implements ZipExtraField final class WinZipAesExtraField implements ZipExtraField
{ {
/** @var int Header id */ /** @var int Header id */
public const HEADER_ID = 0x9901; public const HEADER_ID = 0x9901;

View File

@@ -22,7 +22,7 @@ use PhpZip\Model\ZipEntry;
* *
* @see https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT .ZIP File Format Specification * @see https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT .ZIP File Format Specification
*/ */
class Zip64ExtraField implements ZipExtraField final class Zip64ExtraField implements ZipExtraField
{ {
/** @var int The Header ID for a ZIP64 Extended Information Extra Field. */ /** @var int The Header ID for a ZIP64 Extended Information Extra Field. */
public const HEADER_ID = 0x0001; public const HEADER_ID = 0x0001;

View File

@@ -31,7 +31,7 @@ interface ZipExtraField
* Populate data from this array as if it was in local file data. * Populate data from this array as if it was in local file data.
* *
* @param string $buffer the buffer to read data from * @param string $buffer the buffer to read data from
* @param ?ZipEntry $entry * @param ZipEntry|null $entry optional zip entry
* *
* @return static * @return static
*/ */
@@ -41,7 +41,7 @@ interface ZipExtraField
* Populate data from this array as if it was in central directory data. * Populate data from this array as if it was in central directory data.
* *
* @param string $buffer the buffer to read data from * @param string $buffer the buffer to read data from
* @param ?ZipEntry $entry * @param ZipEntry|null $entry optional zip entry
* *
* @return static * @return static
*/ */

View File

@@ -943,13 +943,10 @@ class ZipEntry
*/ */
public function setEncryptionMethod(?int $encryptionMethod): self public function setEncryptionMethod(?int $encryptionMethod): self
{ {
if ($encryptionMethod === null) { $method = $encryptionMethod ?? ZipEncryptionMethod::NONE;
$encryptionMethod = ZipEncryptionMethod::NONE;
}
$encryptionMethod = (int) $encryptionMethod; ZipEncryptionMethod::checkSupport($method);
ZipEncryptionMethod::checkSupport($encryptionMethod); $this->encryptionMethod = $method;
$this->encryptionMethod = $encryptionMethod;
$this->setEncrypted($this->encryptionMethod !== ZipEncryptionMethod::NONE); $this->setEncrypted($this->encryptionMethod !== ZipEncryptionMethod::NONE);
$this->extractVersion = self::UNKNOWN; $this->extractVersion = self::UNKNOWN;

View File

@@ -62,10 +62,11 @@ class IgnoreFilesRecursiveFilterIterator extends \RecursiveFilterIterator
/** /**
* @return IgnoreFilesRecursiveFilterIterator * @return IgnoreFilesRecursiveFilterIterator
* @psalm-suppress UndefinedInterfaceMethod
* @noinspection PhpPossiblePolymorphicInvocationInspection
*/ */
public function getChildren(): self public function getChildren(): self
{ {
/** @noinspection PhpPossiblePolymorphicInvocationInspection */
return new self($this->getInnerIterator()->getChildren(), $this->ignoreFiles); return new self($this->getInnerIterator()->getChildren(), $this->ignoreFiles);
} }
} }

View File

@@ -99,6 +99,7 @@ class ZipFile implements \Countable, \ArrayAccess, \Iterator
throw new ZipException("File {$filename} does not exist."); throw new ZipException("File {$filename} does not exist.");
} }
/** @psalm-suppress InvalidArgument */
set_error_handler( set_error_handler(
static function (int $errorNumber, string $errorString): ?bool { static function (int $errorNumber, string $errorString): ?bool {
throw new InvalidArgumentException($errorString, $errorNumber); throw new InvalidArgumentException($errorString, $errorNumber);
@@ -417,6 +418,7 @@ class ZipFile implements \Countable, \ArrayAccess, \Iterator
continue; continue;
} }
/** @psalm-suppress InvalidArgument */
set_error_handler( set_error_handler(
static function (int $errorNumber, string $errorString) use ($entry, $file): ?bool { static function (int $errorNumber, string $errorString) use ($entry, $file): ?bool {
throw new ZipException( throw new ZipException(
@@ -1454,6 +1456,7 @@ class ZipFile implements \Countable, \ArrayAccess, \Iterator
{ {
$tempFilename = $filename . '.temp' . uniqid('', false); $tempFilename = $filename . '.temp' . uniqid('', false);
/** @psalm-suppress InvalidArgument */
set_error_handler( set_error_handler(
static function (int $errorNumber, string $errorString): ?bool { static function (int $errorNumber, string $errorString): ?bool {
throw new InvalidArgumentException($errorString, $errorNumber); throw new InvalidArgumentException($errorString, $errorNumber);
@@ -1479,7 +1482,7 @@ class ZipFile implements \Countable, \ArrayAccess, \Iterator
} }
} }
if (!@rename($tempFilename, $filename)) { if (!rename($tempFilename, $filename)) {
if (is_file($tempFilename)) { if (is_file($tempFilename)) {
unlink($tempFilename); unlink($tempFilename);
} }