-
Notifications
You must be signed in to change notification settings - Fork 667
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
Feature: Psalm pseudo-type for instantiable-object
#6075
Comments
I found these snippets: https://psalm.dev/r/16e0076766<?php
interface Foo
{
}
/**
* @template T of Foo
*/
final class Bar
{
/**
* @psalm-var class-string<T>
*/
private string $bar;
/**
* @psalm-param class-string<T> $bar
*/
public function __construct(string $bar)
{
$this->bar = $bar;
}
/**
* @psalm-return T
*/
public function createInstance(): object
{
$className = $this->bar;
return new $className;
}
}
$factory = new Bar(Foo::class);
$factory->createInstance();
|
This actually looks like a bug to me. Not only because we don't know whether the class-string is instantiable, but also because constructor signature is unknown. Even if it's instantiable, it may have required arguments and would still fail at runtime. |
Yeah, in retrospect making interface names pass a class-string check was a mistake. I think that could be a major-version change. |
actually it probably doesn't need to be a major-version change |
This is quite a big refactor, I'll have a look at it this week |
Actually this is more complex. I’m afraid introducing the change before a major version would induce gnashing of teeth. I foresee this change happening in two steps: the first very minor step makes This will help prepare for the next step, which would happen in a major version change, changing the meaning of |
Sounds reasonable and I'd be fine with that. Shall we move this to a dedicated issue instead? This issue was initially meant to be a feature request which tells psalm that only classes are accepted which are safely instantiable without any So one can simply do But I see the pitfalls, so maybe keeping it to track (even for me) so this can be probably achieved later on. But I'd be fine with closing this as |
Fixes #6075 by preventing that class of potential errors
Fixes #6075 by preventing that class of potential errors from interface strings
Fixes #6075 by preventing that class of potential errors from interface strings
Fixes #6075 by preventing that class of potential errors from interface strings
Fixes #6075 by preventing that class of potential errors from interface strings
Fixes #6075 by preventing that class of potential errors from interface strings
Fixes #6075 by preventing that class of potential errors from interface strings
Hey there,
not sure if thats already possible but is there a way to provide something like this?
https://psalm.dev/r/16e0076766
Foo::class
is the Interface and thus matches the requirements of the template. But it wont be instantiable because its the interface and thus leads to runtime errors.What I want to have is something like:
So
instantiable
would match for class names which provide eitherfinal __construct
or anything else which is already verified byUnsafeInstantiation
.Is this something which could be achieved?
The text was updated successfully, but these errors were encountered: