Skip to content

Commit

Permalink
Merge pull request #76 from gsteel/minor-sa-improvements
Browse files Browse the repository at this point in the history
Minor SA improvements to compression related filters
  • Loading branch information
Ocramius committed Nov 6, 2022
2 parents 73fb499 + c9ad71b commit 49ead1d
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 79 deletions.
76 changes: 22 additions & 54 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@
<DocblockTypeContradiction occurrences="1">
<code>gettype($options)</code>
</DocblockTypeContradiction>
<InvalidThrow occurrences="1">
<code>Exception\ExceptionInterface</code>
</InvalidThrow>
<MixedArgument occurrences="4">
<code>$key</code>
<code>$key</code>
Expand Down Expand Up @@ -175,6 +172,9 @@
</UndefinedInterfaceMethod>
</file>
<file src="src/Compress/AbstractCompressionAlgorithm.php">
<InvalidPropertyAssignmentValue occurrences="1">
<code>[]</code>
</InvalidPropertyAssignmentValue>
<MixedAssignment occurrences="1">
<code>$option</code>
</MixedAssignment>
Expand All @@ -186,14 +186,6 @@
<InvalidReturnType occurrences="1">
<code>string</code>
</InvalidReturnType>
<MixedInferredReturnType occurrences="2">
<code>int</code>
<code>string</code>
</MixedInferredReturnType>
<MixedReturnStatement occurrences="2">
<code>$this-&gt;options['archive']</code>
<code>$this-&gt;options['blocksize']</code>
</MixedReturnStatement>
<ParamNameMismatch occurrences="2">
<code>$content</code>
<code>$content</code>
Expand All @@ -202,8 +194,7 @@
<code>(int) $blocksize</code>
<code>(string) $archive</code>
</RedundantCastGivenDocblockType>
<RedundantConditionGivenDocblockType occurrences="3">
<code>null !== $archive</code>
<RedundantConditionGivenDocblockType occurrences="2">
<code>null !== $content</code>
<code>null !== $content</code>
</RedundantConditionGivenDocblockType>
Expand All @@ -226,19 +217,6 @@
<MixedAssignment occurrences="1">
<code>$size</code>
</MixedAssignment>
<MixedInferredReturnType occurrences="3">
<code>int</code>
<code>string</code>
<code>string</code>
</MixedInferredReturnType>
<MixedOperand occurrences="1">
<code>$this-&gt;options['archive']</code>
</MixedOperand>
<MixedReturnStatement occurrences="3">
<code>$this-&gt;options['archive']</code>
<code>$this-&gt;options['level']</code>
<code>$this-&gt;options['mode']</code>
</MixedReturnStatement>
<ParamNameMismatch occurrences="2">
<code>$content</code>
<code>$content</code>
Expand All @@ -247,9 +225,8 @@
<code>(int) $level</code>
<code>(string) $archive</code>
</RedundantCastGivenDocblockType>
<RedundantConditionGivenDocblockType occurrences="5">
<RedundantConditionGivenDocblockType occurrences="4">
<code>$mode === 'deflate' &amp;&amp; null !== $content</code>
<code>null !== $archive</code>
<code>null !== $content</code>
<code>null !== $content</code>
<code>null !== $content</code>
Expand Down Expand Up @@ -336,6 +313,9 @@
<DocblockTypeContradiction occurrences="1">
<code>$result === false</code>
</DocblockTypeContradiction>
<InvalidNullableReturnType occurrences="1">
<code>string</code>
</InvalidNullableReturnType>
<MixedArgument occurrences="1">
<code>$content</code>
</MixedArgument>
Expand All @@ -348,26 +328,22 @@
<code>$file[]</code>
<code>$info</code>
</MixedAssignment>
<MixedInferredReturnType occurrences="3">
<code>string</code>
<code>string</code>
<code>string</code>
</MixedInferredReturnType>
<MixedMethodCall occurrences="1">
<code>isFile</code>
</MixedMethodCall>
<MixedReturnStatement occurrences="3">
<code>$this-&gt;options['archive']</code>
<code>$this-&gt;options['mode']</code>
<code>$this-&gt;options['target']</code>
</MixedReturnStatement>
<NullableReturnStatement occurrences="1">
<code>$this-&gt;getArchive()</code>
</NullableReturnStatement>
<ParamNameMismatch occurrences="2">
<code>$content</code>
<code>$content</code>
</ParamNameMismatch>
<PossiblyInvalidArgument occurrences="1">
<code>$content</code>
</PossiblyInvalidArgument>
<PossiblyNullArgument occurrences="1">
<code>$this-&gt;getArchive()</code>
</PossiblyNullArgument>
<PossiblyUndefinedVariable occurrences="2">
<code>$file</code>
<code>$file</code>
Expand All @@ -385,23 +361,19 @@
<code>$res</code>
<code>$res</code>
</InvalidArgument>
<InvalidNullableReturnType occurrences="1">
<code>string</code>
</InvalidNullableReturnType>
<InvalidScalarArgument occurrences="2">
<code>$res</code>
<code>$res</code>
</InvalidScalarArgument>
<MixedArgument occurrences="1">
<code>$res</code>
</MixedArgument>
<MixedInferredReturnType occurrences="3">
<code>string</code>
<code>string</code>
<code>string</code>
</MixedInferredReturnType>
<MixedReturnStatement occurrences="3">
<code>$this-&gt;options['archive']</code>
<NullableReturnStatement occurrences="1">
<code>$this-&gt;options['archive']</code>
<code>$this-&gt;options['target']</code>
</MixedReturnStatement>
</NullableReturnStatement>
<ParamNameMismatch occurrences="2">
<code>$content</code>
<code>$content</code>
Expand All @@ -410,6 +382,9 @@
<code>strrpos($content, DIRECTORY_SEPARATOR)</code>
<code>strrpos($content, DIRECTORY_SEPARATOR)</code>
</PossiblyFalseOperand>
<PossiblyNullArgument occurrences="1">
<code>$this-&gt;getArchive()</code>
</PossiblyNullArgument>
<RedundantCast occurrences="1">
<code>(string) $target</code>
</RedundantCast>
Expand Down Expand Up @@ -1327,11 +1302,6 @@
<code>$existsOrOptions !== null</code>
</RedundantConditionGivenDocblockType>
</file>
<file src="src/StaticFilter.php">
<InvalidThrow occurrences="1">
<code>Exception\ExceptionInterface</code>
</InvalidThrow>
</file>
<file src="src/StringPrefix.php">
<DocblockTypeContradiction occurrences="1">
<code>is_string($prefix)</code>
Expand Down Expand Up @@ -2194,8 +2164,6 @@
</DeprecatedMethod>
<InvalidArgument occurrences="1">
<code>$e-&gt;getMessage()</code>
<code>$e-&gt;getMessage()</code>
<code>$e-&gt;getMessage()</code>
</InvalidArgument>
</file>
<file src="test/StringToUpperTest.php">
Expand Down
6 changes: 4 additions & 2 deletions src/Compress/AbstractCompressionAlgorithm.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@

/**
* Abstract compression adapter
*
* @template TOptions of array
*/
abstract class AbstractCompressionAlgorithm implements CompressionAlgorithmInterface
{
/** @var array */
/** @var TOptions */
protected $options = [];

/**
* @param null|array|Traversable $options (Optional) Options to set
* @param null|iterable $options (Optional) Options to set
*/
public function __construct($options = null)
{
Expand Down
13 changes: 9 additions & 4 deletions src/Compress/Bz2.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Laminas\Filter\Compress;

use Laminas\Filter\Exception;
use Traversable;

use function bzclose;
use function bzcompress;
Expand All @@ -20,6 +19,12 @@

/**
* Compression adapter for Bz2
*
* @psalm-type Options = array{
* blocksize: int,
* archive: string|null,
* }
* @extends AbstractCompressionAlgorithm<Options>
*/
class Bz2 extends AbstractCompressionAlgorithm
{
Expand All @@ -30,15 +35,15 @@ class Bz2 extends AbstractCompressionAlgorithm
* 'archive' => Archive to use
* )
*
* @var array
* @var Options
*/
protected $options = [
'blocksize' => 4,
'archive' => null,
];

/**
* @param null|array|Traversable $options (Optional) Options to set
* @param null|Options|iterable $options (Optional) Options to set
* @throws Exception\ExtensionNotLoadedException If bz2 extension not loaded.
*/
public function __construct($options = null)
Expand Down Expand Up @@ -79,7 +84,7 @@ public function setBlocksize($blocksize)
/**
* Returns the set archive
*
* @return string
* @return string|null
*/
public function getArchive()
{
Expand Down
19 changes: 13 additions & 6 deletions src/Compress/Gz.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Laminas\Filter\Compress;

use Laminas\Filter\Exception;
use Traversable;

use function end;
use function extension_loaded;
Expand All @@ -22,13 +21,21 @@
use function gzread;
use function gzuncompress;
use function gzwrite;
use function is_string;
use function strpos;
use function unpack;

use const SEEK_END;

/**
* Compression adapter for Gzip (ZLib)
*
* @psalm-type Options = array{
* level: int,
* mode: string,
* archive: string|null,
* }
* @extends AbstractCompressionAlgorithm<Options>
*/
class Gz extends AbstractCompressionAlgorithm
{
Expand All @@ -40,7 +47,7 @@ class Gz extends AbstractCompressionAlgorithm
* 'archive' => Archive to use
* )
*
* @var array
* @var Options
*/
protected $options = [
'level' => 9,
Expand All @@ -49,7 +56,7 @@ class Gz extends AbstractCompressionAlgorithm
];

/**
* @param null|array|Traversable $options (Optional) Options to set
* @param null|Options|iterable $options (Optional) Options to set
* @throws Exception\ExtensionNotLoadedException If zlib extension not loaded.
*/
public function __construct($options = null)
Expand Down Expand Up @@ -117,7 +124,7 @@ public function setMode($mode)
/**
* Returns the set archive
*
* @return string
* @return string|null
*/
public function getArchive()
{
Expand Down Expand Up @@ -146,10 +153,10 @@ public function setArchive($archive)
public function compress($content)
{
$archive = $this->getArchive();
if (! empty($archive)) {
if (is_string($archive) && $archive !== '') {
$file = gzopen($archive, 'w' . $this->getLevel());
if (! $file) {
throw new Exception\RuntimeException("Error opening the archive '" . $this->options['archive'] . "'");
throw new Exception\RuntimeException("Error opening the archive '" . $archive . "'");
}

gzwrite($file, $content);
Expand Down
19 changes: 13 additions & 6 deletions src/Compress/Tar.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@

/**
* Compression adapter for Tar
*
* @psalm-type Options = array{
* archive?: string|null,
* target: string,
* mode?: 'gz'|'bz2'|null,
* }
* @extends AbstractCompressionAlgorithm<Options>
*/
class Tar extends AbstractCompressionAlgorithm
{
Expand All @@ -33,7 +40,7 @@ class Tar extends AbstractCompressionAlgorithm
* 'target' => Target to write the files to
* )
*
* @var array
* @var Options
*/
protected $options = [
'archive' => null,
Expand All @@ -42,7 +49,7 @@ class Tar extends AbstractCompressionAlgorithm
];

/**
* @param array $options (Optional) Options to set
* @param Options $options (Optional) Options to set
* @throws Exception\ExtensionNotLoadedException If Archive_Tar component not available.
*/
public function __construct($options = null)
Expand All @@ -60,11 +67,11 @@ public function __construct($options = null)
/**
* Returns the set archive
*
* @return string
* @return string|null
*/
public function getArchive()
{
return $this->options['archive'];
return $this->options['archive'] ?? null;
}

/**
Expand Down Expand Up @@ -112,11 +119,11 @@ public function setTarget($target)
/**
* Returns the set compression mode
*
* @return string
* @return string|null
*/
public function getMode()
{
return $this->options['mode'];
return $this->options['mode'] ?? null;
}

/**
Expand Down
Loading

0 comments on commit 49ead1d

Please sign in to comment.