Skip to content
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

Pslam errors with FlagBag #192

Open
laurentmuller opened this issue Jun 17, 2022 · 11 comments
Open

Pslam errors with FlagBag #192

laurentmuller opened this issue Jun 17, 2022 · 11 comments

Comments

@laurentmuller
Copy link
Contributor

laurentmuller commented Jun 17, 2022

I have some errors when I use the FlagBag class and test within Psalm.

Example:

namespace App\Enum;

enum Permissions: int
{
    case Execute = 1 << 0;
    case Write = 1 << 1;
    case Read = 1 << 2;
}

I then use this enumeration within FlagBag:

$flagBag = FlagBag::from(Permissions::class);

Pslam report this error:

ERROR: InvalidArgument - Argument 1 of Elao\Enum\FlagBag::from expects Elao\Enum\T|class-string<Elao\Enum\T>, App\Enum\Permissions::class provided (see https://psalm.dev/004)
        FlagBag::from(Permissions::class);

We have annoted the Permissions class ?

@ogizanagi
Copy link
Member

does adding:

 * @template T of \BackedEnum

to the FlagBag::from method solves this?

@laurentmuller
Copy link
Contributor Author

laurentmuller commented Jun 17, 2022

Yes, with this change, Pslam does not more complain about invalid argument.

@ogizanagi
Copy link
Member

PR welcomed then :)

ogizanagi added a commit that referenced this issue Jun 20, 2022
…methods (laurentmuller)

This PR was merged into the 2.x-dev branch.

Discussion
----------

Fix PhpStan issue with FlagBag missing template on static methods

Commits
-------

ccedb24 Correction for PR #192.
@ogizanagi
Copy link
Member

Fixed in #193

@laurentmuller
Copy link
Contributor Author

laurentmuller commented Jun 2, 2023

Hello,

I open this error again because Pslam complains about the following code:

private function getDefaultPermissions(): FlagBag
{
    return FlagBag::from(Permissions::Execute , Permissions::Write, Permissions::Read);
}

Argument 2 of Elao\Enum\FlagBag::from expects Elao\Enum\T, but enum(App\Enum\Permissions::Execute) provided (see https://psalm.dev/004)

Argument 3 of Elao\Enum\FlagBag::from expects Elao\Enum\T, but enum(App\Enum\Permissions::SHOW) provided (see https://psalm.dev/004)

I have modified the from function with this annotation:

/**
  * @template E of \BackedEnum
  * @param class-string<E>|E $enumOrType
  * @param E ...$flags
  * @return FlagBag<E>
  */
public static function from(string|\BackedEnum $enumOrType, \BackedEnum ...$flags): static
{
    //...
}

And it work without error.

@ogizanagi
Copy link
Member

It seems the suggested code triggers an issue with PHPStan :

 ------ -----------------------------------------------------------------------------------------------------------------------------
  Line   FlagBag.php
 ------ -----------------------------------------------------------------------------------------------------------------------------
  90     PHPDoc tag @return with type Elao\Enum\FlagBag<E of BackedEnum> is incompatible with native type static(Elao\Enum\FlagBag<T
         of BackedEnum>).
 ------ -----------------------------------------------------------------------------------------------------------------------------

@laurentmuller
Copy link
Contributor Author

OK. I tested with a new specific PHPStan annotation and it's work for both (PHPStan and Pslam):

/**
 * @param class-string<T>|T $enumOrType
 * 
 * @phpstan-return FlagBag<T>
 *
 * @psalm-template E of \BackedEnum
 * @psalm-param E ...$flags
 * @psalm-return FlagBag<E>
 */
public static function from(string|\BackedEnum $enumOrType, \BackedEnum ...$flags): static
{
    // ...
}

@laurentmuller
Copy link
Contributor Author

Any news about this ?

Thanks

@ogizanagi
Copy link
Member

ogizanagi commented Sep 18, 2023

Does

/**
 * @param class-string<T>|T $enumOrType
 * @param T ...$flags
 *
 * @return FlagBag<T>
 */
public static function from(string|\BackedEnum $enumOrType, \BackedEnum ...$flags): static
{
    // ...
}

work? I don't get why we should add another generic type E (nor specific annotations)

@ogizanagi ogizanagi reopened this Sep 18, 2023
@laurentmuller
Copy link
Contributor Author

It is not working for me. I tried with the following annotations (using a new named template):

/**
  * @template E of \BackedEnum
  *
  * @param class-string<E> $enumType
  *
  * @return FlagBag<E>
  */
public static function fromAll(string $enumType): FlagBag
{
    // ...
}

And it's working! I do the same for the from function:

/**
  * @template E of \BackedEnum
  * 
  * @param class-string<E>|E $enumOrType
  * @param E ...$flags
  * 
  * @return FlagBag<E>
  */
public static function from(string|\BackedEnum $enumOrType, \BackedEnum ...$flags): static
{
    // ...
}

@ogizanagi
Copy link
Member

Great news! Would you be up to provide the PR?

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

No branches or pull requests

2 participants