Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Factory cleanup #128

Merged
merged 6 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 4 additions & 72 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.23.1@8471a896ccea3526b26d082f4461eeea467f10a4">
<file src="src/AbstractDateDropdown.php">
<MixedArgumentTypeCoercion>
<code><![CDATA[$value]]></code>
</MixedArgumentTypeCoercion>
<PossiblyUnusedMethod>
<code><![CDATA[setNullOnAllEmpty]]></code>
<code><![CDATA[setNullOnEmpty]]></code>
</PossiblyUnusedMethod>
<RiskyTruthyFalsyComparison>
<code><![CDATA[empty($value)]]></code>
</RiskyTruthyFalsyComparison>
</file>
<file src="src/AbstractFilter.php">
<DocblockTypeContradiction>
Expand Down Expand Up @@ -325,9 +319,6 @@
</PossiblyInvalidArgument>
</file>
<file src="src/DateTimeSelect.php">
<MixedArgumentTypeCoercion>
<code><![CDATA[$value]]></code>
</MixedArgumentTypeCoercion>
<MixedReturnStatement>
<code><![CDATA[$value]]></code>
</MixedReturnStatement>
Expand Down Expand Up @@ -549,32 +540,6 @@
<code><![CDATA[$shareByDefault]]></code>
</PossiblyUnusedProperty>
</file>
<file src="src/FilterPluginManagerFactory.php">
<DeprecatedClass>
<code><![CDATA[new Config($config['filters'])]]></code>
</DeprecatedClass>
<DeprecatedInterface>
<code><![CDATA[FilterPluginManagerFactory]]></code>
</DeprecatedInterface>
<MissingParamType>
<code><![CDATA[$name]]></code>
<code><![CDATA[$name]]></code>
<code><![CDATA[$requestedName]]></code>
</MissingParamType>
<MissingPropertyType>
<code><![CDATA[$creationOptions]]></code>
</MissingPropertyType>
<MixedArgument>
<code><![CDATA[$config['filters']]]></code>
<code><![CDATA[$this->creationOptions]]></code>
</MixedArgument>
<MoreSpecificImplementedParamType>
<code><![CDATA[$options]]></code>
</MoreSpecificImplementedParamType>
<ParamNameMismatch>
<code><![CDATA[$container]]></code>
</ParamNameMismatch>
</file>
<file src="src/FilterProviderInterface.php">
<UnusedClass>
<code><![CDATA[FilterProviderInterface]]></code>
Expand Down Expand Up @@ -852,23 +817,10 @@
</MixedReturnStatement>
</file>
<file src="src/Word/Service/SeparatorToSeparatorFactory.php">
<DeprecatedInterface>
<code><![CDATA[SeparatorToSeparatorFactory]]></code>
</DeprecatedInterface>
<MissingParamType>
<code><![CDATA[$creationOptions]]></code>
</MissingParamType>
<MissingReturnType>
<code><![CDATA[setCreationOptions]]></code>
</MissingReturnType>
<MixedArgument>
<code><![CDATA[$options['replacement_separator'] ?? '-']]></code>
<code><![CDATA[$options['search_separator'] ?? ' ']]></code>
</MixedArgument>
<PossiblyUnusedMethod>
<code><![CDATA[__construct]]></code>
<code><![CDATA[setCreationOptions]]></code>
</PossiblyUnusedMethod>
<UnusedParam>
<code><![CDATA[$container]]></code>
<code><![CDATA[$requestedName]]></code>
</UnusedParam>
</file>
<file src="src/Word/UnderscoreToStudlyCase.php">
<MissingClosureParamType>
Expand Down Expand Up @@ -1088,26 +1040,6 @@
<code><![CDATA[staticUcaseFilter]]></code>
</PossiblyUnusedMethod>
</file>
<file src="test/FilterPluginManagerFactoryTest.php">
<DeprecatedMethod>
<code><![CDATA[getServiceLocator]]></code>
</DeprecatedMethod>
<MissingClosureParamType>
<code><![CDATA[$container]]></code>
<code><![CDATA[$value]]></code>
<code><![CDATA[$value]]></code>
</MissingClosureParamType>
<MissingClosureReturnType>
<code><![CDATA[static fn($value) => $value]]></code>
<code><![CDATA[static fn($value) => $value]]></code>
</MissingClosureReturnType>
<MissingReturnType>
<code><![CDATA[testFactoryConfiguresPluginManagerUnderServiceManagerV2]]></code>
</MissingReturnType>
<UnusedClosureParam>
<code><![CDATA[$container]]></code>
</UnusedClosureParam>
</file>
<file src="test/HtmlEntitiesTest.php">
<PossiblyUnusedMethod>
<code><![CDATA[returnUnfilteredDataProvider]]></code>
Expand Down
17 changes: 9 additions & 8 deletions src/AbstractDateDropdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Laminas\Filter;

use function array_reduce;
use function assert;
use function count;
use function is_array;
use function is_iterable;
Expand All @@ -22,7 +21,7 @@
* @psalm-type InputArray = array<string, string>
* @template TOptions of Options
* @template-extends AbstractFilter<TOptions>
* @template TInput of array
* @template TInput of array<array-key, numeric>
*/
abstract class AbstractDateDropdown extends AbstractFilter
{
Expand Down Expand Up @@ -99,22 +98,23 @@ public function filter(mixed $value): mixed
// Convert the date to a specific format
if (
$this->isNullOnEmpty()
&& array_reduce($value, [self::class, 'reduce'], false)
&& array_reduce($value, self::reduce(...), false)
) {
return null;
}

if (
$this->isNullOnAllEmpty()
&& array_reduce($value, [self::class, 'reduce'], true)
&& array_reduce($value, self::reduce(...), true)
) {
return null;
}

ksort($value);
$this->filterable($value);
assert(is_array($value));

ksort($value);
/** @psalm-var array<array-key, string> $value Forcing the type here because it has already been asserted */

return vsprintf($this->format, $value);
}

Expand All @@ -123,6 +123,7 @@ public function filter(mixed $value): mixed
*
* @param array $value
* @throws Exception\RuntimeException
* @psalm-assert TInput $value
gsteel marked this conversation as resolved.
Show resolved Hide resolved
*/
protected function filterable(array $value): void
{
Expand All @@ -140,8 +141,8 @@ protected function filterable(array $value): void
/**
* Reduce to a single value
*/
private static function reduce(string $soFar, string|null $value): bool
private static function reduce(bool $soFar, string|null $value): bool
{
return $soFar || empty($value);
return $soFar || ($value === null || $value === '');
}
}
2 changes: 1 addition & 1 deletion src/DateTimeSelect.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* day: numeric,
* hour: numeric,
* minute: numeric,
* second: numeric
* second: numeric,
* }
* @template TOptions of Options
* @template-extends AbstractDateDropdown<TOptions, InputArray>
Expand Down
2 changes: 1 addition & 1 deletion src/Decompress.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __invoke(mixed $value): mixed
*
* Decompresses the content $value with the defined settings
*
* @param mixed $value Content to decompress
* @param mixed $value Content to decompress
* @return mixed|string The decompressed content
*/
public function filter(mixed $value): mixed
Expand Down
53 changes: 7 additions & 46 deletions src/FilterPluginManagerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

namespace Laminas\Filter;

use Laminas\ServiceManager\Config;
use Laminas\ServiceManager\FactoryInterface;
use Laminas\ServiceManager\ServiceLocatorInterface;
use Laminas\ServiceManager\ServiceManager;
use Psr\Container\ContainerInterface;

Expand All @@ -15,66 +12,30 @@
/**
* @psalm-import-type ServiceManagerConfiguration from ServiceManager
*/
class FilterPluginManagerFactory implements FactoryInterface
final class FilterPluginManagerFactory
{
/**
* laminas-servicemanager v2 support for invocation options.
*
* @param array
*/
protected $creationOptions;

/**
* {@inheritDoc}
*
* @param ServiceManagerConfiguration|null $options
* @return FilterPluginManager
*/
public function __invoke(ContainerInterface $container, $name, ?array $options = null)
public function __invoke(ContainerInterface $container): FilterPluginManager
{
$pluginManager = new FilterPluginManager($container, $options ?? []);

// If this is in a laminas-mvc application, the ServiceListener will inject
// merged configuration during bootstrap.
if ($container->has('ServiceListener')) {
return $pluginManager;
return new FilterPluginManager($container);
}

// If we do not have a config service, nothing more to do
if (! $container->has('config')) {
return $pluginManager;
return new FilterPluginManager($container);
}

$config = $container->get('config');

// If we do not have filters configuration, nothing more to do
if (! isset($config['filters']) || ! is_array($config['filters'])) {
return $pluginManager;
return new FilterPluginManager($container);
}

// Wire service configuration for validators
(new Config($config['filters']))->configureServiceManager($pluginManager);

return $pluginManager;
}

/**
* {@inheritDoc}
*
* @return FilterPluginManager
*/
public function createService(ServiceLocatorInterface $container, $name = null, $requestedName = null)
{
return $this($container, $requestedName ?: FilterPluginManager::class, $this->creationOptions);
}
/** @psalm-var ServiceManagerConfiguration $config['filters'] */

/**
* laminas-servicemanager v2 support for invocation options.
*
* @return void
*/
public function setCreationOptions(array $options)
{
$this->creationOptions = $options;
return new FilterPluginManager($container, $config['filters']);
}
}
66 changes: 15 additions & 51 deletions src/Word/Service/SeparatorToSeparatorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,65 +5,29 @@
namespace Laminas\Filter\Word\Service;

use Laminas\Filter\Word\SeparatorToSeparator;
use Laminas\ServiceManager\Exception\InvalidServiceException;
use Laminas\ServiceManager\FactoryInterface;
use Laminas\ServiceManager\ServiceLocatorInterface;
use Psr\Container\ContainerInterface;
use Traversable;

use function get_debug_type;
use function is_array;
use function iterator_to_array;
use function sprintf;
use function assert;
use function is_string;

final class SeparatorToSeparatorFactory implements FactoryInterface
final class SeparatorToSeparatorFactory
{
/**
* Options to pass to the constructor (when used in v2), if any.
*
* @param null|array
*/
private array $creationOptions = [];
public function __invoke(
ContainerInterface $container,
string $requestedName,
?array $options = null,
): SeparatorToSeparator {
$options = $options ?? [];

public function __construct($creationOptions = null)
{
if (null === $creationOptions) {
return;
}
$searchSeparator = $options['search_separator'] ?? ' ';
$replacementSeparator = $options['replacement_separator'] ?? '-';

if ($creationOptions instanceof Traversable) {
$creationOptions = iterator_to_array($creationOptions);
}
assert(is_string($searchSeparator));
assert(is_string($replacementSeparator));

if (! is_array($creationOptions)) {
throw new InvalidServiceException(sprintf(
'%s cannot use non-array, non-traversable creation options; received %s',
self::class,
get_debug_type($creationOptions)
));
}

$this->creationOptions = $creationOptions;
}

/**
* {@inheritDoc}
*/
public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null)
{
return new SeparatorToSeparator(
$options['search_separator'] ?? ' ',
$options['replacement_separator'] ?? '-'
$searchSeparator,
$replacementSeparator
);
}

public function createService(ServiceLocatorInterface $serviceLocator)
{
return $this($serviceLocator, self::class, $this->creationOptions);
}

public function setCreationOptions(array $options)
{
$this->creationOptions = $options;
}
}
Loading