diff --git a/psalm-baseline.xml b/psalm-baseline.xml index e4d003b6..fb5fde8f 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -1,5 +1,5 @@ - + filterable @@ -129,36 +129,21 @@ - ! is_array($options) + is_string($adapter) mixed - - $adapter - $adapterOptions - - - $adapter - $adapter - $key - adapter]]> - + + $options + - $adapter - $key $value - - array - new $adapter($options) - - adapterOptions]]> - $options @@ -436,17 +421,6 @@ - - ! is_string($value) - - - $value - - - $value - $value - $value - $value @@ -469,11 +443,13 @@ adapter->decrypt($value)]]> - $value $value + + return $value; + @@ -486,6 +462,7 @@ + Encrypt Encrypt\EncryptionAlgorithmInterface @@ -524,16 +501,21 @@ adapter->encrypt($value)]]> adapter->toString()]]> - $value $value + + return $value; + __call getAdapter getAdapterInstance + + + @@ -560,6 +542,10 @@ encryption]]> $value + + compression]]> + compression]]> + @@ -577,6 +563,7 @@ encryption['vector']]]> + $decrypted $options $value @@ -584,11 +571,14 @@ array string string + string encryption]]> + $decrypted + $decrypted encryption]]> encryption['key']]]> encryption['vector']]]> @@ -621,6 +611,10 @@ + + compression]]> + compression]]> + Openssl @@ -652,6 +646,7 @@ $count + $decrypted $envKey $i $i @@ -663,6 +658,9 @@ $option $option + + string + $i @@ -670,6 +668,10 @@ keys]]> + + $decrypted + $decrypted + __construct getCompression @@ -693,6 +695,9 @@ Filter\Decrypt parent::filter($content) + + Decrypt + @@ -722,11 +727,13 @@ filename]]> filename]]> $uploadData - $value $value + + return $value; + $filename @@ -749,6 +756,9 @@ Filter\Encrypt parent::filter($content) + + Encrypt + @@ -777,11 +787,13 @@ filename]]> filename]]> $uploadData - $value $value + + return $value; + $filename @@ -809,7 +821,6 @@ - is_array($options) is_array($options) @@ -834,7 +845,6 @@ $value - $value $options @@ -855,18 +865,10 @@ string - string|array - - $uploadData - $uploadData - $value - - $value - @@ -987,31 +989,9 @@ - - - - - $value - $value - $value - $value - $value - - string|array - - - $uploadData - $value - $value - $value - - - $value - $value - $uploadData @@ -1312,37 +1292,23 @@ - - is_scalar($value) - - tagsAllowed[$tagName]]]> - - tagsAllowed[$tagName][$attributeName]]]> - - $attribute $attribute $element - - $value - $temp - - (string) $value - (string) $value - is_array($options) + is_string($attribute) @@ -1404,9 +1370,6 @@ is_string($separator) - - $separator - $separator @@ -1613,9 +1576,6 @@ getArchive getArchive - - $adapter - @@ -1639,16 +1599,9 @@ - - $compressed - $input - $compressed - - null - returnUnfilteredDataProvider @@ -1885,7 +1838,6 @@ $filtered - $input $filtered diff --git a/src/Compress.php b/src/Compress.php index 783b0245..501870e5 100644 --- a/src/Compress.php +++ b/src/Compress.php @@ -20,46 +20,55 @@ /** * Compresses a given string * + * @psalm-type AdapterType = 'Bz2'|'Gz'|'Lzf'|'Rar'|'Snappy'|'Tar'|'Zip' + * @psalm-type AdapterTypeOrInstance = Compress\CompressionAlgorithmInterface|AdapterType * @psalm-type Options = array{ - * adapter?: Compress\CompressionAlgorithmInterface|'Bz2'|'Gz'|'Lzf'|'Rar'|'Snappy'|'Tar'|'Zip', - * adapter_options?: array, - * ... - * } + * adapter?: AdapterTypeOrInstance, + * options?: array, + * }&array * @extends AbstractFilter */ class Compress extends AbstractFilter { /** * Compression adapter + * + * @var AdapterTypeOrInstance */ protected $adapter = 'Gz'; /** * Compression adapter constructor options + * + * @var array */ protected $adapterOptions = []; /** - * @param string|array|Traversable $options (Optional) Options to set + * @param Options|Traversable|null|AdapterTypeOrInstance $options */ public function __construct($options = null) { if ($options instanceof Traversable) { + /** @psalm-var Options $options */ $options = ArrayUtils::iteratorToArray($options); } - if (is_string($options)) { - $this->setAdapter($options); - } elseif ($options instanceof Compress\CompressionAlgorithmInterface) { + + if (is_string($options) || $options instanceof Compress\CompressionAlgorithmInterface) { $this->setAdapter($options); - } elseif (is_array($options)) { + + return; + } + + if (is_array($options)) { $this->setOptions($options); } } /** - * Set filter setate + * Set filter state * - * @param array $options + * @param Options|Traversable $options * @throws Exception\InvalidArgumentException If options is not an array or Traversable. * @return self */ @@ -111,13 +120,17 @@ public function getAdapter() } } - $this->adapter = new $adapter($options); - if (! $this->adapter instanceof Compress\CompressionAlgorithmInterface) { - throw new Exception\InvalidArgumentException( - "Compression adapter '" . $adapter - . "' does not implement Laminas\\Filter\\Compress\\CompressionAlgorithmInterface" - ); + $instance = new $adapter($options); + if (! $instance instanceof Compress\CompressionAlgorithmInterface) { + throw new Exception\InvalidArgumentException(sprintf( + "Compression adapter '%s' does not implement %s", + $adapter, + CompressionAlgorithmInterface::class, + )); } + + $this->adapter = $instance; + return $this->adapter; } @@ -134,7 +147,7 @@ public function getAdapterName() /** * Sets compression adapter * - * @param string|CompressionAlgorithmInterface $adapter Adapter to use + * @param AdapterTypeOrInstance $adapter Adapter to use * @return self * @throws Exception\InvalidArgumentException */ @@ -158,7 +171,7 @@ public function setAdapter($adapter) /** * Retrieve adapter options * - * @return array + * @return array */ public function getAdapterOptions() { @@ -168,6 +181,7 @@ public function getAdapterOptions() /** * Set adapter options * + * @param array $options * @return self */ public function setAdapterOptions(array $options) diff --git a/src/Decompress.php b/src/Decompress.php index 51896e85..ab3f5511 100644 --- a/src/Decompress.php +++ b/src/Decompress.php @@ -18,8 +18,8 @@ class Decompress extends Compress * * Decompresses the content $value with the defined settings * - * @param string $value Content to decompress - * @return string The decompressed content + * @param mixed $value Content to decompress + * @return mixed|string The decompressed content */ public function __invoke($value) { @@ -31,8 +31,8 @@ public function __invoke($value) * * Decompresses the content $value with the defined settings * - * @param string $value Content to decompress - * @return string The decompressed content + * @param mixed $value Content to decompress + * @return mixed|string The decompressed content */ public function filter($value) { diff --git a/src/File/Rename.php b/src/File/Rename.php index 66810dc6..5aaea12f 100644 --- a/src/File/Rename.php +++ b/src/File/Rename.php @@ -177,9 +177,9 @@ public function getNewName($value, $source = false) * Renames the file $value to the new name set before * Returns the file $value, removing all but digit characters * - * @param string|array $value Full path of file to change or $_FILES data array + * @param mixed $value Full path of file to change or $_FILES data array * @throws Exception\RuntimeException - * @return string|array The new filename which has been set + * @return mixed|string|array The new filename which has been set */ public function filter($value) { @@ -199,7 +199,7 @@ public function filter($value) $value = $value['tmp_name']; } - $file = $this->getNewName($value, true); + $file = $this->getNewName((string) $value, true); if (is_string($file)) { if ($isFileUpload) { return $uploadData; diff --git a/src/File/UpperCase.php b/src/File/UpperCase.php index cddd91da..76372026 100644 --- a/src/File/UpperCase.php +++ b/src/File/UpperCase.php @@ -12,6 +12,7 @@ use function file_put_contents; use function is_array; use function is_scalar; +use function is_string; use function is_writable; /** @final */ @@ -22,8 +23,8 @@ class UpperCase extends StringToUpper * * Does a lowercase on the content of the given file * - * @param string|array $value Full path of file to change or $_FILES data array - * @return string|array The given $value + * @param mixed $value Full path of file to change or $_FILES data array + * @return mixed|string|array The given $value * @throws Exception\RuntimeException * @throws Exception\InvalidArgumentException */ @@ -45,7 +46,7 @@ public function filter($value) $value = $value['tmp_name']; } - if (! file_exists($value)) { + if (! is_string($value) || ! file_exists($value)) { throw new Exception\InvalidArgumentException("File '$value' not found"); } diff --git a/src/PregReplace.php b/src/PregReplace.php index c7c300f1..bb793778 100644 --- a/src/PregReplace.php +++ b/src/PregReplace.php @@ -9,9 +9,7 @@ use function func_get_args; use function get_debug_type; -use function gettype; use function is_array; -use function is_object; use function is_string; use function iterator_to_array; use function preg_match; @@ -77,7 +75,7 @@ public function setPattern($pattern) throw new Exception\InvalidArgumentException(sprintf( '%s expects pattern to be array or string; received "%s"', __METHOD__, - is_object($pattern) ? $pattern::class : gettype($pattern) + get_debug_type($pattern), )); } diff --git a/src/StringPrefix.php b/src/StringPrefix.php index af3d870e..0eaf8b91 100644 --- a/src/StringPrefix.php +++ b/src/StringPrefix.php @@ -4,8 +4,7 @@ namespace Laminas\Filter; -use function gettype; -use function is_object; +use function get_debug_type; use function is_scalar; use function is_string; use function sprintf; @@ -47,7 +46,7 @@ public function setPrefix($prefix) throw new Exception\InvalidArgumentException(sprintf( '%s expects "prefix" to be string; received "%s"', __METHOD__, - is_object($prefix) ? $prefix::class : gettype($prefix) + get_debug_type($prefix), )); } diff --git a/src/StringSuffix.php b/src/StringSuffix.php index 1b5d7f50..d9ffaad5 100644 --- a/src/StringSuffix.php +++ b/src/StringSuffix.php @@ -4,8 +4,7 @@ namespace Laminas\Filter; -use function gettype; -use function is_object; +use function get_debug_type; use function is_scalar; use function is_string; use function sprintf; @@ -47,7 +46,7 @@ public function setSuffix($suffix) throw new Exception\InvalidArgumentException(sprintf( '%s expects "suffix" to be string; received "%s"', __METHOD__, - is_object($suffix) ? $suffix::class : gettype($suffix) + get_debug_type($suffix), )); } diff --git a/src/StripTags.php b/src/StripTags.php index 0436f8e8..7222014b 100644 --- a/src/StripTags.php +++ b/src/StripTags.php @@ -46,7 +46,7 @@ class StripTags extends AbstractFilter * Tags are stored in the array keys, and the array values are themselves * arrays of the attributes allowed for the corresponding tag. * - * @var array + * @var array> */ protected $tagsAllowed = []; @@ -55,7 +55,7 @@ class StripTags extends AbstractFilter * * Attributes stored here are allowed for all of the allowed tags. * - * @var array + * @var array */ protected $attributesAllowed = []; @@ -102,7 +102,7 @@ public function __construct($options = null) /** * Returns the tagsAllowed option * - * @return array + * @return array> */ public function getTagsAllowed() { @@ -154,7 +154,7 @@ public function setTagsAllowed($tagsAllowed) /** * Returns the attributesAllowed option * - * @return array + * @return array */ public function getAttributesAllowed() { @@ -164,7 +164,7 @@ public function getAttributesAllowed() /** * Sets the attributesAllowed option * - * @param array|string $attributesAllowed + * @param list|string $attributesAllowed * @return self Provides a fluent interface */ public function setAttributesAllowed($attributesAllowed) @@ -190,8 +190,7 @@ public function setAttributesAllowed($attributesAllowed) * * If the value provided is non-scalar, the value will remain unfiltered * - * @todo improve docblock descriptions - * @param string $value + * @param mixed $value * @return string|mixed */ public function filter($value) @@ -220,7 +219,7 @@ public function filter($value) $dataFiltered = ''; // Parse the input data iteratively as regular pre-tag text followed by a // tag; either may be empty strings - preg_match_all('/([^<]*)(]*>?)/', (string) $value, $matches); + preg_match_all('/([^<]*)(]*>?)/', $value, $matches); // Iterate over each set of matches foreach ($matches[1] as $index => $preTag) { @@ -249,8 +248,7 @@ public function filter($value) * @param string $tag * @return string */ - // @codingStandardsIgnoreStart - protected function _filterTag($tag) + protected function _filterTag($tag) // phpcs:ignore { // @codingStandardsIgnoreEnd // Parse the tag into: diff --git a/test/CompressTest.php b/test/CompressTest.php index e7a07020..b8dab312 100644 --- a/test/CompressTest.php +++ b/test/CompressTest.php @@ -5,6 +5,7 @@ namespace LaminasTest\Filter; use Laminas\Filter\Boolean; +use Laminas\Filter\Compress; use Laminas\Filter\Compress as CompressFilter; use Laminas\Filter\Compress\CompressionAlgorithmInterface; use Laminas\Filter\Exception; @@ -22,6 +23,7 @@ use function uniqid; use function unlink; +/** @psalm-import-type AdapterType from Compress */ class CompressTest extends TestCase { private string $tmpDir; @@ -48,19 +50,21 @@ public function tearDown(): void } } - /** @return iterable */ + /** @return iterable */ public static function returnFilterType(): iterable { if (extension_loaded('bz2')) { - yield ['bz2']; + yield ['Bz2']; } if (extension_loaded('zlib')) { - yield ['gz']; + yield ['Gz']; } } /** * Basic usage + * + * @param AdapterType $filterType */ #[DataProvider('returnFilterType')] public function testBasicUsage(string $filterType): void @@ -77,6 +81,8 @@ public function testBasicUsage(string $filterType): void /** * Setting Options + * + * @param AdapterType $filterType */ #[DataProvider('returnFilterType')] public function testGetSetAdapterOptionsInConstructor(string $filterType): void @@ -99,6 +105,8 @@ public function testGetSetAdapterOptionsInConstructor(string $filterType): void /** * Setting Options through constructor + * + * @param AdapterType $filterType */ #[DataProvider('returnFilterType')] public function testGetSetAdapterOptions(string $filterType): void @@ -124,7 +132,7 @@ public function testGetSetBlocksize(): void self::markTestSkipped('Extension bz2 is required for this test'); } - $filter = new CompressFilter('bz2'); + $filter = new CompressFilter('Bz2'); self::assertSame(4, $filter->getBlocksize()); $filter->setBlocksize(6); self::assertSame(6, $filter->getOptions('blocksize')); @@ -136,6 +144,8 @@ public function testGetSetBlocksize(): void /** * Setting Archive + * + * @param AdapterType $filterType */ #[DataProvider('returnFilterType')] public function testGetSetArchive(string $filterType): void @@ -149,6 +159,8 @@ public function testGetSetArchive(string $filterType): void /** * Setting Archive + * + * @param AdapterType $filterType */ #[DataProvider('returnFilterType')] public function testCompressToFile(string $filterType): void @@ -172,6 +184,8 @@ public function testCompressToFile(string $filterType): void /** * testing toString + * + * @param AdapterType $filterType */ #[DataProvider('returnFilterType')] public function testToString(string $filterType): void @@ -182,6 +196,8 @@ public function testToString(string $filterType): void /** * testing getAdapter + * + * @param AdapterType $filterType */ #[DataProvider('returnFilterType')] public function testGetAdapter(string $filterType): void @@ -204,15 +220,18 @@ public function testSetAdapter(): void $filter = new CompressFilter(); self::assertSame('Gz', $filter->getAdapterName()); + /** @psalm-suppress InvalidArgument */ $filter->setAdapter(Boolean::class); $this->expectException(Exception\InvalidArgumentException::class); $this->expectExceptionMessage('does not implement'); - $adapter = $filter->getAdapter(); + $filter->getAdapter(); } /** - * Decompress archiv + * Decompress archive + * + * @param AdapterType $filterType */ #[DataProvider('returnFilterType')] public function testDecompressArchive(string $filterType): void @@ -257,6 +276,9 @@ public static function returnUnfilteredDataProvider(): iterable } } + /** + * @param AdapterType $filterType + */ #[DataProvider('returnUnfilteredDataProvider')] public function testReturnUnfiltered(string $filterType, mixed $input): void { diff --git a/test/DateTimeFormatterTest.php b/test/DateTimeFormatterTest.php index b77a2812..6c072aa7 100644 --- a/test/DateTimeFormatterTest.php +++ b/test/DateTimeFormatterTest.php @@ -17,6 +17,7 @@ class DateTimeFormatterTest extends TestCase { + /** @var non-empty-string */ private string $defaultTimezone; public function setUp(): void diff --git a/test/DecompressTest.php b/test/DecompressTest.php index b68c58c8..d03195b5 100644 --- a/test/DecompressTest.php +++ b/test/DecompressTest.php @@ -4,6 +4,7 @@ namespace LaminasTest\Filter; +use Laminas\Filter\Compress; use Laminas\Filter\Decompress as DecompressFilter; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; @@ -19,6 +20,7 @@ use function uniqid; use function unlink; +/** @psalm-import-type AdapterType from Compress */ class DecompressTest extends TestCase { private string $tmpDir; @@ -45,19 +47,21 @@ public function tearDown(): void } } - /** @return iterable */ + /** @return iterable */ public static function returnFilterType(): iterable { if (extension_loaded('bz2')) { - yield ['bz2']; + yield ['Bz2']; } if (extension_loaded('zlib')) { - yield ['gz']; + yield ['Gz']; } } /** * Basic usage + * + * @param AdapterType $filterType */ #[DataProvider('returnFilterType')] public function testBasicUsage(string $filterType): void @@ -74,6 +78,8 @@ public function testBasicUsage(string $filterType): void /** * Setting Archive + * + * @param AdapterType $filterType */ #[DataProvider('returnFilterType')] public function testCompressToFile(string $filterType): void @@ -97,6 +103,8 @@ public function testCompressToFile(string $filterType): void /** * Basic usage + * + * @param AdapterType $filterType */ #[DataProvider('returnFilterType')] public function testDecompressArchive(string $filterType): void @@ -113,6 +121,9 @@ public function testDecompressArchive(string $filterType): void self::assertSame('compress me', $content2); } + /** + * @param AdapterType $filterType + */ #[DataProvider('returnFilterType')] public function testFilterMethodProxiesToDecompress(string $filterType): void { @@ -143,6 +154,9 @@ public static function returnUnfilteredDataProvider(): iterable } } + /** + * @param AdapterType $filterType + */ #[DataProvider('returnUnfilteredDataProvider')] public function testReturnUnfiltered(string $filterType, mixed $input): void { diff --git a/test/StripTagsTest.php b/test/StripTagsTest.php index 8b727474..08d7532c 100644 --- a/test/StripTagsTest.php +++ b/test/StripTagsTest.php @@ -85,6 +85,7 @@ public function testSetAttributesAllowedArray(): void 'ok' => 'String', null, ]; + /** @psalm-suppress ArgumentTypeCoercion */ $this->filter->setAttributesAllowed($attributesAllowed); $attributesAllowedExpected = [ 'class' => null,