Fix psalm annotation on Enum::from#146
Conversation
|
Thanks! |
|
This was a bit bold: the fix of return type was correct but I shouldn't have changed the param. Ironically, phpstan picks it up correctly (despite it's Will send another PR in a moment. Sorry about that :( |
FYI, no mistake there: I did not finish up my work in #143 though, sorry |
|
@Ocramius I'll double check tomorrow but it didn't work for me (either psalm or phpstan complained). So, I thought it would be enough to set return type to I'll follow up when I recheck it. |
|
On the following code: <?php
/**
* @psalm-immutable
* @psalm-template T
* @psalm-consistent-constructor
*/
class Enum {
/**
* Cache of instances of the Enum class
*
* @var array
* @psalm-var array<class-string, array<string, static>>
*/
protected static $instances = [];
/**
* @param mixed $value
* @return static
* @psalm-return static<T>
*/
public static function from($value): self
{
throw new RuntimeException();
}
}
/**
* @method static self RED()
* @psalm-immutable
* @template-extends Enum<string>
*/
class Colours extends Enum {
protected const RED = '#f00';
}
function foo(Colours $entry): void {
var_export($entry);
}
foo(Colours::from('red'));phpstan chokes on Psalm detects no issues in such code but shows type of the |
@return staticis accurate enough, so it doesn't need a clarification for psalm.static<T>triggers false-positive alerts from static analyzers and seems to be just a mistake;Tis more precise as a parameter thanmixed: it might help catch more errors when usingEnum::from.