diff --git a/composer.json b/composer.json index 9f7a3aef..6aa47699 100644 --- a/composer.json +++ b/composer.json @@ -39,6 +39,7 @@ }, "require-dev": { "laminas/laminas-coding-standard": "~2.5.0", + "laminas/laminas-i18n": "^2.23", "laminas/laminas-uri": "^2.11.0", "pear/archive_tar": "^1.4.14", "phpunit/phpunit": "^10.4.2", diff --git a/composer.lock b/composer.lock index 86f24f3b..999a982c 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "beb092d5157d4cf71a38e327c6e516de", + "content-hash": "4725652ff0b79f76f2859d8244db2db3", "packages": [ { "name": "laminas/laminas-servicemanager", @@ -1054,6 +1054,91 @@ ], "time": "2023-10-10T08:35:13+00:00" }, + { + "name": "laminas/laminas-i18n", + "version": "2.24.0", + "source": { + "type": "git", + "url": "https://github.com/laminas/laminas-i18n.git", + "reference": "4f23433e415b14c4d66a7d7d1983c5890ac9ed2a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/laminas/laminas-i18n/zipball/4f23433e415b14c4d66a7d7d1983c5890ac9ed2a", + "reference": "4f23433e415b14c4d66a7d7d1983c5890ac9ed2a", + "shasum": "" + }, + "require": { + "ext-intl": "*", + "laminas/laminas-servicemanager": "^3.21.0", + "laminas/laminas-stdlib": "^3.0", + "php": "~8.1.0 || ~8.2.0 || ~8.3.0" + }, + "conflict": { + "laminas/laminas-view": "<2.20.0", + "zendframework/zend-i18n": "*" + }, + "require-dev": { + "laminas/laminas-cache": "^3.11.0", + "laminas/laminas-cache-storage-adapter-memory": "^2.3.0", + "laminas/laminas-cache-storage-deprecated-factory": "^1.1", + "laminas/laminas-coding-standard": "~2.5.0", + "laminas/laminas-config": "^3.9.0", + "laminas/laminas-eventmanager": "^3.12", + "laminas/laminas-filter": "^2.33", + "laminas/laminas-validator": "^2.41", + "laminas/laminas-view": "^2.32", + "phpunit/phpunit": "^10.4.2", + "psalm/plugin-phpunit": "^0.18.4", + "vimeo/psalm": "^5.15.0" + }, + "suggest": { + "laminas/laminas-cache": "You should install this package to cache the translations", + "laminas/laminas-config": "You should install this package to use the INI translation format", + "laminas/laminas-eventmanager": "You should install this package to use the events in the translator", + "laminas/laminas-filter": "You should install this package to use the provided filters", + "laminas/laminas-i18n-resources": "This package provides validator and captcha translations", + "laminas/laminas-validator": "You should install this package to use the provided validators", + "laminas/laminas-view": "You should install this package to use the provided view helpers" + }, + "type": "library", + "extra": { + "laminas": { + "component": "Laminas\\I18n", + "config-provider": "Laminas\\I18n\\ConfigProvider" + } + }, + "autoload": { + "psr-4": { + "Laminas\\I18n\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "description": "Provide translations for your application, and filter and validate internationalized values", + "homepage": "https://laminas.dev", + "keywords": [ + "i18n", + "laminas" + ], + "support": { + "chat": "https://laminas.dev/chat", + "docs": "https://docs.laminas.dev/laminas-i18n/", + "forum": "https://discourse.laminas.dev", + "issues": "https://github.com/laminas/laminas-i18n/issues", + "rss": "https://github.com/laminas/laminas-i18n/releases.atom", + "source": "https://github.com/laminas/laminas-i18n" + }, + "funding": [ + { + "url": "https://funding.communitybridge.org/projects/laminas-project", + "type": "community_bridge" + } + ], + "time": "2023-11-06T09:22:50+00:00" + }, { "name": "laminas/laminas-uri", "version": "2.11.0", @@ -1114,16 +1199,16 @@ }, { "name": "laminas/laminas-validator", - "version": "2.41.0", + "version": "2.42.0", "source": { "type": "git", "url": "https://github.com/laminas/laminas-validator.git", - "reference": "ca27df621e12cbd4c138dab7abf3e7e5692c582c" + "reference": "a5221732b2ff6df59908bbf2eb274ed3688665bc" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/laminas/laminas-validator/zipball/ca27df621e12cbd4c138dab7abf3e7e5692c582c", - "reference": "ca27df621e12cbd4c138dab7abf3e7e5692c582c", + "url": "https://api.github.com/repos/laminas/laminas-validator/zipball/a5221732b2ff6df59908bbf2eb274ed3688665bc", + "reference": "a5221732b2ff6df59908bbf2eb274ed3688665bc", "shasum": "" }, "require": { @@ -1194,7 +1279,7 @@ "type": "community_bridge" } ], - "time": "2023-10-30T08:22:19+00:00" + "time": "2023-11-06T09:13:00+00:00" }, { "name": "myclabs/deep-copy", diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 14a3bdea..058c2ef4 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -1,9 +1,6 @@ - - filterable - $value @@ -11,15 +8,6 @@ setNullOnAllEmpty setNullOnEmpty - - $expectedInputs - - - - - - - @@ -38,15 +26,6 @@ is_object($options) - - - assert(is_string($encoding)) - is_string($encoding) - - - is_string($encoding) - - setList @@ -367,18 +346,20 @@ $value + + $value + - - ! is_string($value) - + + string + - $value + filter($value)]]> $value $value - $value $value @@ -403,7 +384,6 @@ - is_array($options) is_array($options) @@ -449,18 +429,10 @@ string - string|array - - $uploadData - $uploadData - $value - - $value - @@ -644,30 +616,6 @@ $shareByDefault - - Alnum - Alnum - Alnum - Alnum - Alnum - Alpha - Alpha - Alpha - Alpha - Alpha - NumberFormat - NumberFormat - NumberFormat - NumberFormat - NumberFormat - NumberFormat - NumberParse - NumberParse - NumberParse - NumberParse - NumberParse - NumberParse - @@ -733,7 +681,7 @@ $spec[0] - + @@ -744,8 +692,8 @@ $processedPart $rule $rules[$spec] - $source[$ruleName] $spec + $value[$ruleName] $sourceName @@ -761,11 +709,11 @@ $ruleFilter $ruleValue $sourceValue - + $ruleFilter($processedPart) @@ -782,14 +730,11 @@ rules[$spec][$index]]]> - + - $source + $value - - $source - self @@ -827,9 +772,6 @@ - - $pattern - @@ -842,11 +784,8 @@ $args[1] - options['pattern']]]> - options['pattern']]]> options['pattern']]]> options['replacement']]]> - options['replacement']]]> bool @@ -904,9 +843,6 @@ - - is_scalar($value) - @@ -923,16 +859,9 @@ - - $value - $temp - - (string) $value - (string) $value - is_array($options) @@ -945,9 +874,6 @@ - - $value - $type @@ -988,12 +914,6 @@ - - is_string($separator) - - - $separator - $separator @@ -1003,10 +923,6 @@ getSeparator - - - - @@ -1433,7 +1349,6 @@ $filtered - $input $filtered @@ -1481,11 +1396,6 @@ true - - $value - $value - $value - $expected $expected diff --git a/src/AbstractDateDropdown.php b/src/AbstractDateDropdown.php index 75093d60..811ef2ca 100644 --- a/src/AbstractDateDropdown.php +++ b/src/AbstractDateDropdown.php @@ -5,6 +5,7 @@ namespace Laminas\Filter; use function array_reduce; +use function assert; use function count; use function is_array; use function is_iterable; @@ -18,34 +19,29 @@ * null_on_all_empty?: bool, * ... * } + * @psalm-type InputArray = array * @template TOptions of Options * @template-extends AbstractFilter + * @template TInput of array */ abstract class AbstractDateDropdown extends AbstractFilter { /** * If true, the filter will return null if any date field is empty - * - * @var bool */ - protected $nullOnEmpty = false; + protected bool $nullOnEmpty = false; /** * If true, the filter will return null if all date fields are empty - * - * @var bool */ - protected $nullOnAllEmpty = false; + protected bool $nullOnAllEmpty = false; /** * Sprintf format string to use for formatting the date, fields will be used in alphabetical order. - * - * @var string */ - protected $format = ''; + protected string $format = ''; - /** @var int */ - protected $expectedInputs; + protected int $expectedInputs = 0; /** * @param mixed $options If array or Traversable, passes value to @@ -59,37 +55,29 @@ public function __construct(mixed $options = null) } /** - * @param bool $nullOnAllEmpty - * @return self + * @return $this */ - public function setNullOnAllEmpty($nullOnAllEmpty) + public function setNullOnAllEmpty(bool $nullOnAllEmpty): self { $this->nullOnAllEmpty = $nullOnAllEmpty; return $this; } - /** - * @return bool - */ - public function isNullOnAllEmpty() + public function isNullOnAllEmpty(): bool { return $this->nullOnAllEmpty; } /** - * @param bool $nullOnEmpty - * @return self + * @return $this */ - public function setNullOnEmpty($nullOnEmpty) + public function setNullOnEmpty(bool $nullOnEmpty): self { $this->nullOnEmpty = $nullOnEmpty; return $this; } - /** - * @return bool - */ - public function isNullOnEmpty() + public function isNullOnEmpty(): bool { return $this->nullOnEmpty; } @@ -98,12 +86,10 @@ public function isNullOnEmpty() * Attempts to filter an array of date/time information to a formatted * string. * - * @param mixed $value input to the filter - * @return mixed|string|null * @throws Exception\RuntimeException If filtering $value is impossible. - * @psalm-return ($value is array ? string|null : mixed) + * @psalm-return ($value is InputArray ? string : mixed|null) */ - public function filter($value) + public function filter(mixed $value): mixed { if (! is_array($value)) { // nothing to do @@ -113,24 +99,23 @@ public function filter($value) // Convert the date to a specific format if ( $this->isNullOnEmpty() - && array_reduce($value, self::class . '::reduce', false) + && array_reduce($value, [self::class, 'reduce'], false) ) { return null; } if ( $this->isNullOnAllEmpty() - && array_reduce($value, self::class . '::reduce', true) + && array_reduce($value, [self::class, 'reduce'], true) ) { return null; } $this->filterable($value); + assert(is_array($value)); ksort($value); - $value = vsprintf($this->format, $value); - - return $value; + return vsprintf($this->format, $value); } /** @@ -139,7 +124,7 @@ public function filter($value) * @param array $value * @throws Exception\RuntimeException */ - protected function filterable($value) + protected function filterable(array $value): void { if (count($value) !== $this->expectedInputs) { throw new Exception\RuntimeException( @@ -154,12 +139,8 @@ protected function filterable($value) /** * Reduce to a single value - * - * @param string $soFar - * @param string $value - * @return bool */ - public static function reduce($soFar, $value) + private static function reduce(string $soFar, string|null $value): bool { return $soFar || empty($value); } diff --git a/src/AbstractFilter.php b/src/AbstractFilter.php index a053ddb9..6accdef4 100644 --- a/src/AbstractFilter.php +++ b/src/AbstractFilter.php @@ -95,9 +95,8 @@ public function getOptions() * Proxies to {@link filter()} * * @throws Exception\ExceptionInterface If filtering $value is impossible. - * @return mixed */ - public function __invoke(mixed $value) + public function __invoke(mixed $value): mixed { return $this->filter($value); } diff --git a/src/AbstractUnicode.php b/src/AbstractUnicode.php index b9dc9744..11995022 100644 --- a/src/AbstractUnicode.php +++ b/src/AbstractUnicode.php @@ -5,9 +5,7 @@ namespace Laminas\Filter; use function array_map; -use function assert; use function in_array; -use function is_string; use function mb_internal_encoding; use function mb_list_encodings; use function sprintf; @@ -25,12 +23,11 @@ abstract class AbstractUnicode extends AbstractFilter /** * Set the input encoding for the given string * - * @param string|null $encoding - * @return self * @throws Exception\InvalidArgumentException * @throws Exception\ExtensionNotLoadedException + * @return $this */ - public function setEncoding($encoding = null) + public function setEncoding(?string $encoding = null): self { if ($encoding !== null) { $encoding = strtolower($encoding); @@ -49,16 +46,12 @@ public function setEncoding($encoding = null) /** * Returns the set encoding - * - * @return string */ - public function getEncoding() + public function getEncoding(): string { $encoding = $this->options['encoding'] ?? null; - assert($encoding === null || is_string($encoding)); if ($encoding === null) { - $encoding = mb_internal_encoding(); - assert(is_string($encoding)); + $encoding = mb_internal_encoding(); $this->options['encoding'] = $encoding; } diff --git a/src/AllowList.php b/src/AllowList.php index ca348a4a..e5211b94 100644 --- a/src/AllowList.php +++ b/src/AllowList.php @@ -58,7 +58,7 @@ public function getStrict() } /** - * Set the list of items to white-list. + * Set the list of items to allow * * @param array|Traversable $list */ @@ -72,7 +72,7 @@ public function setList($list = []): void } /** - * Get the list of items to white-list + * Get the list of items to allow * * @return array */ @@ -84,9 +84,13 @@ public function getList() /** * {@inheritDoc} * - * Will return $value if its present in the white-list. If $value is rejected then it will return null. + * Will return $value if its present in the allow-list. If $value is rejected then it will return null. + * + * @template T + * @param T $value + * @return T|null */ - public function filter($value) + public function filter(mixed $value): mixed { return in_array($value, $this->getList(), $this->getStrict()) ? $value : null; } diff --git a/src/BaseName.php b/src/BaseName.php index 2eedf935..1ccf351a 100644 --- a/src/BaseName.php +++ b/src/BaseName.php @@ -21,11 +21,9 @@ class BaseName extends AbstractFilter * * If the value provided is non-scalar, the value will remain unfiltered * - * @param mixed $value - * @return string|mixed * @psalm-return ($value is scalar ? string : mixed) */ - public function filter($value) + public function filter(mixed $value): mixed { if (! is_scalar($value)) { return $value; diff --git a/src/Boolean.php b/src/Boolean.php index e88c0742..12e01bea 100644 --- a/src/Boolean.php +++ b/src/Boolean.php @@ -207,9 +207,8 @@ public function getTranslations() * Returns a boolean representation of $value * * @param null|array|bool|float|int|string $value - * @return bool|mixed */ - public function filter($value) + public function filter(mixed $value): mixed { $type = $this->getType(); $casting = $this->getCasting(); diff --git a/src/Callback.php b/src/Callback.php index 22e23e23..6a987b88 100644 --- a/src/Callback.php +++ b/src/Callback.php @@ -101,10 +101,10 @@ public function getCallbackParams() /** * Calls the filter per callback * - * @param mixed $value Options for the set callable + * @param mixed $value Options for the set callable * @return mixed Result from the filter which was called */ - public function filter($value) + public function filter(mixed $value): mixed { $params = (array) $this->options['callback_params']; array_unshift($params, $value); diff --git a/src/Compress.php b/src/Compress.php index a7bbbf67..aa8b714a 100644 --- a/src/Compress.php +++ b/src/Compress.php @@ -211,11 +211,11 @@ public function __call($method, $options) * * Compresses the content $value with the defined settings * - * @param mixed $value Content to compress - * @return string|mixed The compressed content + * @param mixed $value Content to compress + * @return mixed The compressed content * @psalm-return ($value is string ? string : mixed) */ - public function filter($value) + public function filter(mixed $value): mixed { if (! is_string($value)) { return $value; diff --git a/src/DataUnitFormatter.php b/src/DataUnitFormatter.php index 62c657f9..ca9114e3 100644 --- a/src/DataUnitFormatter.php +++ b/src/DataUnitFormatter.php @@ -212,11 +212,9 @@ protected function getPrefixAt(mixed $index) * * If the value provided is not numeric, the value will remain unfiltered * - * @param mixed $value - * @return string|mixed * @psalm-return ($value is numeric ? string : mixed) */ - public function filter($value) + public function filter(mixed $value): mixed { if (! is_numeric($value)) { return $value; diff --git a/src/DateSelect.php b/src/DateSelect.php index 72c39e70..dd4e69f4 100644 --- a/src/DateSelect.php +++ b/src/DateSelect.php @@ -10,19 +10,20 @@ * null_on_all_empty?: bool, * ... * } + * @psalm-type InputArray = array{ + * year: numeric, + * month: numeric, + * day: numeric, + * } * @template TOptions of Options - * @template-extends AbstractDateDropdown + * @template-extends AbstractDateDropdown * @final */ class DateSelect extends AbstractDateDropdown { /** * Year-Month-Day - * - * @var string */ - protected $format = '%3$s-%2$s-%1$s'; - - /** @var int */ - protected $expectedInputs = 3; + protected string $format = '%3$s-%2$s-%1$s'; + protected int $expectedInputs = 3; } diff --git a/src/DateTimeFormatter.php b/src/DateTimeFormatter.php index 2aad025a..80127fe5 100644 --- a/src/DateTimeFormatter.php +++ b/src/DateTimeFormatter.php @@ -58,9 +58,8 @@ public function setFormat($format) * * @param DateTime|string|int|mixed $value * @throws Exception\InvalidArgumentException - * @return string|mixed */ - public function filter($value) + public function filter(mixed $value): mixed { try { $result = $this->normalizeDateTime($value); diff --git a/src/DateTimeSelect.php b/src/DateTimeSelect.php index b309cb8c..1f42b9aa 100644 --- a/src/DateTimeSelect.php +++ b/src/DateTimeSelect.php @@ -14,28 +14,28 @@ * null_on_all_empty?: bool, * ... * } + * @psalm-type InputArray = array{ + * year: numeric, + * month: numeric, + * day: numeric, + * hour: numeric, + * minute: numeric, + * second: numeric + * } * @template TOptions of Options - * @template-extends AbstractDateDropdown + * @template-extends AbstractDateDropdown * @final */ class DateTimeSelect extends AbstractDateDropdown { /** * Year-Month-Day Hour:Min:Sec - * - * @var string */ - protected $format = '%6$s-%4$s-%1$s %2$s:%3$s:%5$s'; - - /** @var int */ - protected $expectedInputs = 6; + protected string $format = '%6$s-%4$s-%1$s %2$s:%3$s:%5$s'; + protected int $expectedInputs = 6; - /** - * @param mixed $value - * @return array|mixed|null|string - * @throws Exception\RuntimeException - */ - public function filter($value) + /** @inheritDoc */ + public function filter(mixed $value): mixed { if (! is_array($value)) { // nothing to do @@ -53,7 +53,7 @@ public function filter($value) || (isset($value['second']) && empty($value['second'])) ) ) { - return; + return null; } if ( @@ -64,11 +64,11 @@ public function filter($value) && empty($value['day']) && empty($value['hour']) && empty($value['minute']) - && (! isset($value['second']) || empty($value['second'])) + && empty($value['second']) ) ) { // Cannot handle this value - return; + return null; } if (! isset($value['second'])) { @@ -79,8 +79,6 @@ public function filter($value) ksort($value); - $value = vsprintf($this->format, $value); - - return $value; + return vsprintf($this->format, $value); } } diff --git a/src/Decompress.php b/src/Decompress.php index 51896e85..4eb388bb 100644 --- a/src/Decompress.php +++ b/src/Decompress.php @@ -21,7 +21,7 @@ class Decompress extends Compress * @param string $value Content to decompress * @return string The decompressed content */ - public function __invoke($value) + public function __invoke(mixed $value): mixed { return $this->filter($value); } @@ -31,10 +31,10 @@ 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 The decompressed content */ - public function filter($value) + public function filter(mixed $value): mixed { if (! is_string($value) && $value !== null) { return $value; diff --git a/src/DenyList.php b/src/DenyList.php index 6f66850f..82ea504a 100644 --- a/src/DenyList.php +++ b/src/DenyList.php @@ -86,7 +86,7 @@ public function getList() * * Will return null if $value is present in the black-list. If $value is NOT present then it will return $value. */ - public function filter($value) + public function filter(mixed $value): mixed { return in_array($value, $this->getList(), $this->getStrict()) ? null : $value; } diff --git a/src/Digits.php b/src/Digits.php index 9414c2de..1a10c20a 100644 --- a/src/Digits.php +++ b/src/Digits.php @@ -25,11 +25,9 @@ class Digits extends AbstractFilter * * If the value provided is not integer, float or string, the value will remain unfiltered * - * @param mixed $value - * @return string|mixed * @psalm-return ($value is int|float|string ? numeric-string : mixed) */ - public function filter($value) + public function filter(mixed $value): mixed { if (is_int($value)) { return (string) $value; diff --git a/src/Dir.php b/src/Dir.php index 24862495..7ddb5a0e 100644 --- a/src/Dir.php +++ b/src/Dir.php @@ -19,11 +19,9 @@ class Dir extends AbstractFilter * * Returns dirname($value) * - * @param mixed $value - * @return string|mixed * @psalm-return ($value is scalar ? string : mixed) */ - public function filter($value) + public function filter(mixed $value): mixed { if (! is_scalar($value)) { return $value; diff --git a/src/File/LowerCase.php b/src/File/LowerCase.php index 615733e3..cb47999c 100644 --- a/src/File/LowerCase.php +++ b/src/File/LowerCase.php @@ -24,12 +24,12 @@ class LowerCase extends StringToLower * * Does a lowercase on the content of the given file * - * @param mixed $value Full path of file to change or $_FILES data array - * @return string|mixed The given $value + * @param mixed $value Full path of file to change or $_FILES data array + * @return mixed The given $value * @throws Exception\InvalidArgumentException * @throws Exception\RuntimeException */ - public function filter($value) + public function filter(mixed $value): mixed { if (! is_scalar($value) && ! is_array($value)) { return $value; diff --git a/src/File/Rename.php b/src/File/Rename.php index 66810dc6..77d43731 100644 --- a/src/File/Rename.php +++ b/src/File/Rename.php @@ -177,11 +177,11 @@ 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 + * @return mixed The new filename which has been set * @throws Exception\RuntimeException - * @return string|array The new filename which has been set */ - public function filter($value) + public function filter(mixed $value): mixed { if (! is_scalar($value) && ! is_array($value)) { return $value; diff --git a/src/File/RenameUpload.php b/src/File/RenameUpload.php index 8b7f1e08..94327df0 100644 --- a/src/File/RenameUpload.php +++ b/src/File/RenameUpload.php @@ -229,7 +229,7 @@ public function getRandomize() * - UploadedFileInterface for UploadedFileInterface $value * @throws Exception\RuntimeException */ - public function filter($value) + public function filter(mixed $value): mixed { // PSR-7 uploaded file if ($value instanceof UploadedFileInterface) { diff --git a/src/File/UpperCase.php b/src/File/UpperCase.php index cddd91da..04300c08 100644 --- a/src/File/UpperCase.php +++ b/src/File/UpperCase.php @@ -27,7 +27,7 @@ class UpperCase extends StringToUpper * @throws Exception\RuntimeException * @throws Exception\InvalidArgumentException */ - public function filter($value) + public function filter(mixed $value): mixed { if (! is_scalar($value) && ! is_array($value)) { return $value; diff --git a/src/FilterChain.php b/src/FilterChain.php index 18a84811..8dd800da 100644 --- a/src/FilterChain.php +++ b/src/FilterChain.php @@ -233,11 +233,9 @@ public function getFilters() * * Filters are run in the order in which they were added to the chain (FIFO) * - * @param mixed $value - * @return mixed * @psalm-suppress MixedAssignment values are always mixed */ - public function filter($value) + public function filter(mixed $value): mixed { $valueFiltered = $value; foreach ($this as $filter) { diff --git a/src/FilterInterface.php b/src/FilterInterface.php index 6e75bff8..7d6bc9a5 100644 --- a/src/FilterInterface.php +++ b/src/FilterInterface.php @@ -9,9 +9,9 @@ interface FilterInterface /** * Returns the result of filtering $value * - * @param mixed $value * @throws Exception\RuntimeException If filtering $value is impossible. - * @return mixed */ - public function filter($value); + public function filter(mixed $value): mixed; + + public function __invoke(mixed $value): mixed; } diff --git a/src/HtmlEntities.php b/src/HtmlEntities.php index 8a2584d0..d0ab5c48 100644 --- a/src/HtmlEntities.php +++ b/src/HtmlEntities.php @@ -190,12 +190,10 @@ public function setDoubleQuote($doubleQuote) * * If the value provided is non-scalar, the value will remain unfiltered * - * @param mixed $value - * @return string|mixed * @throws Exception\DomainException On encoding mismatches. * @psalm-return ($value is scalar ? string : mixed) */ - public function filter($value) + public function filter(mixed $value): mixed { if (! is_scalar($value)) { return $value; diff --git a/src/Inflector.php b/src/Inflector.php index ea626cd1..c254980f 100644 --- a/src/Inflector.php +++ b/src/Inflector.php @@ -407,31 +407,30 @@ public function setStaticRuleReference($name, mixed &$reference) /** * Inflect * - * @param string|array $source + * @param string|array $value * @throws Exception\RuntimeException - * @return string */ - public function filter($source) + public function filter(mixed $value): mixed { // clean source - foreach ((array) $source as $sourceName => $sourceValue) { - $source[ltrim($sourceName, ':')] = $sourceValue; + foreach ((array) $value as $sourceName => $sourceValue) { + $value[ltrim($sourceName, ':')] = $sourceValue; } $pregQuotedTargetReplacementIdentifier = preg_quote($this->targetReplacementIdentifier, '#'); $processedParts = []; foreach ($this->rules as $ruleName => $ruleValue) { - if (isset($source[$ruleName])) { + if (isset($value[$ruleName])) { if (is_string($ruleValue)) { // overriding the set rule $processedParts['#' . $pregQuotedTargetReplacementIdentifier . $ruleName . '#'] = str_replace( '\\', '\\\\', - $source[$ruleName] + $value[$ruleName] ); } elseif (is_array($ruleValue)) { - $processedPart = $source[$ruleName]; + $processedPart = $value[$ruleName]; foreach ($ruleValue as $ruleFilter) { $processedPart = $ruleFilter($processedPart); } diff --git a/src/MonthSelect.php b/src/MonthSelect.php index 2722f28f..b7ab5135 100644 --- a/src/MonthSelect.php +++ b/src/MonthSelect.php @@ -10,19 +10,19 @@ * null_on_all_empty?: bool, * ... * } + * @psalm-type InputArray = array{ + * year: numeric, + * month: numeric, + * } * @template TOptions of Options - * @template-extends AbstractDateDropdown + * @template-extends AbstractDateDropdown * @final */ class MonthSelect extends AbstractDateDropdown { /** * Year-Month - * - * @var string */ - protected $format = '%2$s-%1$s'; - - /** @var int */ - protected $expectedInputs = 2; + protected string $format = '%2$s-%1$s'; + protected int $expectedInputs = 2; } diff --git a/src/PregReplace.php b/src/PregReplace.php index c7c300f1..bfd8d0b9 100644 --- a/src/PregReplace.php +++ b/src/PregReplace.php @@ -21,7 +21,7 @@ /** * @psalm-type Options = array{ - * pattern?: string|list|null, + * pattern?: non-empty-string|list|null, * replacement?: string|list, * } * @extends AbstractFilter @@ -67,7 +67,7 @@ public function __construct($options = null) * * @see preg_replace() * - * @param string|list $pattern - same as the first argument of preg_replace + * @param non-empty-string|list $pattern - same as the first argument of preg_replace * @return self * @throws Exception\InvalidArgumentException */ @@ -98,7 +98,7 @@ public function setPattern($pattern) /** * Get currently set match pattern * - * @return string|list|null + * @return non-empty-string|list|null */ public function getPattern() { @@ -140,11 +140,9 @@ public function getReplacement() /** * Perform regexp replacement as filter * - * @param mixed $value - * @return mixed * @throws Exception\RuntimeException */ - public function filter($value) + public function filter(mixed $value): mixed { return self::applyFilterOnlyToStringableValuesAndStringableArrayValues( $value, @@ -158,17 +156,16 @@ public function filter($value) */ private function filterNormalizedValue($value) { - if ($this->options['pattern'] === null) { + $pattern = $this->options['pattern'] ?? null; + if ($pattern === null) { throw new Exception\RuntimeException(sprintf( 'Filter %s does not have a valid pattern set', static::class )); } - /** @var string|string[] $pattern */ - $pattern = $this->options['pattern']; /** @var string|string[] $replacement */ - $replacement = $this->options['replacement']; + $replacement = $this->options['replacement'] ?? ''; return preg_replace($pattern, $replacement, $value); } diff --git a/src/RealPath.php b/src/RealPath.php index 593c7bab..260cf63b 100644 --- a/src/RealPath.php +++ b/src/RealPath.php @@ -83,11 +83,9 @@ public function getExists() * * If the value provided is non-scalar, the value will remain unfiltered * - * @param mixed $value - * @return string|mixed * @psalm-return ($value is string ? string : mixed) */ - public function filter($value) + public function filter(mixed $value): mixed { if (! is_string($value)) { return $value; diff --git a/src/StringPrefix.php b/src/StringPrefix.php index af3d870e..6ebd9324 100644 --- a/src/StringPrefix.php +++ b/src/StringPrefix.php @@ -76,7 +76,7 @@ public function getPrefix() /** * {@inheritdoc} */ - public function filter($value) + public function filter(mixed $value): mixed { if (! is_scalar($value)) { return $value; diff --git a/src/StringSuffix.php b/src/StringSuffix.php index 1b5d7f50..a02990a8 100644 --- a/src/StringSuffix.php +++ b/src/StringSuffix.php @@ -77,7 +77,7 @@ public function getSuffix() /** * {@inheritdoc} */ - public function filter($value) + public function filter(mixed $value): mixed { if (! is_scalar($value)) { return $value; diff --git a/src/StringToLower.php b/src/StringToLower.php index 449b2e59..cf9981be 100644 --- a/src/StringToLower.php +++ b/src/StringToLower.php @@ -34,11 +34,9 @@ public function __construct($encodingOrOptions = null) * * If the value provided is non-scalar, the value will remain unfiltered * - * @param mixed $value - * @return string|mixed * @psalm-return ($value is string ? string : $value) */ - public function filter($value) + public function filter(mixed $value): mixed { if (! is_scalar($value)) { return $value; diff --git a/src/StringToUpper.php b/src/StringToUpper.php index 15f18731..9c7a2a84 100644 --- a/src/StringToUpper.php +++ b/src/StringToUpper.php @@ -34,11 +34,9 @@ public function __construct($encodingOrOptions = null) * * If the value provided is non-scalar, the value will remain unfiltered * - * @param mixed $value - * @return string|mixed * @psalm-return ($value is scalar ? string : mixed) */ - public function filter($value) + public function filter(mixed $value): mixed { if (! is_scalar($value)) { return $value; diff --git a/src/StringTrim.php b/src/StringTrim.php index 917a155f..137c24bb 100644 --- a/src/StringTrim.php +++ b/src/StringTrim.php @@ -73,11 +73,9 @@ public function getCharList() * * Returns the string $value with characters stripped from the beginning and end * - * @param mixed $value - * @return string|mixed * @psalm-return ($value is string ? string : mixed) */ - public function filter($value) + public function filter(mixed $value): mixed { if (! is_string($value)) { return $value; diff --git a/src/StripNewlines.php b/src/StripNewlines.php index 7feb4eac..a5e46f72 100644 --- a/src/StripNewlines.php +++ b/src/StripNewlines.php @@ -19,11 +19,8 @@ class StripNewlines extends AbstractFilter * Defined by Laminas\Filter\FilterInterface * * Returns $value without newline control characters - * - * @param mixed $value - * @return mixed */ - public function filter($value) + public function filter(mixed $value): mixed { return self::applyFilterOnlyToStringableValuesAndStringableArrayValues( $value, diff --git a/src/StripTags.php b/src/StripTags.php index 0436f8e8..caa25447 100644 --- a/src/StripTags.php +++ b/src/StripTags.php @@ -190,11 +190,9 @@ public function setAttributesAllowed($attributesAllowed) * * If the value provided is non-scalar, the value will remain unfiltered * - * @todo improve docblock descriptions - * @param string $value - * @return string|mixed + * @psalm-return ($value is scalar ? string : mixed) */ - public function filter($value) + public function filter(mixed $value): mixed { if (! is_scalar($value)) { return $value; @@ -220,7 +218,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) { diff --git a/src/ToFloat.php b/src/ToFloat.php index f7613e5d..03a07f16 100644 --- a/src/ToFloat.php +++ b/src/ToFloat.php @@ -20,11 +20,9 @@ class ToFloat extends AbstractFilter * * If the value provided is non-scalar, the value will remain unfiltered * - * @param mixed $value - * @return float|mixed * @psalm-return ($value is scalar ? float : mixed) */ - public function filter($value) + public function filter(mixed $value): mixed { if (! is_scalar($value)) { return $value; diff --git a/src/ToInt.php b/src/ToInt.php index be86fd13..99b26a29 100644 --- a/src/ToInt.php +++ b/src/ToInt.php @@ -20,11 +20,9 @@ class ToInt extends AbstractFilter * * If the value provided is non-scalar, the value will remain unfiltered * - * @param mixed $value - * @return int|mixed * @psalm-return ($value is scalar ? int : mixed) */ - public function filter($value) + public function filter(mixed $value): mixed { if (! is_scalar($value)) { return $value; diff --git a/src/ToNull.php b/src/ToNull.php index a2dbaeae..6c772477 100644 --- a/src/ToNull.php +++ b/src/ToNull.php @@ -129,11 +129,8 @@ public function getType() * * Returns null representation of $value, if value is empty and matches * types that should be considered null. - * - * @param null|array|bool|float|int|string $value - * @return null|mixed */ - public function filter($value) + public function filter(mixed $value): mixed { $type = $this->getType(); diff --git a/src/UpperCaseWords.php b/src/UpperCaseWords.php index 90dd6d61..36d7b978 100644 --- a/src/UpperCaseWords.php +++ b/src/UpperCaseWords.php @@ -44,10 +44,10 @@ public function __construct($encodingOrOptions = null) * If the value provided is not a string, the value will remain unfiltered * * @param string|mixed $value - * @return string|mixed + * @return mixed * @psalm-return ($value is string ? string : mixed) */ - public function filter($value) + public function filter(mixed $value): mixed { if (! is_string($value)) { return $value; diff --git a/src/UriNormalize.php b/src/UriNormalize.php index 547afe68..d77beaa0 100644 --- a/src/UriNormalize.php +++ b/src/UriNormalize.php @@ -90,11 +90,8 @@ public function setEnforcedScheme($enforcedScheme) /** * Filter the URL by normalizing it and applying a default scheme if set - * - * @param mixed $value - * @return mixed|string */ - public function filter($value) + public function filter(mixed $value): mixed { if (! is_scalar($value)) { return $value; diff --git a/src/Word/AbstractSeparator.php b/src/Word/AbstractSeparator.php index 46fa45e0..969e4f7f 100644 --- a/src/Word/AbstractSeparator.php +++ b/src/Word/AbstractSeparator.php @@ -5,10 +5,8 @@ namespace Laminas\Filter\Word; use Laminas\Filter\AbstractFilter; -use Laminas\Filter\Exception; use function is_array; -use function is_string; /** * @psalm-type Options = array{ @@ -20,15 +18,14 @@ */ abstract class AbstractSeparator extends AbstractFilter { - /** @var string */ - protected $separator = ' '; + protected string $separator = ' '; /** * @param Options|string $separator Space by default */ - public function __construct($separator = ' ') + public function __construct(string|array $separator = ' ') { - if (is_array($separator) && isset($separator['separator']) && is_string($separator['separator'])) { + if (is_array($separator) && isset($separator['separator'])) { $this->setSeparator($separator['separator']); return; @@ -37,28 +34,14 @@ public function __construct($separator = ' ') $this->setSeparator($separator); } - /** - * Sets a new separator - * - * @param string $separator Separator - * @return self - * @throws Exception\InvalidArgumentException - */ - public function setSeparator($separator) + /** @return $this */ + public function setSeparator(string $separator): self { - if (! is_string($separator)) { - throw new Exception\InvalidArgumentException('"' . $separator . '" is not a valid separator.'); - } $this->separator = $separator; return $this; } - /** - * Returns the actual set separator - * - * @return string - */ - public function getSeparator() + public function getSeparator(): string { return $this->separator; } diff --git a/src/Word/CamelCaseToSeparator.php b/src/Word/CamelCaseToSeparator.php index cdc0d5fa..4b885cf7 100644 --- a/src/Word/CamelCaseToSeparator.php +++ b/src/Word/CamelCaseToSeparator.php @@ -19,11 +19,7 @@ */ class CamelCaseToSeparator extends AbstractSeparator { - /** - * @param mixed $value - * @return mixed - */ - public function filter($value) + public function filter(mixed $value): mixed { return self::applyFilterOnlyToStringableValuesAndStringableArrayValues( $value, @@ -35,7 +31,7 @@ public function filter($value) * @param string|string[] $value * @return string|string[] */ - private function filterNormalizedValue($value) + private function filterNormalizedValue(string|array $value): string|array { if (StringUtils::hasPcreUnicodeSupport()) { $pattern = ['#(?<=(?:\p{Lu}))(\p{Lu}\p{Ll})#', '#(?<=(?:\p{Ll}|\p{Nd}))(\p{Lu})#']; diff --git a/src/Word/DashToSeparator.php b/src/Word/DashToSeparator.php index 76dc898c..b82ceb80 100644 --- a/src/Word/DashToSeparator.php +++ b/src/Word/DashToSeparator.php @@ -19,11 +19,7 @@ */ class DashToSeparator extends AbstractSeparator { - /** - * @param mixed $value - * @return mixed - */ - public function filter($value) + public function filter(mixed $value): mixed { return self::applyFilterOnlyToStringableValuesAndStringableArrayValues( $value, diff --git a/src/Word/SeparatorToCamelCase.php b/src/Word/SeparatorToCamelCase.php index d97f5aca..c4b732d3 100644 --- a/src/Word/SeparatorToCamelCase.php +++ b/src/Word/SeparatorToCamelCase.php @@ -22,11 +22,7 @@ */ class SeparatorToCamelCase extends AbstractSeparator { - /** - * @param mixed $value - * @return mixed - */ - public function filter($value) + public function filter(mixed $value): mixed { return self::applyFilterOnlyToStringableValuesAndStringableArrayValues( $value, diff --git a/src/Word/SeparatorToSeparator.php b/src/Word/SeparatorToSeparator.php index 83fbfcf3..9a63aa7c 100644 --- a/src/Word/SeparatorToSeparator.php +++ b/src/Word/SeparatorToSeparator.php @@ -86,11 +86,9 @@ public function getReplacementSeparator() * * Returns the string $value, replacing the searched separators with the defined ones * - * @param string|mixed $value - * @return mixed * @psalm-return ($value is string ? string : mixed) */ - public function filter($value) + public function filter(mixed $value): mixed { return self::applyFilterOnlyToStringableValuesAndStringableArrayValues( $value, diff --git a/src/Word/UnderscoreToStudlyCase.php b/src/Word/UnderscoreToStudlyCase.php index 461d1be2..3277bbf5 100755 --- a/src/Word/UnderscoreToStudlyCase.php +++ b/src/Word/UnderscoreToStudlyCase.php @@ -27,11 +27,9 @@ class UnderscoreToStudlyCase extends UnderscoreToCamelCase /** * Defined by Laminas\Filter\Filter * - * @param mixed $value - * @return string|array * @psalm-return ($value is scalar ? string : $value is array ? array : mixed) */ - public function filter($value) + public function filter(mixed $value): mixed { if (! is_scalar($value) && ! is_array($value)) { return $value; diff --git a/test/AbstractUnicodeTest.php b/test/AbstractUnicodeTest.php index 7e9f549b..bfd862b6 100644 --- a/test/AbstractUnicodeTest.php +++ b/test/AbstractUnicodeTest.php @@ -23,8 +23,7 @@ protected function setUp(): void parent::setUp(); $this->filter = new class extends AbstractUnicode { - /** @param mixed $value */ - public function filter($value): string + public function filter(mixed $value): mixed { assert(is_string($value)); return strtolower($value); diff --git a/test/TestAsset/LowerCase.php b/test/TestAsset/LowerCase.php index 8f3b65a6..80bf6f6a 100644 --- a/test/TestAsset/LowerCase.php +++ b/test/TestAsset/LowerCase.php @@ -11,7 +11,7 @@ /** @template-extends AbstractFilter */ class LowerCase extends AbstractFilter { - public function filter($value) + public function filter(mixed $value): mixed { return strtolower($value); } diff --git a/test/TestAsset/StripUpperCase.php b/test/TestAsset/StripUpperCase.php index a10f763f..ef7ccbae 100644 --- a/test/TestAsset/StripUpperCase.php +++ b/test/TestAsset/StripUpperCase.php @@ -11,7 +11,7 @@ /** @template-extends AbstractFilter */ class StripUpperCase extends AbstractFilter { - public function filter($value) + public function filter(mixed $value): mixed { return preg_replace('/[A-Z]/', '', $value); }