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

How to use pseudo scalar type trait-string #3865

Closed
smoench opened this issue Jul 22, 2020 · 6 comments
Closed

How to use pseudo scalar type trait-string #3865

smoench opened this issue Jul 22, 2020 · 6 comments

Comments

@smoench
Copy link

smoench commented Jul 22, 2020

Unfortunately I couldn't find much documentation about trait-string and couldn't get it running as I thought it should be used:

https://psalm.dev/r/615d86ff95

Probably there is a bug or documentation needs to be updated.

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/615d86ff95
<?php
namespace Bar;

trait Foo {}

/**
 * @param trait-string $traitString
 * @return trait-string
 */
function takesTraitString(string $traitString) {
    return $traitString;
}


takesTraitString('Bar\Foo');
takesTraitString(Foo::class);
Psalm output (using commit 983f233):

ERROR: InvalidScalarArgument - 15:18 - Argument 1 of Bar\takesTraitString expects trait-string, string(Bar\Foo) provided

ERROR: InvalidScalarArgument - 16:18 - Argument 1 of Bar\takesTraitString expects trait-string, Bar\Foo::class provided

@orklah
Copy link
Collaborator

orklah commented Jul 22, 2020

I think trait-string is inferred when you pass through trait_exists:
https://psalm.dev/r/3bff912a5b

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/3bff912a5b
<?php
namespace Bar;

trait Foo {}

/**
 * @param trait-string $traitString
 * @return trait-string
 */
function takesTraitString(string $traitString) {
    return $traitString;
}
$a = Foo::class;
if(trait_exists($a)){
	takesTraitString($a);
}
Psalm output (using commit 983f233):

No issues!

@smoench
Copy link
Author

smoench commented Jul 22, 2020

Interesting 🤔 Thank you @orklah

@muglug muglug closed this as completed Jul 22, 2020
@muglug
Copy link
Collaborator

muglug commented Jul 22, 2020

I would say trait-string is something you pretty much never need. I only support it in Psalm for completeness (hence why there's no documentation on it).

@ptomulik
Copy link
Contributor

ptomulik commented Jun 7, 2024

Hi,

phpunit 11.2 introduced CoversTrait attribute, and it uses the trait-string. But it seems that trait-string is still kind of broken. So running psalm on projects using phpunit 11.2 yields tons of InvalidArgument errors

InvalidArgument: Argument 1 of PHPUnit\Framework\Attributes\CoversTrait::__construct expects trait-string, but FooTrait::class provided (see https://psalm.dev/004)

Can we do something about it?

ptomulik added a commit to phptailors/logic that referenced this issue Jun 7, 2024
  trait-string simply does not work in psalm,

  vimeo/psalm#3865
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

4 participants