-
Notifications
You must be signed in to change notification settings - Fork 672
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
Using setters with class-string-map #4460
Labels
Comments
I found these snippets: https://psalm.dev/r/53e6c832e1<?php
namespace Bar;
/**
* @psalm-consistent-constructor
*/
class Foo {}
class A {
/** @var class-string-map<T as Foo, T> */
public static array $map = [];
/**
* @template T as Foo
* @param class-string<T> $class
* @return T
*/
public function get(string $class) : Foo {
if (isset(self::$map[$class])) {
return self::$map[$class];
}
throw new \RuntimeException();
}
/** @param class-string<Foo> $class */
public function set(string $class): void {
self::$map[$class] = new $class();
}
}
|
Yeah, when I added |
This is interesting: https://psalm.dev/r/f8a3440139 using |
I found these snippets: https://psalm.dev/r/f8a3440139<?php
class Container
{
/**
* @psalm-var class-string-map<T, T>
*/
private array $instances = [];
/**
* @psalm-template U of object
* @psalm-param class-string<U> $key
* @psalm-param U $instance
*/
public function set(string $key, object $instance): void
{
if(isset($this->instances[$key])) {} // <- !!!
$this->instances[$key] = $instance;
}
}
|
ptomulik
added a commit
to ptomulik/psalm
that referenced
this issue
Feb 26, 2023
ptomulik
added a commit
to ptomulik/psalm
that referenced
this issue
Feb 26, 2023
ptomulik
added a commit
to ptomulik/psalm
that referenced
this issue
Feb 27, 2023
ptomulik
added a commit
to ptomulik/psalm
that referenced
this issue
Feb 27, 2023
ptomulik
added a commit
to ptomulik/psalm
that referenced
this issue
Feb 28, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've been experimenting with undocumented
class-string-map
type, and I think the following (slight modification of the passing test case) should be type-safe: https://psalm.dev/r/53e6c832e1Related: #4458 (using a
class-string-map
there could be a valid solution).The text was updated successfully, but these errors were encountered: