Skip to content

Fix psalm annotation on Enum::from#146

Merged
mnapoli merged 1 commit into
myclabs:masterfrom
kamazee:fix_psalm_annotations
Jul 4, 2021
Merged

Fix psalm annotation on Enum::from#146
mnapoli merged 1 commit into
myclabs:masterfrom
kamazee:fix_psalm_annotations

Conversation

@kamazee

@kamazee kamazee commented Jul 4, 2021

Copy link
Copy Markdown
Contributor
  • @return static is 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;
  • enum entry's template type T is more precise as a parameter than mixed: it might help catch more errors when using Enum::from.

@mnapoli

mnapoli commented Jul 4, 2021

Copy link
Copy Markdown
Member

Thanks!

@mnapoli mnapoli merged commit 8bef486 into myclabs:master Jul 4, 2021
@kamazee

kamazee commented Jul 4, 2021

Copy link
Copy Markdown
Contributor Author

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 psalm- prefixed) but psalm itself doesn't, likely because of vimeo/psalm#2571 .

Will send another PR in a moment. Sorry about that :(

@Ocramius

Ocramius commented Jul 4, 2021

Copy link
Copy Markdown
Contributor

triggers false-positive alerts from static analyzers and seems to be just a mistake;

FYI, no mistake there: TheActualEnum<string|int> was intentional.

I did not finish up my work in #143 though, sorry

@kamazee

kamazee commented Jul 4, 2021

Copy link
Copy Markdown
Contributor Author

@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 TheActualEnum which already stated that it extended Enum<int|string>.

I'll follow up when I recheck it.

@kamazee

kamazee commented Jul 5, 2021

Copy link
Copy Markdown
Contributor Author

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 static<T> and is okay with just static that extends Enum<string>:
https://phpstan.org/r/7b434d16-9edc-4d44-8304-14f152cb15eb

Psalm detects no issues in such code but shows type of the from result as Colours<empty> which doesn't look right to me:
https://psalm.dev/r/5e1d95b337 (hover a pointer over the last line of the snippet)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants