Skip to content

Commit

Permalink
Merge pull request #64 from gsteel/iterable-filter-chain
Browse files Browse the repository at this point in the history
The Filter Chain should be iterable
  • Loading branch information
Ocramius authored Sep 20, 2022
2 parents c38e90b + 4ced3af commit 06322d9
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 76 deletions.
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,18 @@
"require": {
"php": "^7.4 || ~8.0.0 || ~8.1.0",
"laminas/laminas-servicemanager": "^3.14.0",
"laminas/laminas-stdlib": "^3.6.1"
"laminas/laminas-stdlib": "^3.13.0"
},
"require-dev": {
"laminas/laminas-coding-standard": "^2.3.0",
"laminas/laminas-coding-standard": "~2.4.0",
"laminas/laminas-crypt": "^3.5.1",
"laminas/laminas-uri": "^2.9.1",
"pear/archive_tar": "^1.4.14",
"phpspec/prophecy-phpunit": "^2.0.1",
"phpunit/phpunit": "^9.5.10",
"phpunit/phpunit": "^9.5.24",
"psalm/plugin-phpunit": "^0.17.0",
"psr/http-factory": "^1.0.1",
"vimeo/psalm": "^4.24.0"
"vimeo/psalm": "^4.27.0"
},
"conflict": {
"laminas/laminas-validator": "<2.10.1",
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

76 changes: 11 additions & 65 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="4.13.1@5cf660f63b548ccd4a56f62d916ee4d6028e01a3">
<files psalm-version="4.27.0@faf106e717c37b8c81721845dba9de3d8deed8ff">
<file src="src/AbstractDateDropdown.php">
<MissingReturnType occurrences="1">
<code>filterable</code>
Expand Down Expand Up @@ -410,9 +410,6 @@
<code>strrpos($content, DIRECTORY_SEPARATOR)</code>
<code>strrpos($content, DIRECTORY_SEPARATOR)</code>
</PossiblyFalseOperand>
<PossiblyNullReference occurrences="1">
<code>read</code>
</PossiblyNullReference>
<RedundantCast occurrences="1">
<code>(string) $target</code>
</RedundantCast>
Expand Down Expand Up @@ -876,30 +873,9 @@
</RedundantPropertyInitializationCheck>
</file>
<file src="src/File/LowerCase.php">
<DocblockTypeContradiction occurrences="1">
<code>! is_scalar($value) &amp;&amp; ! is_array($value)</code>
</DocblockTypeContradiction>
<MixedArgument occurrences="4">
<code>$value</code>
<code>$value</code>
<code>$value</code>
<code>$value</code>
</MixedArgument>
<MixedAssignment occurrences="2">
<code>$content</code>
<MixedAssignment occurrences="1">
<code>$value</code>
</MixedAssignment>
<MixedInferredReturnType occurrences="1">
<code>string|array</code>
</MixedInferredReturnType>
<MixedReturnStatement occurrences="3">
<code>$uploadData</code>
<code>$value</code>
<code>$value</code>
</MixedReturnStatement>
<MoreSpecificImplementedParamType occurrences="1">
<code>$value</code>
</MoreSpecificImplementedParamType>
<PossiblyUndefinedVariable occurrences="1">
<code>$uploadData</code>
</PossiblyUndefinedVariable>
Expand Down Expand Up @@ -1112,9 +1088,8 @@
<code>gettype($callback)</code>
<code>gettype($options)</code>
</DocblockTypeContradiction>
<MixedArgument occurrences="6">
<MixedArgument occurrences="5">
<code>$callback</code>
<code>$item['data']</code>
<code>$key</code>
<code>$name</code>
<code>$priority</code>
Expand All @@ -1127,9 +1102,8 @@
<code>$spec['priority']</code>
<code>$spec['priority']</code>
</MixedArrayAccess>
<MixedAssignment occurrences="12">
<MixedAssignment occurrences="9">
<code>$callback</code>
<code>$filter</code>
<code>$key</code>
<code>$name</code>
<code>$options</code>
Expand All @@ -1138,32 +1112,25 @@
<code>$spec</code>
<code>$spec</code>
<code>$value</code>
<code>$valueFiltered</code>
<code>$valueFiltered</code>
</MixedAssignment>
<MixedFunctionCall occurrences="1">
<code>call_user_func($filter, $valueFiltered)</code>
</MixedFunctionCall>
<MixedPropertyTypeCoercion occurrences="1">
<code>new PriorityQueue()</code>
</MixedPropertyTypeCoercion>
<RedundantConditionGivenDocblockType occurrences="2">
<code>is_object($callback)</code>
<code>is_object($options)</code>
</RedundantConditionGivenDocblockType>
</file>
<file src="src/FilterPluginManager.php">
<DeprecatedClass occurrences="4">
<DeprecatedClass occurrences="2">
<code>Blacklist::class</code>
<code>Whitelist::class</code>
<code>\Zend\Filter\Blacklist::class</code>
<code>\Zend\Filter\Whitelist::class</code>
</DeprecatedClass>
<DuplicateArrayKey occurrences="2">
<code>ToInt::class =&gt; InvokableFactory::class</code>
<code>ToNull::class =&gt; InvokableFactory::class</code>
</DuplicateArrayKey>
<InvalidScalarArgument occurrences="1">
<code>$e-&gt;getCode()</code>
</InvalidScalarArgument>
<MixedArrayOffset occurrences="2"/>
<MixedArrayOffset occurrences="1"/>
<MixedInferredReturnType occurrences="1">
<code>($name is class-string ? InstanceType : callable(mixed): mixed)</code>
</MixedInferredReturnType>
Expand All @@ -1174,7 +1141,7 @@
<code>$name</code>
<code>$plugin</code>
</ParamNameMismatch>
<UndefinedClass occurrences="26">
<UndefinedClass occurrences="22">
<code>Alnum</code>
<code>Alnum</code>
<code>Alnum</code>
Expand All @@ -1197,10 +1164,6 @@
<code>NumberParse</code>
<code>NumberParse</code>
<code>NumberParse</code>
<code>\Zend\I18n\Filter\Alnum</code>
<code>\Zend\I18n\Filter\Alpha</code>
<code>\Zend\I18n\Filter\NumberFormat</code>
<code>\Zend\I18n\Filter\NumberParse</code>
</UndefinedClass>
</file>
<file src="src/FilterPluginManagerFactory.php">
Expand Down Expand Up @@ -1432,22 +1395,13 @@
</PossiblyInvalidArgument>
</file>
<file src="src/StringToLower.php">
<DocblockTypeContradiction occurrences="1">
<code>is_scalar($value)</code>
</DocblockTypeContradiction>
<MixedArgument occurrences="1">
<code>$this-&gt;options['encoding']</code>
</MixedArgument>
<MoreSpecificImplementedParamType occurrences="1">
<code>$value</code>
</MoreSpecificImplementedParamType>
<PossiblyInvalidArgument occurrences="2">
<code>$encodingOrOptions</code>
<code>$encodingOrOptions</code>
</PossiblyInvalidArgument>
<RedundantCastGivenDocblockType occurrences="1">
<code>(string) $value</code>
</RedundantCastGivenDocblockType>
<RedundantConditionGivenDocblockType occurrences="1">
<code>null !== $this-&gt;getEncoding()</code>
</RedundantConditionGivenDocblockType>
Expand Down Expand Up @@ -2473,9 +2427,6 @@
</MissingReturnType>
</file>
<file src="test/FilterChainTest.php">
<MissingClosureParamType occurrences="1">
<code>$value</code>
</MissingClosureParamType>
<MissingParamType occurrences="1">
<code>$value</code>
</MissingParamType>
Expand All @@ -2490,12 +2441,10 @@
<code>$value</code>
<code>$value</code>
</MixedArgument>
<MixedAssignment occurrences="5">
<code>$compare</code>
<MixedAssignment occurrences="3">
<code>$config</code>
<code>$config</code>
<code>$config</code>
<code>$filter</code>
</MixedAssignment>
</file>
<file src="test/FilterPluginManagerCompatibilityTest.php">
Expand Down Expand Up @@ -2725,9 +2674,6 @@
<MissingReturnType occurrences="1">
<code>returnUnfilteredDataProvider</code>
</MissingReturnType>
<MixedArgument occurrences="1">
<code>$input</code>
</MixedArgument>
</file>
<file src="test/StringToUpperTest.php">
<DeprecatedMethod occurrences="1">
Expand Down
25 changes: 19 additions & 6 deletions src/FilterChain.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Laminas\Filter;

use Countable;
use IteratorAggregate;
use Laminas\ServiceManager\ServiceManager;
use Laminas\Stdlib\PriorityQueue;
use ReturnTypeWillChange;
Expand All @@ -22,8 +23,9 @@

/**
* @final
* @implements IteratorAggregate<array-key, FilterInterface|callable(mixed): mixed>
*/
class FilterChain extends AbstractFilter implements Countable
class FilterChain extends AbstractFilter implements Countable, IteratorAggregate
{
/**
* Default priority at which filters are added
Expand All @@ -36,7 +38,7 @@ class FilterChain extends AbstractFilter implements Countable
/**
* Filter chain
*
* @var PriorityQueue
* @var PriorityQueue<FilterInterface|callable(mixed): mixed, int>
*/
protected $filters;

Expand Down Expand Up @@ -211,7 +213,7 @@ public function merge(FilterChain $filterChain)
/**
* Get all the filters
*
* @return PriorityQueue
* @return PriorityQueue<FilterInterface|callable(mixed): mixed, int>
*/
public function getFilters()
{
Expand All @@ -225,13 +227,18 @@ public function getFilters()
*
* @param mixed $value
* @return mixed
* @psalm-suppress MixedAssignment values are always mixed
*/
public function filter($value)
{
$chain = clone $this->filters;

$valueFiltered = $value;
foreach ($chain as $filter) {
foreach ($this as $filter) {
if ($filter instanceof FilterInterface) {
$valueFiltered = $filter->filter($valueFiltered);

continue;
}

$valueFiltered = call_user_func($filter, $valueFiltered);
}

Expand All @@ -258,4 +265,10 @@ public function __sleep()
{
return ['filters'];
}

/** @return Traversable<array-key, FilterInterface|callable(mixed): mixed> */
public function getIterator(): Traversable
{
return clone $this->filters;
}
}

0 comments on commit 06322d9

Please sign in to comment.