Sfoglia il codice sorgente

added static analysis

Ne-Lexa 4 anni fa
parent
commit
00dd097b2d

+ 8 - 4
.github/workflows/build.yml

@@ -54,7 +54,7 @@ jobs:
                     extensions:  ${{ env.extensions }}
                     coverage:    pcov
                     ini-values:  ${{ env.PHP_INI }}
-                    tools:       composer:v2
+                    tools:       composer:v2, cs2pr
 
             -
                 name: Determine composer cache directory on Linux or MacOS
@@ -109,9 +109,13 @@ jobs:
                 if:   env.PHPUNIT_COVERAGE == 1
                 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
                 if:   env.PHPUNIT_COVERAGE == 1
-                run: |
-                     wget https://scrutinizer-ci.com/ocular.phar
-                     php ocular.phar code-coverage:upload --format=php-clover coverage.clover
+                run:  |
+                      wget https://scrutinizer-ci.com/ocular.phar
+                      php ocular.phar code-coverage:upload --format=php-clover coverage.clover

+ 8 - 6
src/IO/Stream/ResponseStream.php

@@ -59,7 +59,7 @@ class ResponseStream implements StreamInterface
         ],
     ];
 
-    /** @var resource */
+    /** @var resource|null */
     private $stream;
 
     private ?int $size = null;
@@ -97,7 +97,7 @@ class ResponseStream implements StreamInterface
      */
     public function getMetadata($key = null)
     {
-        if (!$this->stream) {
+        if ($this->stream === null) {
             return $key ? null : [];
         }
         $meta = stream_get_meta_data($this->stream);
@@ -141,7 +141,7 @@ class ResponseStream implements StreamInterface
      */
     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
     {
-        $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;
 
-        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
     {
-        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.
+     *
+     * @psalm-suppress InvalidPropertyAssignmentValue
      */
     public function close(): void
     {

+ 5 - 5
src/IO/ZipReader.php

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

+ 3 - 6
src/Model/Data/ZipFileData.php

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

+ 4 - 4
src/Model/Extra/Fields/AbstractUnicodeExtraField.php

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

+ 5 - 11
src/Model/Extra/Fields/ApkAlignmentExtraField.php

@@ -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://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
@@ -31,12 +31,6 @@ class ApkAlignmentExtraField implements ZipExtraField
      */
     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 */
     public const ALIGNMENT_BYTES = 4;
 
@@ -86,8 +80,8 @@ class ApkAlignmentExtraField implements ZipExtraField
     /**
      * Populate data from this array as if it was in local file data.
      *
-     * @param string    $buffer the buffer to read data from
-     * @param ?ZipEntry $entry
+     * @param string        $buffer the buffer to read data from
+     * @param ZipEntry|null $entry  optional zip entry
      *
      * @throws ZipException
      *
@@ -116,8 +110,8 @@ class ApkAlignmentExtraField implements ZipExtraField
     /**
      * Populate data from this array as if it was in central directory data.
      *
-     * @param string    $buffer the buffer to read data from
-     * @param ?ZipEntry $entry
+     * @param string        $buffer the buffer to read data from
+     * @param ZipEntry|null $entry  optional zip entry
      *
      * @throws ZipException on error
      *

+ 7 - 7
src/Model/Extra/Fields/AsiExtraField.php

@@ -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
  */
-class AsiExtraField implements ZipExtraField
+final class AsiExtraField implements ZipExtraField
 {
     /** @var int Header id */
     public const HEADER_ID = 0x756e;
@@ -99,12 +99,12 @@ class AsiExtraField implements ZipExtraField
     /**
      * Populate data from this array as if it was in local file data.
      *
-     * @param string    $buffer the buffer to read data from
-     * @param ?ZipEntry $entry
+     * @param string        $buffer the buffer to read data from
+     * @param ZipEntry|null $entry  optional zip entry
      *
      * @throws Crc32Exception
      *
-     * @return static
+     * @return AsiExtraField
      */
     public static function unpackLocalFileData(string $buffer, ?ZipEntry $entry = null): self
     {
@@ -134,8 +134,8 @@ class AsiExtraField implements ZipExtraField
     /**
      * Populate data from this array as if it was in central directory data.
      *
-     * @param string    $buffer the buffer to read data from
-     * @param ?ZipEntry $entry
+     * @param string        $buffer the buffer to read data from
+     * @param ZipEntry|null $entry  optional zip entry
      *
      * @throws Crc32Exception
      *
@@ -216,7 +216,7 @@ class AsiExtraField implements ZipExtraField
      *
      * @return int the type with the mode
      */
-    protected function getPermissionsMode(int $mode): int
+    private function getPermissionsMode(int $mode): int
     {
         $type = 0;
 

+ 5 - 5
src/Model/Extra/Fields/ExtendedTimestampExtraField.php

@@ -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
  */
-class ExtendedTimestampExtraField implements ZipExtraField
+final class ExtendedTimestampExtraField implements ZipExtraField
 {
     /** @var int Header id */
     public const HEADER_ID = 0x5455;
@@ -158,8 +158,8 @@ class ExtendedTimestampExtraField implements ZipExtraField
     /**
      * Populate data from this array as if it was in local file data.
      *
-     * @param string    $buffer the buffer to read data from
-     * @param ?ZipEntry $entry
+     * @param string        $buffer the buffer to read data from
+     * @param ZipEntry|null $entry  optional zip entry
      *
      * @return ExtendedTimestampExtraField
      */
@@ -195,8 +195,8 @@ class ExtendedTimestampExtraField implements ZipExtraField
     /**
      * Populate data from this array as if it was in central directory data.
      *
-     * @param string    $buffer the buffer to read data from
-     * @param ?ZipEntry $entry
+     * @param string        $buffer the buffer to read data from
+     * @param ZipEntry|null $entry  optional zip entry
      *
      * @return ExtendedTimestampExtraField
      */

+ 5 - 5
src/Model/Extra/Fields/JarMarkerExtraField.php

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

+ 5 - 5
src/Model/Extra/Fields/NewUnixExtraField.php

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

+ 6 - 6
src/Model/Extra/Fields/NtfsExtraField.php

@@ -19,9 +19,9 @@ use PhpZip\Model\ZipEntry;
 /**
  * NTFS Extra Field.
  *
- * @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 */
     public const HEADER_ID = 0x000a;
@@ -84,8 +84,8 @@ class NtfsExtraField implements ZipExtraField
     /**
      * Populate data from this array as if it was in local file data.
      *
-     * @param string    $buffer the buffer to read data from
-     * @param ?ZipEntry $entry
+     * @param string        $buffer the buffer to read data from
+     * @param ZipEntry|null $entry  optional zip entry
      *
      * @throws ZipException
      *
@@ -127,8 +127,8 @@ class NtfsExtraField implements ZipExtraField
     /**
      * Populate data from this array as if it was in central directory data.
      *
-     * @param string    $buffer the buffer to read data from
-     * @param ?ZipEntry $entry
+     * @param string        $buffer the buffer to read data from
+     * @param ZipEntry|null $entry  optional zip entry
      *
      * @throws ZipException
      *

+ 5 - 5
src/Model/Extra/Fields/OldUnixExtraField.php

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

+ 1 - 1
src/Model/Extra/Fields/UnicodeCommentExtraField.php

@@ -55,7 +55,7 @@ namespace PhpZip\Model\Extra\Fields;
  *
  * @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;
 

+ 1 - 1
src/Model/Extra/Fields/UnicodePathExtraField.php

@@ -56,7 +56,7 @@ namespace PhpZip\Model\Extra\Fields;
  *
  * @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;
 

+ 1 - 1
src/Model/Extra/Fields/UnrecognizedExtraField.php

@@ -18,7 +18,7 @@ use PhpZip\Model\ZipEntry;
 /**
  * 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;
 

+ 1 - 1
src/Model/Extra/Fields/WinZipAesExtraField.php

@@ -24,7 +24,7 @@ use PhpZip\Model\ZipEntry;
  *
  * @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 */
     public const HEADER_ID = 0x9901;

+ 1 - 1
src/Model/Extra/Fields/Zip64ExtraField.php

@@ -22,7 +22,7 @@ use PhpZip\Model\ZipEntry;
  *
  * @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. */
     public const HEADER_ID = 0x0001;

+ 4 - 4
src/Model/Extra/ZipExtraField.php

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

+ 3 - 6
src/Model/ZipEntry.php

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

+ 2 - 1
src/Util/Iterator/IgnoreFilesRecursiveFilterIterator.php

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

+ 4 - 1
src/ZipFile.php

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