Skip to content

Commit

Permalink
Merge pull request #97 from gsteel/manager-get-inference
Browse files Browse the repository at this point in the history
Improve inference for `InputFilterPluginManager::get()`
  • Loading branch information
Xerkus committed Jan 10, 2024
2 parents ee59d59 + 765bebb commit 82a76f4
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 14 deletions.
18 changes: 7 additions & 11 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,6 @@
</PossiblyUnusedReturnValue>
</file>
<file src="src/Factory.php">
<InvalidReturnStatement>
<code>$inputFilter</code>
</InvalidReturnStatement>
<InvalidReturnType>
<code>InputFilterInterface</code>
</InvalidReturnType>
<MixedArgument>
<code><![CDATA[$inputFilterSpecification['count']]]></code>
<code><![CDATA[$inputFilterSpecification['input_filter']]]></code>
Expand Down Expand Up @@ -142,10 +136,6 @@
<code>$value</code>
<code>$value</code>
</PossiblyInvalidArgument>
<PossiblyUndefinedMethod>
<code>add</code>
<code>add</code>
</PossiblyUndefinedMethod>
<UnusedPsalmSuppress>
<code>DeprecatedMethod</code>
<code>DocblockTypeContradiction</code>
Expand Down Expand Up @@ -289,8 +279,14 @@
<code>InputFilterPluginManager</code>
</MethodSignatureMismatch>
<MethodSignatureMustProvideReturnType>
<code>Plu</code>
<code>get</code>
</MethodSignatureMustProvideReturnType>
<MixedInferredReturnType>
<code><![CDATA[($name is class-string<InputInterface> ? T1 : ($name is class-string<InputFilterInterface> ? T2 : InputInterface|InputFilterInterface))]]></code>
</MixedInferredReturnType>
<MixedReturnStatement>
<code>parent::get($name, $options)</code>
</MixedReturnStatement>
<NonInvariantDocblockPropertyType>
<code>$factories</code>
</NonInvariantDocblockPropertyType>
Expand Down
13 changes: 13 additions & 0 deletions psalm.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@
<pluginClass class="Psalm\PhpUnitPlugin\Plugin"/>
</plugins>

<issueHandlers>
<UnusedClass>
<errorLevel type="suppress">
<directory name="test/StaticAnalysis" />
</errorLevel>
</UnusedClass>
<PossiblyUnusedMethod>
<errorLevel type="suppress">
<directory name="test/StaticAnalysis" />
</errorLevel>
</PossiblyUnusedMethod>
</issueHandlers>

<stubs>
<file name=".psr-container.php.stub" preloadClasses="true" />
</stubs>
Expand Down
2 changes: 2 additions & 0 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Laminas\Validator\ValidatorInterface;
use Traversable;

use function assert;
use function class_exists;
use function get_debug_type;
use function gettype;
Expand Down Expand Up @@ -317,6 +318,7 @@ public function createInputFilter($inputFilterSpecification)
}

$inputFilter = $this->getInputFilterManager()->get($type);
assert($inputFilter instanceof InputFilterInterface); // As opposed to InputInterface

if ($inputFilter instanceof CollectionInputFilter) {
$inputFilter->setFactory($this);
Expand Down
15 changes: 14 additions & 1 deletion src/InputFilterPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
* @psalm-import-type ServiceManagerConfiguration from ServiceManager
* @template InstanceType of InputFilterInterface|InputInterface
* @extends AbstractPluginManager<InstanceType>
* @method InputFilterInterface|InputInterface get(string $name, ?array $options = null)
*/
class InputFilterPluginManager extends AbstractPluginManager
{
Expand Down Expand Up @@ -188,4 +187,18 @@ public function validatePlugin($plugin)
throw new Exception\RuntimeException($e->getMessage(), $e->getCode(), $e);
}
}

/**
* @inheritDoc
* phpcs:disable Generic.Files.LineLength.TooLong
* // Template constraint required or we get mixed added to output. Two templates because union does not work
* @template T1 of InputInterface
* @template T2 of InputFilterInterface
* @param class-string<T1>|class-string<T2>|string $name
* @return ($name is class-string<InputInterface> ? T1 : ($name is class-string<InputFilterInterface> ? T2 : InputInterface|InputFilterInterface))
*/
public function get($name, ?array $options = null)
{
return parent::get($name, $options);
}
}
1 change: 0 additions & 1 deletion test/StaticAnalysis/AddingInputsWithArraySpecs.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

/**
* @extends InputFilter<array<string, mixed>>
* @psalm-suppress UnusedClass
*/
final class AddingInputsWithArraySpecs extends InputFilter
{
Expand Down
32 changes: 32 additions & 0 deletions test/StaticAnalysis/InputFilterPluginManagerType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace LaminasTest\InputFilter\StaticAnalysis;

use Laminas\InputFilter\InputFilterInterface;
use Laminas\InputFilter\InputFilterPluginManager;
use Laminas\InputFilter\InputInterface;

final class InputFilterPluginManagerType
{
public function __construct(private InputFilterPluginManager $manager)
{
}

public function getWillReturnAnInputOrInputFilterGivenAString(
string $anyString,
): InputInterface|InputFilterInterface {
return $this->manager->get($anyString);
}

public function getWithFQCNWillReturnTheObjectOfType(): InputFilterWithTemplatedValues
{
return $this->manager->get(InputFilterWithTemplatedValues::class);
}

public function getInvalidFQCNReturnsFallbackType(): InputInterface|InputFilterInterface
{
return $this->manager->get(self::class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use function count;
use function reset;

/** @psalm-suppress UnusedClass */
final class InputFilterTemplateInfersFilterValueTypes
{
public function __construct(
Expand Down

0 comments on commit 82a76f4

Please sign in to comment.