-
-
Notifications
You must be signed in to change notification settings - Fork 31
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
Improve inference for InputFilterPluginManager::get()
#97
Changes from 4 commits
15e9e3d
774bb40
be8b829
be509ec
68627d4
765bebb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -317,6 +318,7 @@ public function createInputFilter($inputFilterSpecification) | |
} | ||
|
||
$inputFilter = $this->getInputFilterManager()->get($type); | ||
assert($inputFilter instanceof InputFilterInterface); // As opposed to InputInterface | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How do we know it is not an InputInterface here? InputFilterManager allows both There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this ought to be full check for input filter with meaningful exception, not just an assert since the method is user facing and there is no soft guard somewhere else. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An exception here would be a BC break though wouldn't it? The assertion will blow up in tests, it verifies the intended type for static analysis, and otherwise is a no-op for users There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It wouldn't be. It would just be a fatal error if method not implemented or kinda work with anything as long as object has There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is more of a usability concern really. Exception would allow to say it needs to be input filter and get_debug_type was returned for the requested type |
||
|
||
if ($inputFilter instanceof CollectionInputFilter) { | ||
$inputFilter->setFactory($this); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
{ | ||
|
@@ -188,4 +187,17 @@ public function validatePlugin($plugin) | |
throw new Exception\RuntimeException($e->getMessage(), $e->getCode(), $e); | ||
} | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
* @template T1 of InputInterface | ||
* @template T2 of InputFilterInterface | ||
* @param class-string<T1>|class-string<T2>|string $name | ||
* @return T1|T2 | ||
* @psalm-return ($name is class-string<T1> ? T1 : ($name is class-string<T2> ? T2 : T1|T2)) | ||
*/ | ||
public function get($name, ?array $options = null) | ||
{ | ||
return parent::get($name, $options); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't the entire signature the same as Overall OK with this, just seems redundant with the parent DIC declarations There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am testing this locally. It seems it could be simplified. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried following:
Result is close:
For string parameters values are wrong. However if I replace conditional fallback with explicit types it works out:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually nevermind. This is wrong with both your version and my smiplification
I don't think conditional actually uses template there. It creates transient template instead used only for conditional. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was having problems with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, so here is the results:
This indicates that template constraint is This is closer but has other issues:
Psalm then insists on throwing in mixed here
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is uncharted waters and I feel we are hitting at least couple edge case bugs here. So, for current psalm version following works:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @gsteel Psalm tests could be improved with https://psalm.dev/docs/annotating_code/supported_annotations/#psalm-check-type to ensure we get expected types I am sure it will not cause us any more grief on another lockfile maintenance PR 😆 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Xerkus - just looking at this now and unfortunately, the string case union return type is messed up: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://psalm.dev/r/6e213fb5a6 Looks like a bug with |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?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); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why psalm complains about plugin manager get? Does it want simple typehinted return and conditional be in psalm-return?
Also what is the MethodSignatureMismatch just above that was previously baselined? Is it due to psr container aliasing shenanigans?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To answer it: some inherited weirdness going on.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think a lot of this will be solved with SMv4 when we can drop PSR container v1 and tighten up the templates and types in the plugin manager.