Skip to content

Commit

Permalink
Merge pull request #53 from gsteel/servicemanager-v2
Browse files Browse the repository at this point in the history
Remove ServiceManager V2 compatibility
  • Loading branch information
Ocramius committed Jun 7, 2022
2 parents a47555d + 884e7ba commit 14dca87
Show file tree
Hide file tree
Showing 14 changed files with 379 additions and 346 deletions.
21 changes: 21 additions & 0 deletions .psr-container-stub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Psr\Container {
/**
* Provides automatic type inference for Psalm when retrieving a service from a container using a FQCN
*/
interface ContainerInterface
{
/** @param string|class-string $id */
public function has(string $id): bool;

/**
* @template T of object
* @psalm-param string|class-string<T> $id
* @psalm-return ($id is class-string ? T : mixed)
*/
public function get(string $id): object;
}
}
356 changes: 218 additions & 138 deletions composer.lock

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions docs/book/specs.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ return [
];
```

When creating a controller, we might then pull the `InputFilterManager`, and
When creating a controller, we might then pull the `InputFilterPluginManager`, and
retrieve the `foobar` input filter we've defined in order to inject it:

```php
use Laminas\InputFilter\InputFilterPluginManager;
use Laminas\ServiceManager\FactoryInterface;
use Laminas\ServiceManager\ServiceLocatorInterface;

Expand All @@ -74,7 +75,7 @@ class MyValidatingControllerFactory implements FactoryInterface
$services = $controllers->getServiceLocator();

// Retrieve the InputFilterManager
$filters = $services->get('InputFilterManager');
$filters = $services->get(InputFilterPluginManager::class);

// Instantiate the controller and pass it the foobar input filter
return new MyValidatingController($filters->get('foobar'));
Expand Down
100 changes: 12 additions & 88 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.23.0@f1fe6ff483bf325c803df9f510d09a03fd796f88">
<file src="src/ArrayInput.php">
<DeprecatedInterface occurrences="1">
<code>ArrayInput</code>
Expand Down Expand Up @@ -141,11 +141,10 @@
<code>setAllowEmpty</code>
<code>setContinueIfEmpty</code>
</DeprecatedMethod>
<DocblockTypeContradiction occurrences="4">
<DocblockTypeContradiction occurrences="3">
<code>! is_array($inputFilterSpecification) &amp;&amp; ! $inputFilterSpecification instanceof Traversable</code>
<code>! is_array($inputSpecification) &amp;&amp; ! $inputSpecification instanceof Traversable</code>
<code>is_array($validator)</code>
<code>null === $this-&gt;inputFilterManager</code>
</DocblockTypeContradiction>
<InvalidReturnStatement occurrences="1">
<code>$inputFilter</code>
Expand Down Expand Up @@ -213,9 +212,6 @@
<code>add</code>
<code>add</code>
</PossiblyUndefinedMethod>
<PropertyNotSetInConstructor occurrences="1">
<code>$inputFilterManager</code>
</PropertyNotSetInConstructor>
<RedundantConditionGivenDocblockType occurrences="10">
<code>$input-&gt;getFilterChain()</code>
<code>$input-&gt;getValidatorChain()</code>
Expand Down Expand Up @@ -414,16 +410,8 @@
<DeprecatedInterface occurrences="1">
<code>InputFilterAbstractServiceFactory</code>
</DeprecatedInterface>
<DeprecatedMethod occurrences="2">
<code>getServiceLocator</code>
<code>getServiceLocator</code>
</DeprecatedMethod>
<MissingConstructor occurrences="1">
<code>$factory</code>
</MissingConstructor>
<MixedArgument occurrences="2">
<MixedArgument occurrences="1">
<code>$config</code>
<code>$container-&gt;get('InputFilterManager')</code>
</MixedArgument>
<MixedArrayAccess occurrences="1">
<code>$allConfig['input_filter_specs']</code>
Expand All @@ -432,14 +420,6 @@
<code>$allConfig</code>
<code>$config</code>
</MixedAssignment>
<MixedInferredReturnType occurrences="2">
<code>FilterPluginManager</code>
<code>ValidatorPluginManager</code>
</MixedInferredReturnType>
<MixedReturnStatement occurrences="2">
<code>$container-&gt;get('FilterManager')</code>
<code>$container-&gt;get('ValidatorManager')</code>
</MixedReturnStatement>
<ParamNameMismatch occurrences="6">
<code>$cName</code>
<code>$container</code>
Expand All @@ -452,61 +432,22 @@
<code>setPluginManager</code>
<code>setPluginManager</code>
</PossiblyNullReference>
<RedundantConditionGivenDocblockType occurrences="3">
<code>$container-&gt;getServiceLocator()</code>
<code>$container-&gt;getServiceLocator()</code>
<code>$this-&gt;factory instanceof Factory</code>
</RedundantConditionGivenDocblockType>
</file>
<file src="src/InputFilterPluginManager.php">
<InvalidScalarArgument occurrences="1">
<code>$e-&gt;getCode()</code>
</InvalidScalarArgument>
<MissingReturnType occurrences="1">
<code>populateFactory</code>
</MissingReturnType>
<MixedArgument occurrences="2">
<code>$container-&gt;get('FilterManager')</code>
<code>$container-&gt;get('ValidatorManager')</code>
</MixedArgument>
<MixedArgumentTypeCoercion occurrences="1">
<code>$v3config</code>
</MixedArgumentTypeCoercion>
<MixedAssignment occurrences="1">
<code>$container</code>
</MixedAssignment>
<MixedMethodCall occurrences="4">
<code>get</code>
<code>get</code>
<code>has</code>
<code>has</code>
</MixedMethodCall>
<MethodSignatureMismatch occurrences="1">
<code>InputFilterPluginManager</code>
</MethodSignatureMismatch>
<NonInvariantDocblockPropertyType occurrences="1">
<code>$factories</code>
</NonInvariantDocblockPropertyType>
<PossiblyNullReference occurrences="2">
<code>setPluginManager</code>
<code>setPluginManager</code>
</PossiblyNullReference>
<PropertyTypeCoercion occurrences="1">
<code>$this-&gt;initializers</code>
</PropertyTypeCoercion>
<UndefinedThisPropertyFetch occurrences="1">
<code>$this-&gt;serviceLocator</code>
</UndefinedThisPropertyFetch>
</file>
<file src="src/InputFilterPluginManagerFactory.php">
<DeprecatedInterface occurrences="1">
<code>InputFilterPluginManagerFactory</code>
</DeprecatedInterface>
<MissingParamType occurrences="3">
<code>$name</code>
<code>$name</code>
<code>$requestedName</code>
</MissingParamType>
<MixedArgument occurrences="1">
<code>$config['input_filters']</code>
</MixedArgument>
<ParamNameMismatch occurrences="1">
<code>$container</code>
</ParamNameMismatch>
Expand Down Expand Up @@ -886,7 +827,7 @@
<code>'invalid_value'</code>
<code>'invalid_value'</code>
</InvalidArgument>
<MissingReturnType occurrences="56">
<MissingReturnType occurrences="55">
<code>testCanCreateInputFilterFromProvider</code>
<code>testCanCreateInputFilterWithNullInputs</code>
<code>testCanCreateInputFromProvider</code>
Expand Down Expand Up @@ -938,7 +879,6 @@
<code>testGetInputFilterManagerWhenYouConstructFactoryWithIt</code>
<code>testInputFromPluginManagerMayBeFurtherConfiguredWithSpec</code>
<code>testSetInputFilterManagerOnConstruct</code>
<code>testSetInputFilterManagerWithServiceManager</code>
<code>testSetInputFilterManagerWithoutServiceManager</code>
<code>testSetsBreakChainOnFailure</code>
<code>testSuggestedTypeMayBePluginNameInInputFilterPluginManager</code>
Expand Down Expand Up @@ -1130,8 +1070,7 @@
</TypeDoesNotContainType>
</file>
<file src="test/InputFilterAbstractServiceFactoryTest.php">
<MissingClosureReturnType occurrences="2">
<code>function () {</code>
<MissingClosureReturnType occurrences="1">
<code>function () {</code>
</MissingClosureReturnType>
<MissingReturnType occurrences="10">
Expand All @@ -1158,11 +1097,7 @@
<code>$inputFilter</code>
<code>$validatorChain</code>
</MixedAssignment>
<MixedMethodCall occurrences="13">
<code>addAbstractFactory</code>
<code>addAbstractFactory</code>
<code>get</code>
<code>get</code>
<MixedMethodCall occurrences="9">
<code>get</code>
<code>getFilterChain</code>
<code>getInputFilterManager</code>
Expand Down Expand Up @@ -1203,13 +1138,12 @@
<MissingClosureReturnType occurrences="1">
<code>function () use ($inputFilter) {</code>
</MissingClosureReturnType>
<MissingReturnType occurrences="7">
<MissingReturnType occurrences="6">
<code>testConfiguresInputFilterServicesWhenFound</code>
<code>testDoesNotConfigureInputFilterServicesWhenConfigServiceDoesNotContainInputFiltersConfig</code>
<code>testDoesNotConfigureInputFilterServicesWhenConfigServiceNotPresent</code>
<code>testDoesNotConfigureInputFilterServicesWhenServiceListenerPresent</code>
<code>testFactoryConfiguresPluginManagerUnderContainerInterop</code>
<code>testFactoryConfiguresPluginManagerUnderServiceManagerV2</code>
<code>testFactoryReturnsPluginManager</code>
</MissingReturnType>
<MoreSpecificReturnType occurrences="1">
Expand Down Expand Up @@ -1355,17 +1289,15 @@
<code>testValidationOperatesOnFilteredValue</code>
<code>testValueMayBeInjected</code>
</MissingReturnType>
<MixedArrayAccess occurrences="5">
<code>$messageTemplates[NotEmptyValidator::IS_EMPTY]</code>
<MixedArrayAccess occurrences="4">
<code>$validators[0]['instance']</code>
<code>$validators[1]['instance']</code>
<code>$value['filtered']</code>
<code>$value['raw']</code>
</MixedArrayAccess>
<MixedAssignment occurrences="5">
<MixedAssignment occurrences="4">
<code>$isValidMethod</code>
<code>$isValidMethod</code>
<code>$messageTemplates</code>
<code>$tmpTemplate[4]</code>
<code>$value</code>
</MixedAssignment>
Expand Down Expand Up @@ -1443,9 +1375,6 @@
<RedundantCondition occurrences="1">
<code>isArray</code>
</RedundantCondition>
<RedundantConditionGivenDocblockType occurrences="1">
<code>assertIsArray</code>
</RedundantConditionGivenDocblockType>
</file>
<file src="test/ModuleTest.php">
<InvalidArgument occurrences="1">
Expand Down Expand Up @@ -1481,11 +1410,6 @@
<code>testValidationFailureWhenInvalidDataSetIsProvided</code>
</MissingReturnType>
</file>
<file src="test/TestAsset/CustomFactory.php">
<PropertyNotSetInConstructor occurrences="1">
<code>CustomFactory</code>
</PropertyNotSetInConstructor>
</file>
<file src="test/TestAsset/CustomInput.php">
<DeprecatedInterface occurrences="1">
<code>CustomInput</code>
Expand Down
6 changes: 5 additions & 1 deletion psalm.xml.dist
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0"?>
<psalm
errorLevel="1"
cacheDirectory="./.psalm-cache"
totallyTyped="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
Expand Down Expand Up @@ -42,4 +42,8 @@
<plugins>
<pluginClass class="Psalm\PhpUnitPlugin\Plugin"/>
</plugins>

<stubs>
<file name=".psr-container-stub.php" preloadClasses="true" />
</stubs>
</psalm>
3 changes: 2 additions & 1 deletion src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Factory
/** @var ValidatorChain */
protected $defaultValidatorChain;

/** @var InputFilterPluginManager */
/** @var InputFilterPluginManager|null */
protected $inputFilterManager;

public function __construct(?InputFilterPluginManager $inputFilterManager = null)
Expand Down Expand Up @@ -103,6 +103,7 @@ public function clearDefaultValidatorChain()
}

/**
* @psalm-assert InputFilterPluginManager $this->inputFilterManager
* @return self
*/
public function setInputFilterManager(InputFilterPluginManager $inputFilterManager)
Expand Down
29 changes: 12 additions & 17 deletions src/InputFilterAbstractServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
use Interop\Container\ContainerInterface;
use Laminas\Filter\FilterPluginManager;
use Laminas\ServiceManager\AbstractFactoryInterface;
use Laminas\ServiceManager\AbstractPluginManager;
use Laminas\ServiceManager\ServiceLocatorInterface;
use Laminas\Validator\ValidatorPluginManager;

use function is_array;

class InputFilterAbstractServiceFactory implements AbstractFactoryInterface
{
/** @var Factory */
/** @var Factory|null */
protected $factory;

/**
Expand Down Expand Up @@ -54,34 +53,30 @@ public function canCreate(ContainerInterface $services, $rName)
/**
* Determine if we can create a service with name (v2)
*
* @deprecated This library is no longer compatible with Service manager V2 and this method will be dropped in the
* next major release.
*
* @param string $name
* @param string $requestedName
* @return bool
*/
public function canCreateServiceWithName(ServiceLocatorInterface $container, $name, $requestedName)
{
// v2 => may need to get parent service locator
if ($container instanceof AbstractPluginManager) {
$container = $container->getServiceLocator() ?: $container;
}

return $this->canCreate($container, $requestedName);
}

/**
* Create the requested service (v2)
*
* @deprecated This library is no longer compatible with Service manager V2 and this method will be dropped in the
* next major release.
*
* @param string $cName
* @param string $rName
* @return InputFilterInterface
*/
public function createServiceWithName(ServiceLocatorInterface $container, $cName, $rName)
{
// v2 => may need to get parent service locator
if ($container instanceof AbstractPluginManager) {
$container = $container->getServiceLocator() ?: $container;
}

return $this($container, $rName);
}

Expand All @@ -102,7 +97,7 @@ protected function getInputFilterFactory(ContainerInterface $container)
->getDefaultValidatorChain()
->setPluginManager($this->getValidatorPluginManager($container));

$this->factory->setInputFilterManager($container->get('InputFilterManager'));
$this->factory->setInputFilterManager($container->get(InputFilterPluginManager::class));

return $this->factory;
}
Expand All @@ -112,8 +107,8 @@ protected function getInputFilterFactory(ContainerInterface $container)
*/
protected function getFilterPluginManager(ContainerInterface $container)
{
if ($container->has('FilterManager')) {
return $container->get('FilterManager');
if ($container->has(FilterPluginManager::class)) {
return $container->get(FilterPluginManager::class);
}

return new FilterPluginManager($container);
Expand All @@ -124,8 +119,8 @@ protected function getFilterPluginManager(ContainerInterface $container)
*/
protected function getValidatorPluginManager(ContainerInterface $container)
{
if ($container->has('ValidatorManager')) {
return $container->get('ValidatorManager');
if ($container->has(ValidatorPluginManager::class)) {
return $container->get(ValidatorPluginManager::class);
}

return new ValidatorPluginManager($container);
Expand Down
Loading

0 comments on commit 14dca87

Please sign in to comment.